【发布时间】:2016-05-01 17:34:57
【问题描述】:
我有一个表单,我通过 ajax 将其发送到 php 文件以操作数据库。
//html
<form id='editUserForm' action='insert.php' method='post'>
<input type='text' name="userName/>
<input type='text' name="userLastName/>
<input type='submit' name='editUser' value='submit'/>
</form>
//ajax
(function($){
function processForm( e ){
$.ajax({
url: 'insert.php',
dataType: 'text',
type: 'post',
contentType: 'application/x-www-form-urlencoded',
data: $(this).serialize(),
success: function( data, textStatus, jQxhr ){
alert("Done");
error: function( jqXhr, textStatus, errorThrown ){
console.log( errorThrown );
}
});
e.preventDefault();
}
$('#editUserForm').submit( processForm );
})(jQuery);
//插入.php
if(isset($_POST['editUser'])){
if(isset($_POST['chUserStatus'])){
$active='َactive';
}else{$active='disabled';}
$query="update admins set user='$_POST[chUserName]', pass='$_POST[chUserPass]',email='$_POST[chUserEmail]',level='$_POST[chUserLevel]',status='$active' where id=$_POST[userId]";
$result=mysqli_query($dbCnn,$query);
echo(mysqli_error($dbCnn));
} 问题就在这里,因为我的函数阻止了表单默认提交,它不会将提交 btn 名称/值发布到 insert.php。如何将其作为参数发送到 insert.php?
【问题讨论】: