我的网站上有联系表格。我想更改提交按钮的文字,说“&34;已提交"表单成功提交后,甚至可以说它提交"提交"表格正在提交。我不确定如何做到这一点,我可以做一个改变文本的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代码执行此操作?提前感谢您的帮助。
答案 0 :(得分:0)
您需要的是将数据传递给php脚本,并在不离开页面的情况下返回某些内容/ echo。
了解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.
答案 1 :(得分: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 = '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>';
}
}
?>