如何避免在PHP表单提交页面重新加载 [英] How to avoid page reload in php form submission

查看:127
本文介绍了如何避免在PHP表单提交页面重新加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在我的html页面中使用php,并且在表单提交时执行的同一页面中有一个动作。目前整个表单被重新加载,而我希望PHP操作在后台运行,无需重新加载。我该怎么做。点击提交按钮后,我也想清除文本框。以下是我目前使用的代码

I am currently using php in my html page and there is an action in the same page which gets executed upon form submission. Currently whole form gets reloaded while I want the php action to be run in the background with out reloading. How do I do it. I would also want to clear the text box after submit button is clicked. Following is the code I am currently using

<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>

注意:以下代码适用于我

Note: Following code worked for me

<script type="text/javascript">
    $('#contactForm').submit(function () {
       $.post("mailer.php",$("#contactForm").serialize(), function(data){
       });
        return false;
    });
</script>


推荐答案

您需要为此使用AJAX, 。或者,如果您想在不干扰页面的情况下执行此操作,则需要设置 target

You need to use AJAX for this, for without reloading. Or, if you want to do it without disturbing the page, you need to set target.

$("form").submit(function(){
  $.post($(this).attr("action"), $(this).serialize());
  return false;
});



使用目标



Using target

<form action="index.php" method="post" role="form" target="_blank">

这篇关于如何避免在PHP表单提交页面重新加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆