我正在使用新的方式通过ajax将数据发布到php页面,而不需要页面刷新,但响应不打印到控制台。
注意:我还没有收集表单数据发送,直到我可以真正得到ajax的工作。
HTML:<form id="myForm" action="process.php" method="post">
<input type="text" placeholder="Email" id="email" name="email">
<span class="error">Email not entered</span><br />
<input type="password" placeholder="Password" id="pword" name="pword">
<span class="error">Password not entered</span><br />
<input type="text" placeholder="First Name" id="fname" name="fname">
<span class="error">First Name not entered</span><br />
<input type="text" placeholder="Last Name" id="lname" name="lname">
<span class="error">Last Name not entered</span><br />
<input type="submit" value="Submit">
</form>
jQuery: $('#myForm').on('submit', function(e){e.preventDefault()});
var request = $.ajax({
url : 'process.php',
method : 'POST',
data : {name: 'Robert'},
dataType : 'html'
});
request.done(function(response){
console.log(response);
});
request.fail(function(){
console.log('fail');
});
PHP: <?php
echo "<pre>";
print_r($GLOBALS);
echo "</pre>";
?>
您的on submit函数没有做任何事情。您需要在其中包含ajax调用。
试题:
jQuery(document).ready(function ($) {
$('#myForm').on('submit', function(e){
e.preventDefault();
$.ajax({
url : 'process.php',
method : 'POST',
data : {name: 'Robert'},
dataType : 'html'
})
.done(function(response){
console.log(response);
})
.fail(function(){
console.log('fail');
});
});
});
我也不知道你在哪里加载JS。但是你可能想把它包装在一个文档准备函数中,以确保当它被调用时你的表单存在。
最后有一个非常好的表单插件,将使这对你来说更容易。JQuery.Form .
http://malsup.com/jquery/form/