Ajax:数据不会发送到php url

时间:2017-06-21 02:11:00

标签: javascript php sql ajax database

点击特定链接时,我无法上传数据库信息。

数据不会发送到upload.php 正在调用myfunc。单击链接时会显示成功消息。

使用Javascript:

<script type="text/javascript">

            function myfunc(a, b)
    {
                          $.ajax({
                            url: "upload.php",
                            type: "POST",
                            data: {"a": a, "b": b},
                            success:function() {
                             alert( "Done");
                            }
                           });
    }


        </script>

upload.php的:

if (isset($_POST['a']) && isset($_POST['b'])) 
    {


        $a = $_POST['a'];
        $b  = $_POST['b'];


        $query1 = $db->prepare('UPDATE  users SET a = a + 1 where uid="'.$a.'"');                                        
        $query1->execute();

        $query2 = $db->prepare('UPDATE  users SET b = b + 1 where uid="'.$b.'"');                                        
        $query2->execute();

        if (!$query1 || !$query2)
        { 
            echo "Erreur SQL";
            exit();
        }

    }       

谢谢。

1 个答案:

答案 0 :(得分:0)

您是否先检查数据是否实际发布到upload.php?检查:

使用Javascript:

function myfunc(a, b)
{
  $.ajax({
    url: "upload.php",
    type: "POST",
    data: {"a": a, "b": b},
    success:function(result) {
        alert(result);
        alert("Done");
    }
   });
}

upload.php的

<?php

echo 'a = '.$_POST['a'] . "\n" . 'b = '.$_POST['b']; exit;

if (isset($_POST['a']) && isset($_POST['b'])) 
{
    $a = $_POST['a'];
    $b  = $_POST['b'];


    $query1 = $db->prepare('UPDATE  users SET a = a + 1 where uid="'.$a.'"');                                        
    $query1->execute();

    $query2 = $db->prepare('UPDATE  users SET b = b + 1 where uid="'.$b.'"');                                        
    $query2->execute();

    if (!$query1 || !$query2)
    { 
        echo "Erreur SQL";
        exit();
    }
}     

?>

那应该在弹出窗口中显示a和b的值。如果显示那些,则表示数据正在传递给upload.php。