以下代碼有什麼問題?爲什麼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