首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用JSON编码的数据定义带有sql行id的元素

如何使用JSON编码的数据定义带有sql行id的元素
EN

Stack Overflow用户
提问于 2014-11-18 17:58:17
回答 1查看 86关注 0票数 0

我正在使用jQuery AJAX处理表单数据,它的PHP一方面应该删除服务器上的两个文件,然后删除数据库中的SQL行(用于发送给它的id )。然后,包含SQL行的元素应该更改颜色、向上移动、删除,然后下一个SQL行移到它的位置。动画内容出现在ajax回调的beforeSend函数和成功函数中。

此脚本无法工作,当用户单击按钮时,页面url将更改为php脚本,但该项和文件不会在服务器或数据库中被删除。也没有任何动画发生。

这是我第一次使用jQuery ajax,我认为在回调过程中如何定义元素存在一个问题。任何帮助都是很好的:

js

代码语言:javascript
复制
$("document").ready(function(){
    $(".delform").submit(function(){ 
data = $(this).serialize() + "&" + $.param(data);
        if (confirm("Are you sure you want to delete this listing?")) {
            $.ajax({
                type: "POST",
                dataType: "json",
                url: "delete_list.php",
                data: data,
                beforeSend: function()  {
                    $( "#" + data["idc"] ).animate({'backgroundColor':'#fb6c6c'},600);      
                                        },
                success: function() {
                    $( "#" + data["idc"] ).slideUp(600,function() {
                    $( "#" + data["idc"] ).remove();
                                          });
                                    }
                    });
                    return false;
        }
    });
});

php

代码语言:javascript
复制
  if (isset($_POST["id"])) 
{
$idc = $_POST["id"];

if (isset($_POST["ad_link"]) && !empty($_POST["ad_link"])) 
    {
$ad_linkd=$_POST["ad_link"];
unlink($ad_linkd);    
    }

if (isset($_POST["listing_img"]) && !empty($_POST["listing_img"])) 
        {
$listing_imgd=$_POST["listing_img"];
unlink($listing_imgd);    
        }

try                 {
require('../dbcon2.php');
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "DELETE FROM listings WHERE id = $idc";
$conn->exec($sql);

             }
catch (PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
                    }
    echo json_encode($idc);       
 }

html

代码语言:javascript
复制
<div id="record-<?php echo $id; ?>">
           *bunch of stuff*
   <form method="post" class="delform">
<input name="id" type="hidden" id="id" value="<?php echo $id; ?>" />
<input name="ad_link" type="hidden" id="ad_link" value="<?php echo $ad_link; ?>" />
<input name="listing_img" type="hidden" id="listing_img" value="<?php echo $listing_img; ?>" />
<button type="submit">Delete</button>
  </form>
    </div>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-18 18:57:01

您应该像这样修复php代码

代码语言:javascript
复制
try {
    require('../dbcon2.php');
    // It's better, if you will going to use MySQL DB, use the class designed to connect with it.
    $conn = mysqli_connect("Servername", "usernameDB", "PasswordDB", "NameDB");
    $sql = "DELETE FROM listings WHERE id = $idc";
    mysqli_query($conn, $sql);
    // you have to create a asociative array for a better control
    $data = array("success" => true, "idc" => $idc);
    // and you have to encode the data and also exit the code.
    exit(json_encode($data));
} catch (Exception $e) {
    // you have to create a asociative array for a better control
    $data = array("success" => false, "sentence" => $sql, "error" => $e.getMessage());
    // and you have to encode the data and also exit the code.
    exit(json_encode($data));
}

现在,在您的JS代码中,Ajax将改为此。

代码语言:javascript
复制
$.ajax({
    type: "POST",
    dataType: "json",
    url: "delete_list.php",
    data: data,
    beforeSend: function()  {
        $( "#" + data["idc"] ).animate({'backgroundColor':'#fb6c6c'},600);      
    },
    success: function(response) {
        // the variable response is the data returned from 'delete_list.php' the JSON
        // now validate if the data returned run well
        if (response.success) {
            $( "#" + response.idc ).slideUp(600,function() {
                $( "#" + response.idc ).remove();
            });
        } else {
            console.log("An error has ocurred: sentence: " + response.sentence + "error: " + response.error);
        }
    },
    // add a handler to error cases.
    error: function() {
        alert("An Error has ocurred contacting with the server. Sorry");
    }
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27000840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档