我正在尝试使用ajax将一个简单的表单插入我的数据库(使用insert.PHP)来练习. var_dump($email)下面是空的.脚本贯穿到这里:
echo "Data for $name inserted successfully!";
问题是如上所述变量为空.
插入数据成功!
我在这里错过了什么吗?
的index.PHP
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<!-- The ajax/jquery stuff -->
<script type="text/javascript">
$(document).ready(function(){
//Get the input data using the post method when Push into MysqL is clicked .. we pull it using the id fields of ID, Name and Email respectively...
$("#insert").click(function(){
//Get values of the input fields and store it into the variables.
var name=$("#name").val();
var email=$("#email").val();
//use the $.post() method to call insert.PHP file.. this is the ajax request
$.post('insert.PHP', {name: name, email: email},
function(data){
$("#message").html(data);
$("#message").hide();
$("#message").fadeIn(1500); //Fade in the data given by the insert.PHP file
});
return false;
});
});
</script>
</head>
<body>
<form>
<label>Name: </label> <input id="name" type="text" />
<label>E-Mail: </label> <input id="email" type="text" />
</form>
<a id="insert" title="Insert Data" href="#">Push into MysqL</a>
<!-- For displaying a message -->
<div id="message"></div>
</body>
</html>
insert.PHP
<?PHP
//Configure and Connect to the Databse
include "db_conx.PHP";
if (!$db_conx) {
die('Could not connect: ' . MysqLi_error());
}
//Pull data from home.PHP front-end page
$name=$_POST['name'];
$email=$_POST['email'];
echo "<pre>";
var_dump($email);
echo "</pre><br>";
//Insert Data into MysqL INSERT INTO best_rate (name,email)
$query= "INSERT INTO best_rate(name,email) VALUES('$name','$email')";
$result = MysqLi_query($db_conx,$query);
if($query){
echo "Data for $name inserted successfully!";
}
else{ echo "An error occurred!"; }
?>
更新PHP#2
<?PHP
//Configure and Connect to the Databse
include "db_conx.PHP";
if (!$db_conx) {
die('Could not connect: ' . MysqLi_error());
}
//Pull data from home.PHP front-end page
$name=$_POST['myname'];
$email=$_POST['myemail'];
echo "<pre>";
var_dump($email);
echo "</pre><br>";
//Insert Data into MysqL INSERT INTO best_rate (name,email)
$query= "INSERT INTO best_rate(name,email) VALUES('$name','$email')";
$result = MysqLi_query($db_conx,$query);
if($query){
echo "Data for $name inserted successfully!";
}
else{ echo "An error occurred!"; }
?>
HTML#2
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<!-- The ajax/jquery stuff -->
<script type="text/javascript">
$(document).ready(function(){
//Get the input data using the post method when Push into MysqL is clicked .. we pull it using the id fields of ID, Name and Email respectively...
$("#insert").click(function(){
//Get values of the input fields and store it into the variables.
var name=$("#name").val();
var email=$("#email").val();
//use the $.post() method to call insert.PHP file.. this is the ajax request
$.post('insert.PHP', {myname: name, myemail: email},
function(data){
$("#message").html(data);
$("#message").hide();
$("#message").fadeIn(1500); //Fade in the data given by the insert.PHP file
});
return false;
});
});
</script>
</head>
<body>
<form>
<label>Name: </label> <input id="name" type="text" name="myname"/>
<label>E-Mail: </label><input id="email" type="text" name="myemail"/>
</form>
<a id="insert" title="Insert Data" href="#">Push into MysqL</a>
<!-- For displaying a message -->
<div id="message"></div>
</body>
</html>
表结构
===============================================
id | name | email
db_conx.PHP
<?PHP
$db_conx = MysqLi_connect("localhost", "user", "pass", "database");
if (MysqLi_connect_errno()) {
echo MysqLi_connect_error();
exit();
}
?>
解决方法:
我可以看到你有post方法问题,所以我们可以使用$.get而不是$.post并接收$_GET [“name”]上的数据
谢谢