Home > Backend Development > PHP Tutorial > Select delete with ajax?

Select delete with ajax?

WBOY
Release: 2016-09-12 17:44:44
Original
1221 people have browsed it

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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

Reply content:

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login

You can try to replace with

<code>data:$("#selectid").find('input').serialize()</code>
Copy after login

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>
Copy after login

I don’t think it will work properly....

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template