如何防止表单操作使用链接的php脚本打开新选项卡?

如何防止表单操作使用链接的php脚本打开新选项卡?,php,forms,action,Php,Forms,Action,我在我的网站上有一个带有提交按钮的简单输入表单 <form action="signup.php" method="post" name="form"> <div class="input"> <input type="text" class="button" id="email" name="email" placeholder="NAME@EXAMPLE.COM"> <input t

我在我的网站上有一个带有提交按钮的简单输入表单

  <form action="signup.php" method="post" name="form">
      <div class="input">
             <input type="text" class="button" id="email" name="email" placeholder="NAME@EXAMPLE.COM">
             <input type="submit" name="submit" class="button" id="submit" value="submit">
     </div>
     </form>

输入的文本将使用signup.php脚本写入文本文档

    <?php
            if(isset($_POST['submit']))
    {
             $email = $_POST['email'];
            $file = fopen("signup.txt","a+");
            fwrite($file,$email);
            fclose($file);
            print_r(error_get_last());
    }
    ?>


一切正常。但是,每当单击submit按钮时,php脚本就会打开一个新的空白选项卡。我如何才能摆脱这种情况,并用某种反馈替换输入表单?

您可以在?>下的.php文件中使用html,它将呈现它。尝试:

<p>Thank you for subscribing</p>
感谢您的订阅

你应该升级它以匹配你在第一页上的漂亮设计

编辑:

你也可以这样做

if(error_get_last() == null) echo "<p>Thanks for subscribing</p>";
else print_r(error_get_last());
if(error_get_last()==null)echo“感谢订阅”

”; else print_r(error_get_last());
如我所见,您使用脚本指向空白的php页面,这就是为什么按submit后会出现空白页面。 第二件事是你的if陈述是错误的

最好是将所有内容都放在同一个名为signup.php的页面中

我会这样做:

<form action="signup.php" method="post" name="submit">
      <div class="input">
             <input type="text" class="button" id="email" name="email" placeholder="NAME@EXAMPLE.COM">
             <input type="submit" name="submit" class="button" id="submit" value="submit">
     </div>
     </form>

<?php
            if(isset($_POST['email']))
    {
             $email = $_POST['email'];
            $file = fopen("signup.txt","a+");
            fwrite($file,$email);
            fclose($file);
            if(error_get_last() == null) echo "<p>Your signup was recorded</p>";
            else print_r(error_get_last());
                }
?>

您可以向该段落添加任何类,使其看起来像您想要的那样,并定位在您想要的位置


我希望这对您有所帮助。

是打开了一个新的选项卡,还是在发布后同一个选项卡变为空白?在没有target=“\u blank”的情况下,它不应该打开新选项卡。页面上有Javascript吗?我在这里看不到任何可以这样做的东西。在我的浏览器中没有打开一个新的选项卡。signup.php中有html吗?非常棒的网站顺便说一句!同一选项卡将变为空白。我不希望发生任何事情,除了在原始页面上显示用户反馈。我不确定我是否理解。将html添加到php脚本中,并将其放入index.html文件中?然后如何将表单放置在我的页面上?@Tobi如果您的文件名为index.html,请将其重命名为index.php。html仍然可以工作,并将我编写的php脚本添加到您的页面中,并将表单操作更改为index.php。因为提交后它会再次降落在那里。但有一个问题,为什么要将其保存到.txt文件中。使用数据库要容易得多。只要弄明白这一点。谢谢我用这个来自学。考虑从txt开始,然后转到数据库。我只是想先把它做好。