Home >Backend Development >PHP Tutorial >Select delete with ajax?
Attach my code first
<code><button class="box" data-removetype="select_del">刪除</button> <form id="selectid"> <? while ($row = mysql_fetch_array($s)) { <input type="checkbox" name="id[]" value="<? echo $row['mail_id'];?>"> } ?> </form> $(".box").click(function(e){ var removetype = e.currentTarget.dataset.removetype; /*mailbox*/ if (removetype=='select_del') { $.ajax({ type: "POST", url:"del?to="+removetype, data:$("#selectid").serialize(), cache: false, success: function(){ } }); } }); </code>
del
<code>$user = $_GET['to']; if ($user=='select_del') { $getid=$_POST['id']; foreach($getid as $value) { $DEL = mysql_query(" DELETE FROM `資料表名稱` WHERE `id` = '".$value."' "); } } </code>
The above is my code...
But it doesn't work
So I want to ask if this is the right way to do it?
Make sure the ID and data table name are correct
Attach my code first
<code><button class="box" data-removetype="select_del">刪除</button> <form id="selectid"> <? while ($row = mysql_fetch_array($s)) { <input type="checkbox" name="id[]" value="<? echo $row['mail_id'];?>"> } ?> </form> $(".box").click(function(e){ var removetype = e.currentTarget.dataset.removetype; /*mailbox*/ if (removetype=='select_del') { $.ajax({ type: "POST", url:"del?to="+removetype, data:$("#selectid").serialize(), cache: false, success: function(){ } }); } }); </code>
del
<code>$user = $_GET['to']; if ($user=='select_del') { $getid=$_POST['id']; foreach($getid as $value) { $DEL = mysql_query(" DELETE FROM `資料表名稱` WHERE `id` = '".$value."' "); } } </code>
The above is my code...
But it doesn't work
So I want to ask if this is the right way to do it?
Make sure the ID and data table name are correct
<code>data:$("#selectid").serialize()</code>
You can try to replace with
<code>data:$("#selectid").find('input').serialize()</code>
If it doesn’t work, I think you need to check how the script is written to see if it complies with the specifications. For example: <a class=" <?php if($status==1): ?>selected<?php endif ; ?>" >RETURN</a>
You wrote like this
<code><? while ($row = mysql_fetch_array($s)) { <input type="checkbox" name="id[]" value="<? echo $row['mail_id'];?>"> } ?></code>
I don’t think it will work properly....