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


How to prevent a form action opening a new tab with the linked php script?

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

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

输入的文本将写入带有注册.php脚本的文本文档

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

一切正常。但是每当单击提交按钮时,都会打开一个新的空白选项卡,指向 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());

如我所见,您指向带有脚本的空白 php 页面,这就是为什么按提交后会出现空白页的原因。 第二件事是你的if陈述是错误的。

最适合您的是将所有内容放在名为注册的同一页面中.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());
                }
?>

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

我希望这对你有所帮助。