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