使用PHP无法获得AJAX帖子

时间:2013-09-03 23:22:11

标签: php javascript jquery mysql ajax

我最近才开始学习AJAX和PHP,并且一直在尝试使用MySql。我一直在尝试创建一个网页,将变量传递到php页面,将它们输入数据库,然后返回一个指示是否成功的回显。出于某种原因,我无法使用AJAX或Javascript(不确定)。它可能是显而易见的,所以任何帮助都会很棒!这是由onClick -

调用的AJAX和Javascript代码
< script langauge = "Javascript"  type ="text/javascript">
    function Generate(){

   var maxint = document.getElementById("Max").value;
   var minint = document.getElementById("Min").value;

$.ajax({
    type: "POST",
    async: true,
    url: "Untitled-1.php",
    data:  { 'senduser': maxint, 'sendpass': minint },
    success: function (msg) 
            { alert(msg) },
    error: function (err)
    { alert(err.responseText)}
});
} </script>

这是php代码......

<?php
       $user = $_POST['senduser'];
       $password = $_POST['sendpass'];
     $con=mysqli_connect("host","username", "password","dbname");

      if (mysqli_connect_errno())
 {
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }
 else{
   mysqli_query($con,"INSERT into Customers (password,username) 
                                VALUES ('$password','$user')");
         echo "You have signed up!";
        mysqli_close($con);
      }
           ?>

1 个答案:

答案 0 :(得分:0)

最显而易见的是,你与之间存在差距。和脚本。肯定是 '

还有其他小点,但这是一个工作版本。请注意,您的网页地址可能需要具有root权限。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>
<a href="javascript:void(0)" onclick="Generate()">Go Man Go</a>
<input type="text" value="12" id="Min" />
<input type="text" value="12" id="Max" />
<script type="text/javascript">

    function Generate() {

        var maxint = $("#Max").val();
        var minint = $("#Min").val();

        $.ajax({
            type: "POST",
            async: true,
            url: "Untitled-1.php",
            data: { senduser: maxint, sendpass: minint },
            success: function (msg)
            { alert(msg) },
            error: function (err)
            { alert(err.responseText) }
        });
    }

</script>