0

我最近才开始学习 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);
      }
           ?>
4

1 回答 1

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>
于 2013-09-03T23:41:20.890 回答