将表单提交按钮的值更改为表单提交后提交


Changing the value of a Form Submit Button to say submitted after form submits

>我的网站上有一个联系表格。我想在表单成功提交后将提交按钮的文本更改为"已提交",甚至可以在表单提交时使其显示"提交"。我不确定如何做到这一点,我可以做一个 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脚本的东西,并在不离开页面的情况下返回一些东西/回显一些东西。

看看 AJAX。你将能够做到这一点。这是关于stackoverflow的第一批帖子之一的链接。这是w3schools的链接,为您提供一个快速的示例/想法。

如果您不想使用 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>';
        }
    }
?>