gpt4 book ai didi

php - 通过 php 从 html 发送邮件将返回到 php 页面。它应该在同一个联系表中。怎么做?

转载 作者:行者123 更新时间:2023-11-27 22:49:29 25 4
gpt4 key购买 nike

我有一个包含所有图像和其他文本的 html 页面。下面我有联系表格。我是 PHP 的新手。我用php来发送邮件。但是当我点击发送按钮时,它会转到 backend.php。我有一个空白页。我需要的是它应该在同一页面中。我做了一些 javascript 来显示和隐藏 div。有了这个,我会在同一页面上显示感谢消息。但我不想重定向浏览器以显示它将转到 backend.php。要在同一页面中,我已将 php 重定向到另一个窗口。

我的代码是 -后端.php

<?php
if($_POST['message'])
{
$body = "Name: ".$_POST['name'];
$body .= "<br>Email: ".$_POST['email'];
$body .= "<br>Phone/Mobile: ".$_POST['phone']
$body .= "<br>Address: ".$_POST['address']
$body .= "<br>Message: ".$_POST['msg']
if(mail("Info@travelfoodservices.com","Subjects",$body))
echo 'true';
else echo 'false;';
}
?>

表单代码-

<form onsubmit="return validateFormOnSubmit(this)"  action="backend.php" method="post" target="_blank">
<table align="left">
<tbody>
<tr>
<td align="left"><label for="name">Enter your name:</label> <label style="color:red;display:none;font-size:18px;" Id="namestar">*</label></td>

</tr>
<tr><td align="left"><input Id="name" name="name" style="width:515px;" size="35" maxlength="50" type="text"></td></tr>
<tr ><td height="12px"/></tr>
<tr>
<td align="left"><label for="email">Email Address:</label> <label style="color:red;display:none;font-size:18px;" Id="emailstar">*</label></td>
</tr>
<tr><td align="left"><input Id="email" name="email" style="width:515px;" type="text"></td></tr>
<tr><td height="12px"/></tr>
<tr>
<td align="left"><label for="phone">Phone/Mobile:</label><label style="color:red;font-size:18px;display:none;" Id="phonestar">*</label></td>
</tr>
<tr><td align="left"><input Id="phone" name="phone" style="width:515px;" type="text"></td></tr>

<tr><td height="12px"/></tr>
<tr>
<td align="left"><label for="address">Your Address:</label><label style="color:red;font-size:18px;display:none;" Id="addressstar">*</label></td>
</tr>
<tr><td align="left"><input id="address" name="address" style="width:515px;" type="text"></td></tr>
<tr><td height="12px"/></tr>
<tr>
<td align="left"><label for="msg">Enter Your Comments / Suggestions / Enquiries</label><label style="color:red;font-size:18px;display:none;" Id="messagestar">*</label></td>
</tr>
<tr><td align="left"><textarea id="msg" name="msg" style="width:515px;height:100px;" type="text"></textarea></td></tr>
<tr><td height="12px"/></tr>
<tr>

<td align="right"><table align="left">
<tbody>
<tr >
<td style="width:160px;"/>
<td style="width:250px;" ><label Id="mandatory" style="display:none;color:red;" >* Mandatory fields</label></td>
<td ><input type="submit" name="send" value="send" /></td><td >
<input type="reset" name="reset" value="reset" onclick="resetAll()" /></td></tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>

最佳答案

我会将 backend.php 脚本移动到表单所在页面的顶部。为此,表单脚本(您所指的“HTML 页面”)需要是一个 .php 页面,而不是比 .html 页面。我在这里假设您将其称为 form.php:

<?php
$submitted = false;

if($_POST['msg']) {
$body = "Name: " . $_POST['name'] .
"<br>Email: " . $_POST['email'] .
"<br>Phone/Mobile: " . $_POST['phone'] .
"<br>Address: " . $_POST['address'] .
"<br>Message: " . $_POST['msg'];

// Set the $submitted variable to true. This will let you keep the PHP logic at the top of the page and simply check the value of the variable later to determine whether or not to show a success message.
if(mail("Info@travelfoodservices.com","Subjects",$body)) $submitted = true;
}
?>


<?php
// Somewhere later on in the page, but above the form, show a success message if the $submitted variable has been set to true.
if($submitted) echo '<div>The form has been submitted!</div>';
?>

<form action="form.php" method="post">
<!--All the HTML form code goes here-->
</form>

关于php - 通过 php 从 html 发送邮件将返回到 php 页面。它应该在同一个联系表中。怎么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4930545/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com