在ajax中编辑多个值

时间:2017-02-14 06:04:22

标签: php ajax

我正在尝试使用ajax和php编辑两列。我的代码当前在我的表中编辑了一个值(名称)并将其保存到我的数据库。当我添加第二个变量(p)时,我的ajax调用它会更新两列p和y具有相同的值。如何编辑第三个值并为其指定一个不同的值yI希望两个不同的列在我的db中具有不同的值(列:名称和容量)

此代码编辑并更新两个值:

 <script type="text/javascript">
jQuery(document).ready(function() {  
        $.fn.editable.defaults.mode = 'popup';
        $('.xedit').editable();     
        $(document).on('click','.editable-submit',function(){
            var x = $(this).closest('td').children('span').attr('id');
            var y = $('.input-sm').val();
            var z = $(this).closest('td').children('span');
            $.ajax({
                url: "process.php?id="+x+"&data="+y,
                type: 'GET',
                success: function(s){
                    if(s == 'status'){
                    $(z).html(y);}
                    if(s == 'error') {
                    alert('Error Processing your Request!');}
                },
                error: function(e){
                    alert('Error Processing your Request!!');
                }
            });
        });
});
</script>

这就是我试图编辑三个值的原因:

     <script type="text/javascript">
jQuery(document).ready(function() {  
        $.fn.editable.defaults.mode = 'popup';
        $('.xedit').editable();     
        $(document).on('click','.editable-submit',function(){
            var x = $(this).closest('td').children('span').attr('id');
            var y = $('.input-sm').val();
            var p = $('.input-sm').val();
            var z = $(this).closest('td').children('span');
            $.ajax({
                url: "process.php?id="+x+"&data="+y+"&capacity="+y,
                type: 'GET',
                success: function(s){
                    if(s == 'status'){
                    $(z).html(y);
                    $(z).html(p);}
                    if(s == 'error') {
                    alert('Error Processing your Request!');}
                },
                error: function(e){
                    alert('Error Processing your Request!!');
                }
            });
        });
});
</script>

继承我的php文件(process.php)

    <?php
include("connect.php");
if
    ($_GET['id'],$_GET['capacity'] and $_GET['data'])
{
    $id = $_GET['id'];
    $data = $_GET['data'];
    $capacity = $_GET['capacity'];
    if(mysqli_query($con,"update mytable set name='$data',capacity='$data' where id='$id'")){

        echo "success";
    }

else{
echo 'failed';
}

}
?>

我在index.php中的表

 <tbody>
                    <?php
                    $query = mysqli_query($con,"select * from mytable");
                    $i=0;
                    while($fetch = mysqli_fetch_array($query))
                    {
                        if($i%2==0) $class = 'even'; else $class = 'odd';

                        echo'<tr class="'.$class.'">

                            <td><span class= "xedit external-event bg-brown" id="'.$fetch['id'].'">'.$fetch['name'].'</span></td>

                            <td><span class= "xedit external-event bg-brown" id="'.$fetch['id'].'">'.$fetch['capacity'].'</span></td>

                        </tr>';                         
                    }
                    ?>
                    </tbody>

2 个答案:

答案 0 :(得分:1)

1)您的正确拼写错误:capacity=$data查看此行并将其更改为capacity=$capacity

if(mysqli_query($con,"update mytable set name='$data',capacity='$capacity' where id='$id'"))

2)然后看看If条件。最终你的代码应该是这样的。

        <?php

        include("connect.php");
        if($_GET['id'] && $_GET['capacity'] && $_GET['data'])
        {
            $id = $_GET['id'];
            $data = $_GET['data'];
            $capacity = $_GET['capacity'];
            if(mysqli_query($con,"update mytable set name='$data',capacity='$capacity' where id='$id'"))
            {

                echo "success";
            }
            else
            {
                echo 'failed';
            }

        }

        ?>

答案 1 :(得分:0)

您的SQL查询中有错误。因为你没有传递正确的参数。 请参阅下面的代码。

$id = $_GET['id'];
$data = $_GET['data'];
$capacity = $_GET['capacity'];

// Check Sql
$query = "update mytable set name='$data',capacity='$capacity' where id='$id'";
if(mysqli_query($con,$query)){

  echo "success";
} else{
    echo 'failed';
}