AJAX调用PHP文件,它从数据库中删除一行? [英] AJAX to call PHP file which removes a row from database?

查看:127
本文介绍了AJAX调用PHP文件,它从数据库中删除一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我昨天问了一个问题,如何保存用户的博客帖子。我想出了数据库端,它工作正常。现在,我想点击一个onclick按钮后删除一个博客文章。通过我在网上挖掘的时间,我发现调用一个jQuery AJAX函数是最好的方式去。

Alright, so I asked a question yesterday regarding how to save the blog posts that a user makes. I figured out the database side of it, and that works fine. Now, I want to REMOVE a blog post based after clicking an onclick button. Through my hours of digging through the web, I've found calling an jQuery AJAX function is the best way to go about it. I've been tooling around with it, but I can't get this working.

在blog.php中从数据库检索的博客代码:

Blog code retrieved from database in blog.php:

$connection = mysql_connect("...", "...", "...") or die(mysql_error());
$database = mysql_select_db("...") or die(mysql_error());

$query = mysql_query("SELECT * FROM template") or die(mysql_error());
$template = mysql_fetch_array($query);

$loop = mysql_query("SELECT * FROM content ORDER BY content_id DESC") or die (mysql_error());
while ($row = mysql_fetch_array($loop))
{
        print $template['Title_Open'];
        print $row['title'];
        print '<button class="deletePost" onClick="deleteRow(' . $row['content_id'] . ')">Remove Post</button>';
        print $template['Title_Close'];

        print $template['Body_Open'];
        print $row['body'];
        print $template['Body_Close'];
}

mysqli_close($connection);

这会在home.php上创建以下HTML:

This creates the following HTML on home.php:

<div class="blogtitle" class="post3">Title
<button class="deletePost" onClick="deleteRow(3)">Remove Post</button></div>
<div class="blogbody" class="post3">Content</div>

点击按钮时应该调用我的remove.js(这是我开始丢失什么'm doing):

Which should call my remove.js when button is clicked (This is where I start to lose what I'm doing):

$function deleteRow(id){
    $.ajax({
        url: "remove.php",
            type: "POST",
            data: {action: id}
    });
    return false;         
};

调用remove.php(不知道我在做什么):

Calling remove.php (No idea what I'm doing):

$con=mysqli_connect("...","...","...","...");
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$id = $_POST['action'];

$query = mysql_query("DELETE FROM content WHERE content_id=$id") or die(mysql_error());

我的目标是从表中移除带有ID的行,

My goal here is to REMOVE the row with the ID from the table which would in turn remove the blog post entirely since it won't see the row when it loops through the database table.

任何想法?

感谢您的帮助,
Kyle

Thanks for your help, Kyle

推荐答案

原始代码中的几个问题:Jquery中的函数shouldn' t在开头使用一个$符号,因为你需要传递一个单一的值,我会使用查询字符串而不是POst,而不是在php中调用die我会使用受影响的行返回回调是否或者没有删除该值。但这只是我的方法,有其他方式我相信。

couple of issues in your original code: the functions in Jquery shouldn't use a $ sign at the beginning and since you need to pass a single value I would use the query string rather than the POst, and instead of calling the "die" in php I would use the affected rows to return the callback of whether or not the value was deleted. But this is just my approach, there other ways I'm sure.

以下是代码的一些改进:

Here are little improvements in you code:

    //HTML
    <div class="blogtitle" class="post3">Title
    <button class="deletePost" data-item="3" >Remove Post</button></div>
    <div class="blogbody" class="post3">Content</div>

   //JQUERY
    jQuery(document).ready(function($) {

        $('button.deletePost').each(function(){

            var $this = $(this);

            $this.click(function(){

                var deleteItem  = $this.attr('data-item');

                $.ajax({url:'remove.php?action='+deleteItem}).done(function(data){

                    //colect data from response or custom code when success

                });
             return false;  
            });

        });


    });


   //PHP
    <?php 

    $id = $_REQUEST['action'];
    $query = mysql_query('DELETE FROM content WHERE content_id="'.$id.'"');
    $confirm = mysql_affected_rows() > 0 ? echo 'deleted' : echo 'not found or error';

     ?>

希望此示例有助于快乐编码!

Hope this sample helps :) happy coding !

这篇关于AJAX调用PHP文件,它从数据库中删除一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆