简体   繁体   English

无法使用PHP获得AJAX发布

[英]Can't get AJAX post with PHP to work

I have only recently started to learn AJAX and PHP and have been experimenting with MySql. 我只是最近才开始学习AJAX和PHP,并一直在尝试MySql。 I have been trying to create a webpage which passes variables into a php page, enters them into the database and then returns an echo indicating whether it was successful or not. 我一直在尝试创建一个网页,该网页将变量传递到php页面,将它们输入数据库,然后返回一个回显以指示是否成功。 For some reason i cant get the AJAX or Javascript (not sure) to work. 由于某些原因,我无法使AJAX或Javascript(不确定)正常工作。 It's probably something obvious so any help would be great! 这可能很明显,所以任何帮助都会很棒! This is the AJAX and Javascript code which is invoked by an onClick - 这是由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>

and this is the php code... 这是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);
      }
           ?>

The most obvious thing is that you have a gap between the < and the script. 最明显的是,<和脚本之间存在间隙。 It must be ' 一定是 '

There are other smaller points but here is a working version. 还有其他一些小问题,但这是一个可行的版本。 Note that the address of your page may need to be root relitive. 请注意,页面的地址可能需要相对于根目录。

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM