2016-03-06 180 views
0

我在我的网站上有联系表单。我希望在提交表单成功提交后,将提交按钮的文本更改为“已提交”,甚至可以在提交表单时声明“提交”。我不确定如何做到这一点,我可以做一个onclick事件,这将改变文本,但不是我想要采取的路由,因为消息可能无法发送,并且按钮仍然会提交。更改表单提交按钮的值以在表单提交后提交

这里是我的形式

<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代码的html:

<?php 

    $name = $_POST['name']; 
    $email = $_POST['email']; 
    $message = $_POST['message']; 
    $from = 'From: Portfolio Website'; 
    $to = '[email protected]'; 
    $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代码做到这一点?预先感谢您的帮助。

回答

0

如果你不希望使用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 = '[email protected]'; 
    $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>'; 
     } 
    } 
?>