gpt4 book ai didi

PHP 仅在成功提交后表示感谢

转载 作者:行者123 更新时间:2023-12-01 08:16:31 24 4
gpt4 key购买 nike

因此,我有一个表单,其中包含用于检查每个字段是否已归档的 javascript 验证和用于停止页面重新加载的 jquery ajax 脚本。

PHP 脚本非常简单,仅通过电子邮件发送表单的字段。问题是,如果每个字段都已填写,我只需要显示感谢消息,而目前,当我输入名称字段时,它似乎会显示它。如果我只是输入电子邮件或消息字段,它不会显示它,而且我不知道为什么(我不擅长 PHP)。

<?php 



$name = $_POST['name'];
$email = $_POST['email'];
$priority = $_POST['priority'];
$message = $_POST['message'];
$telephone = $_POST['telephone'];
$formcontent="From: $name \n Email: $email \n Telephone Number: $telephone \n Priority: $priority \n Message: $message";
$recipient = "myemail@address.co.uk";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");

if(mail($name,$email,$message,$headers)){
echo "<p>Thanks for your mail...</p>";
}


?>

停止页面重新加载的 JavaScript。

<script type="text/javascript">

$(document).ready(function() {

$(".contactus").submit(function() {

$.post("mail.php", $(".contactus").serialize(),

function(data) {
$("#formResponse").html(data);
}
);
return false;
});
});
</script>

表单验证。

<script type="text/javascript" language="JavaScript">


var FormName = "contactus";



var RequiredFields = "name,email,priority,message";



function ValidateRequiredFields()
{
var FieldList = RequiredFields.split(",")
var BadList = new Array();
for(var i = 0; i < FieldList.length; i++) {
var s = eval('document.' + FormName + '.' + FieldList[i] + '.value');
s = StripSpacesFromEnds(s);
if(s.length < 1) { BadList.push(FieldList[i]); }
}
if(BadList.length < 1) { return true; }
var ess = new String();
if(BadList.length > 1) { ess = 's'; }
var message = new String('\n\nThe following field' + ess + ' are required:\n');
for(var i = 0; i < BadList.length; i++) { message += '\n' + BadList[i]; }
alert(message);
return false;
}

function StripSpacesFromEnds(s)
{
while((s.indexOf(' ',0) == 0) && (s.length> 1)) {
s = s.substring(1,s.length);
}
while((s.lastIndexOf(' ') == (s.length - 1)) && (s.length> 1)) {
s = s.substring(0,(s.length - 1));
}
if((s.indexOf(' ',0) == 0) && (s.length == 1)) { s = ''; }
return s;
}
// -->
</script>

以及表格

<form action="mail.php" id="myform" class="contactus" onsubmit="return ValidateRequiredFields(); this.submit(); this.reset(); return false;"   name="contactus" method="POST">
<p class="floatleft" style="width:200px; background-color:#FF0000; height:50px; line-height:50px; margin:0; padding:0;">Nameeeeee</p> <input class="sizetext" type="text" maxlength="10" name="name">
<div style="clear:both"></div>
<p class="floatleft" style="width:200px;">Email</p> <input class="sizetext" type="text" maxlength="10" name="email">
<div style="clear:both"></div>
<p class="floatleft" style="width:200px;">Telephone</p> <input class="sizetext" type="text" maxlength="10" name="telephone">


<p>Priority</p>
<select name="priority" size="1">
<option value="Low">Low</option>
<option value="Normal">Normal</option>
<option value="High">High</option>
<option value="Emergency">Emergency</option>
</select>

</select>
<p>Message</p><textarea name="message" rows="6" cols="25"></textarea><br />

<br />
<input class="buttonstyle" type="submit" value="Send">
</form>

最佳答案

<?php

$name = $_POST['name'];
$email = $_POST['email'];
$priority = $_POST['priority'];
$message = $_POST['message'];
$telephone = $_POST['telephone'];

$check = array('name', 'email', 'priority', 'message', 'telephone');
$send = true;

foreach($check as $key => $value){
if(empty($_POST[$value])){
$send = false;
}
}


$formcontent="From: $name \n Email: $email \n Telephone Number: $telephone \n Priority: $priority \n Message: $message";
$recipient = "myemail@address.co.uk";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";

if($send && mail($recipient, $subject, $formcontent, $mailheader)){
echo "<p>Thanks for your mail...</p>";
}
else{
die('Error!');
}

关于PHP 仅在成功提交后表示感谢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10340858/

24 4 0
文章推荐: jquery - 'one' 和 'live' 的组合用于提交?
文章推荐: php - Jquery 计数器 - 在定期增加的两个变量值之间进行计数
文章推荐: jquery - 使用 jQuery 从 HTML 属性(获取 .attr 作为数组)设置多个