以下代码有什么问题?为什么Ajax表单提交不起作用?
警报返回成功,但没有添加到表中。形式:
<form id="Form_1" name="theform" method="post" action="">
<label for="email" class="emailLab">Email * </label>
<input type="email" name="email" id="email_id" autocorrect placeholder="Email" value="" />
<label for="phone">Phone </label>
<input type="tel" name="phone" id="phone_id" autocorrect placeholder="Phone" />
<input type="image" src="images/btn_enter.png" height="26" width="147" id="submit" data-inline="true" data-role="none" />
</form>
在提交时我调用这个函数:
function serverDB(){
// Define variables from input
var vEmail = document.getElementById('email_id').value;
var vPhone = document.getElementById('phone_id').value;
var theData = 'email=' + vEmail + '&phone=' +vPhone;
alert (theData);
$.ajax({
type: "POST",
url: "process.php",
data: theData,
success: function(){
alert ("Success");
}
});
}
而且PHP代码:
<?
$email = $_POST['email'];
$phone = $_POST['phone'];
mysql_connect("host", "user", "password") or die(mysql_error());
mysql_select_db("sandbox_itouch") or die(mysql_error());
mysql_query("INSERT INTO `data` (email, phone) VALUES ('$email','$phone')");
?>
这与您的问题无关,但可以考虑使用[PDO](http://www.php.net/PDO)或[mysqli](http://www.php.net/mysqli)您。使用预先准备好的语句来防止[SQL注入攻击](http://en.wikipedia.org/wiki/SQL_injection) – Callum 2011-05-24 02:18:05