PHP感谢您成功提交

时间:2012-04-26 20:20:38

标签: php jquery forms

所以我有一个表单,同时用于检查每个字段的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>

3 个答案:

答案 0 :(得分:2)

<?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!');
}

答案 1 :(得分:1)

嗯,最简单的方法是检查你的POST字段是否存在而不是空的。如果您想单独检查每个字段,可能会有一个更优雅的解决方案,但这里有:

 if (isset($_POST['name']) && !empty($_POST['name']) && isset($_POST['email']) && !empty($_POST['email']) && isset($_POST['message']) && !empty($_POST['message'])) {
    if(mail($name,$email,$message,$headers)){
echo "<p>Thanks for your mail...</p>";
     }

}

答案 2 :(得分:0)

你需要包装你的代码

if(isset($_POST['submit']))
{
    $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 = @mail($recipient, $subject, $formcontent, $mailheader);

    if($mail){
        echo "<p>Thanks for your mail...</p>";
    }
    else
    {
        echo "Error Sending Mail " ;
    }

}