【问题标题】:Changing the value of a Form Submit Button to say submitted after form submits将表单提交按钮的值更改为在表单提交后说提交
【发布时间】:2016-03-06 04:35:37
【问题描述】:

我的网站上有一个联系表。我想在表单成功提交后将提交按钮的文本更改为“已提交”,甚至可以在表单提交时使其显示“正在提交”。我不确定如何执行此操作,我可以执行一个 onclick 事件来更改文本,但不是我想要采取的路线,因为消息可能无法发送并且按钮仍然会说已提交。

这是我的表单html

<form method="post" action="contact.php">
    <input type="text" name="name" placeholder="Name"><br>
    <input type="email" name="email" placeholder="Email"><br>
    <textarea rows="8" cols="65" name="message"placeholder="Message"></textarea><br>
    <input id="submit" type="submit" name="submit" value="Let's Get In Touch">
</form>

这是我的 php 代码:

<?php

    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: Portfolio Website'; 
    $to = 'kyle.a.binger@gmail.com'; 
    $subject = 'Message From Personal Site';

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    if ($_POST['submit']) {
        if (mail ($to, $subject, $body, $from)) { 
            echo '<p>Your message has been sent!</p>';
        } else { 
            echo '<p>Something went wrong, go back and try again!</p>'; 
        }
    }
?>

有没有办法用我现有的 php 代码做到这一点?提前感谢您的帮助。

【问题讨论】:

    标签: php html forms submit


    【解决方案1】:

    您需要将数据传递给 php 脚本,并在不离开页面的情况下返回某些内容/回显某些内容。

    看看 AJAX。你将能够做到这一点。 Here's a link to one of the first posts on stackoverflow that showed up. Here's a link to w3schools to give you a quick example/idea.

    【讨论】:

      【解决方案2】:

      如果您不想使用 AJAX 并且要发布到页面本身,您可以执行以下操作

      <form method="post" action=""> <!-- removed the PHP file name to post to itself -->
          <input type="text" name="name" placeholder="Name"><br>
          <input type="email" name="email" placeholder="Email"><br>
          <textarea rows="8" cols="65" name="message"placeholder="Message">  </textarea><br>
          <?php
              if (isset($_POST['submit'])) {
              echo '<input id="submit" type="button" name="submit" value="Submitted">'; //Changed type submit to button
              } else {
              echo '<input id="submit" type="submit" name="submit" value="Let\'s Get In Touch">';
              }
          ?>
      </form>
      
      <?php
          $name = $_POST['name'];
          $email = $_POST['email'];
          $message = $_POST['message'];
          $from = 'From: Portfolio Website';
          $to = 'kyle.a.binger@gmail.com';
          $subject = 'Message From Personal Site';
      
          $body = "From: $name\n E-Mail: $email\n Message:\n $message";
      
          if ($_POST['submit']) {
              if (mail ($to, $subject, $body, $from)) {
                  echo '<p>Your message has been sent!</p>';
              } else {
                  echo '<p>Something went wrong, go back and try again!</p>';
              }
          }
      ?>
      

      【讨论】:

        猜你喜欢
        • 2015-10-25
        • 1970-01-01
        • 2015-08-19
        • 2011-10-23
        • 1970-01-01
        • 2020-12-03
        • 2016-12-04
        • 1970-01-01
        • 2014-03-23
        相关资源
        最近更新 更多