Php 将“表单提交”按钮的值更改为“表单提交后提交”
我的网站上有一张联系表。我想在表单成功提交后,将提交按钮的文本改为“已提交”,甚至在表单提交时将其改为“提交”。我不确定该怎么做,我可以做一个onclick事件来改变文本,但不是我想要的路线,因为消息可能无法发送,按钮仍然会显示submitted 这是我的表单htmlPhp 将“表单提交”按钮的值更改为“表单提交后提交”,php,html,forms,submit,Php,Html,Forms,Submit,我的网站上有一张联系表。我想在表单成功提交后,将提交按钮的文本改为“已提交”,甚至在表单提交时将其改为“提交”。我不确定该怎么做,我可以做一个onclick事件来改变文本,但不是我想要的路线,因为消息可能无法发送,按钮仍然会显示submitted 这是我的表单html <form method="post" action="contact.php"> <input type="text" name="name" placeholder="Name"><br&
<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。你将能够完全做到这一点。
如果您不想使用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>';
}
}
?>