dongnai6973 2014-10-07 05:43
浏览 81
已采纳

使用Ajax / Json更新Mysql记录不起作用

What I'm trying to do is to edit mysql records using php. I've used Ajax/Json to edit a single record, but the problem is my codes isn't working. I tried to alert the value of input element after I clicked the save button and the alert output is verified. And also I don't get any message in console.

Here's what I got right now. Any help will appreciate.

Index.php

<div class="entry-form1">
<form action="" method="post">
<input type="text" name="id_edit" id="id_edit" class="inputs_edit">
<input type="text" name="approved_edit" id="approved_edit" class="inputs_edit">
<input type="submit" name="save_edit" id="save_edit" value="Save"/>
</form>
</div>

Search.php

$query1 = $mysqli->query(""); // not to include
while($r = $query1->fetch_assoc()){
<td><a href='#' name='".$r['id']."' id='".$r['pr_id']."' class='edits'>Edit</a></td>
}

  <script>
  $(document).ready(function(){

$(".edits").click(function(){
    $(".entry-form1").fadeIn("fast");
    //not to include some parts of codes
    $.ajax({
    type: "POST",
    url: "auto-complete.php",
    data :edit_post_value,
    dataType:'json',
    success:function(data){
    var requested=data.requested;
    var id=data.id;
    //send to element ID
    $('#id_edit').val(id);
    $('#requested_edit').val(requested);
    }
    });

    $("#save_edit").click(function () {
    var two = $('#id_edit').val();
    var five = $('#requested_edit').val();
    alert(five);
            $.ajax({
            type: "POST",
            url: "item_edit.php",
             data: "id_edit="+two+"&requested_edit="+five,
            dataType:'json',
            success: function(data){
            console.log(JSON.stringify(data))
                if(data.success == "1"){
                        $(".entry-form1").fadeOut("fast");
                    //setTimeout(function(){ window.location.reload(); }, 1000);        
                }
            }
        });
    });
});
</script>

Item_edit.php

<?php
$mysqli = new mysqli("localhost", "root", "", "app");

if(isset($_POST['id_edit'])) {
$id_edit= $_POST['id_edit'];
$requested_edit= $_POST['requested_edit'];
$sql = $mysqli->query("UPDATE pr_list SET requested='$requested_edit' WHERE id='$id_edit'");

        if($sql){
            echo json_encode(array( "success" => "1"));
        }else{
            echo json_encode(array("success" => "0"));
        }
}
?>
  • 写回答

1条回答 默认 最新

  • douma5954 2014-10-07 06:46
    关注

    1) First, you're not capturing the click event, because $("# save_edit") is within a function that is not being called. So, you're not even sending the form to the server.

    2) Second, the way a form works by default send the data and then reload the page, you must call the preventDefault() function from the event object captured to prevent it, before making the ajax call.

    try this:

    $(document).ready(function(){
    
        $("#save_edit").click(function (e) {
            e.preventDefault(); //prevent a page reload
    
            var two = $('#id_edit').val();
            var five = $('#requested_edit').val();
            alert(five);
    
            $.ajax({
                type: "POST",
                url: "/item_edit.php",
                data: "id_edit="+two+"&requested_edit="+five,
                dataType:'json',
                success: function(data){
    
                    console.log(JSON.stringify(data));
                     if(data.success == "1"){
                                $(".entry-form1").fadeOut("fast");
                            //setTimeout(function(){ window.location.reload(); }, 1000);        
                        }
                    }
        });
    
        });
    
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么