我想用 AJAX 提交我的表单。我编写了一个通过按下按钮来触发它的函数:
function putUser() {
$('button#putUser').on('click', function() {
var user = $('input#user').val(),
amount = $('input#amount').val(),
what = $('input#amount').val(),
country = $('input#country').val(),
platform = $('input#platform').val(),
formUrl = $('form#sendUser').attr('action');
var data = {
user: user,
amount: amount,
what: what,
country: country,
platform: platform
}
$.post(formUrl, $('#sendUser').serialize(), function() {
alert('test');
}).done(function() { alert('done')})
return false;
})
}
这在理论上可行,因为我变得done
警觉。但是没有数据被提交。怎么了?我的 SQL 语句工作得很好。
我的PHP代码:
if(isset($_POST['putUser'])) {
$user = $_POST['user'];
$amount = $_POST['amount'];
$what = $_POST['what'];
$country = $_POST['country'];
$platform = $_POST['platform'];
$query = mysql_query('INSERT INTO sells (id, user, amount, what, country, platform) '
. 'VALUES (NULL , "' . mysql_real_escape_string($user) . '", "' . mysql_real_escape_string($amount) . '", "' . mysql_real_escape_string($what) . '", "' . mysql_real_escape_string($contry) . '", "' . mysql_real_escape_string($platform) . '")');
if($query) {
echo 'ok';
} else {
die(mysql_error());
}
}