简体   繁体   English

避免在提交时打开mail.php

[英]avoid opening mail.php on submit

When the submit button is clicked, how do I avoid the browser opening my1.php file? 单击提交按钮后,如何避免浏览器打开my1.php文件?

   <form action="mail/my1.php" method="POST">
            <label for="name">Name</label>
            <input type="text" name="name"><br /><br />
            <input type="submit" name="submit" value="Submit">
           </form>

If you dont want to use the iframe, use the header function to redirect the user ... he will be redirected to my1.php and then thrown to another page, he wont even recognize it. 如果您不想使用iframe,请使用header函数来重定向用户...他将被重定向到my1.php,然后抛出到另一个页面,他甚至不会识别它。 And if you dont use exit; 如果您不使用exit; after the header, the script will still be executed. 标头之后,脚本仍将执行。

header("location: anotherpage.php");

iFrame solution is cleaner and better. iFrame解决方案越来越好。 I just want to offer you another possibility. 我只想为您提供另一种可能性。 Especially because the html-markup can be modified which makes your website vulnerable. 特别是因为可以修改html-markup,这会使您的网站容易受到攻击。

You have plenty of options, here there are ordered by personal preference: 您有很多选择,这里是根据个人喜好排序的:

  1. Add a PHP redirect with header() in mail/my1.php mail/my1.php添加带有header()的PHP重定向
  2. Use AJAX, jQuery makes it rather convenient 使用AJAX,jQuery使它相当方便
  3. Use a frame or an iframe*, not my favorite 使用框架或iframe *,而不是我的最爱

* Nothing to do with Apple. *与苹果无关。

you can submit your page in iframe if you don't want to go on submit page as below 如果您不想进入以下提交页面,则可以在iframe中提交页面

   <form action="mail/my1.php" method="POST" target="my_iframe">
                <label for="name">Name</label>
                <input type="text" name="name"><br /><br />
                <input type="submit" name="submit" value="Submit">
      </form>

    <iframe id="myiframe" name="myiframe"></iframe>

The above will submit you form in iframe. 以上内容将在iframe中提交您的表单。

UPDATE 2: 更新2:

if you don't want to submit the page and do other things like JS/Jquery submit you can simply add onsubmit="return false" in you from tag 如果您不想提交页面并执行其他操作(例如JS / Jquery提交),则只需在标签中添加onsubmit="return false"

<form action="mail/my1.php" method="POST" onsubmit="return false;">
    <label for="name">Name</label>
    <input type="text" name="name"><br /><br />
    <input type="submit" name="submit" value="Submit" id="submit_form">
 </form>

UPDATE 3: 更新3:

if you want to disable your button after click on submit button use this js function 如果要在单击提交按钮后禁用按钮,请使用此js函数

document.getElementsById("submit_form").onclick = function () {
    this.disabled = true;
};

remove the action from the form tag. 从表单标签中删除操作。 The data will not get redirected. 数据将不会被重定向。 If you still want to fetch the values on post use $_REQUEST or $_POST to get values on submit. 如果您仍想在发布时获取值,请使用$ _REQUEST或$ _POST来获取提交值。

YOu can do it as : 你可以这样做:

if($_POST['submit'])
{
   $a=$_REQUEST['name'];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM