我目前在我的html页面中使用php,并且在同一页面中有一个操作在表单提交时执行。目前整个表单被重新加载,而我希望php操作在后台运行而不重新加载。我该怎么做。我还想在单击提交按钮后清除文本框。以下是我目前正在使用的代码
<html>
<head>
</head>
<body>
<form action="index.php" method="post" role="form">
<input type="email" placeholder="Enter email" name="email_address">
<button class="btn btn-primary custom-button red-btn">Sign Up</button>
</form>
<?php
if(isset($_POST['email_address']) !(trim($_POST['email_address']) == ''))
// do some action
}
?>
</body>
</html>
注意:以下代码适用于我
<script type="text/javascript">
$('#contactForm').submit(function () {
$.post("mailer.php",$("#contactForm").serialize(), function(data){
});
return false;
});
</script>
答案 0 :(得分:4)
您需要使用AJAX,无需重新加载。或者,如果您想在不打扰页面的情况下执行此操作,则需要设置target
。
$("form").submit(function(){
$.post($(this).attr("action"), $(this).serialize());
return false;
});
target
<form action="index.php" method="post" role="form" target="_blank">
答案 1 :(得分:1)
使用ajax
HTML
<html>
<head>
</head>
<body>
<form id="myform"><!--changge-->
<input type="email" placeholder="Enter email" name="email_address">
<button class="btn btn-primary custom-button red-btn" id="signup">Sign Up</button>
</form>
</body>
</html>
<script>
$(document).ready(function()
{
$('#signup').click(function()
{
$.ajax({
url:'index.php',
method:'post',
data : $('#myform').serialize(),
success:function()
{
}
)};
);
});
</script>
答案 2 :(得分:0)
你可以试试这个
<html>
<head>
</head>
<body>
<form id="myform" action="index.php"><!--changge-->
<input type="email" placeholder="Enter email" name="email_address">
<button class="btn btn-primary custom-button red-btn" id="signup">
Sign Up</button>
</form>
<script>
$(document).ready(function(){
$('#signup').click(function(){
$.post($(this).attr("action"), $("#myform").serialize(),function(response){
alert(response) // you can get the success response return by php after submission success
});
)};
});