如何在表单提交后显示PHP,或者在可自定义的模式中显示

时间:2014-10-21 13:37:52

标签: php jquery html ajax forms

我在单页网站上有一个工作的php联系表单。当用户单击“提交”时,它们将被带到一个单独的PHP页面,该页面显示IF语句,表明它们正确填写了表单,或者如果页面显示错误,则显示PHP ELSE语句。我想要做的不是打开PHP页面,我宁愿用户看到一个模式,我可以调整以适合我的网站外观/感觉,它显示IF或ELSE语句。这些是我的PHP文件中的语句:

function died($error) {
// your error code can go here 
echo "We are very sorry, but there were error(s) found with the form you submitted. "; 
echo "These errors appear below.<br /><br />"; 
echo $error."<br /><br />"; 
echo "Please go back and fix these errors.<br /><br />";
die();
}

// Validate expected data exists
if(!isset($_POST['visitorname']) || !isset($_POST['company_name']) 
|| !isset($_POST['email']) || !isset($_POST['phone']) || !isset($_POST['subject'])  
||!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}

$visitorname = $_POST['visitorname']; // required 
$company_name = $_POST['company_name']; // not required 
$email_from = $_POST['email']; // required 
$phone = $_POST['phone']; // not required
$subject = $_POST['subject']; // required 
$comments = $_POST['comments']; // required 
$error_message = ""; 
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; 
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
} 
$string_exp = "/^[A-Za-z .'-]+$/"; 
if(!preg_match($string_exp,$visitorname)) {
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/"; 
if(!preg_match($string_exp,$subject)) {
$error_message .= 'The subject you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The message you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($visitorname)."\n";
$email_message .= "Company: ".clean_string($company_name)."\n"; 
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Phone: ".clean_string($phone)."\n";
$email_message .= "Subject: ".clean_string($subject)."\n";
$email_message .= "Message: ".clean_string($comments)."\n";

// This section creates the email headers
$auth = array('host' => $host, 'auth' => true, 'username' => $username, 'password' 
=>  $password);
$headers = array('From' => $from_address, 'To' => $email_to, 'Subject' 
=>   $email_subject, 'Reply-To' => $reply_to);

// This section send the email
$smtp = Mail::factory('smtp', $auth);
$mail = $smtp->send($email_to, $headers, $email_message);

if (PEAR::isError($mail)) {?>
<!-- include your own failure message html here -->
Unfortunately, the message could not be sent at this time. Please try again later.


<!-- <?php echo("<p>". $mail->getMessage()."</p>"); ?> -->

<?php } else { ?>


Thank you for contacting me. I will respond as soon as possible.

<?php } } ?>

以下Jquery / Ajax接近我想要的,因为我可以在不离开页面的情况下提交表单,但我不知道如何让PHP if / else语句出现在警告框中。我也不喜欢警报框的外观,因为我无法设计它。

$ (function() {

$('form.contactform').on('submit',function(){

var element=$(this),
url=element.attr('action'),
type=element.attr('method'),
data={};

element.find('[name]').each(function(index,value){
var element=$(this),
name=element.attr('name');
value=element.val();

data[name]=value;
});

$.ajax({
url:url,
type:type,
data:data,
success:function(response){
console.log(response);
// add your thank you pop up here
alert("Need to call PHP IF/ELSE here somehow.");
}
});
return false;
});

我想要注意的是,我对Jquery / Ajax / PHP并不太方便。我从这里得到了另一位用户的第二位代码帮助了我。感谢。

1 个答案:

答案 0 :(得分:0)

您可以在html页面的任何位置创建一个空div,并在ajax调用返回时通过jquery填充内容。

您网页上的某处:

<div id="contactresponse">
</div>

然后将包含警报调用的行更改为:

$("#contactresponse").text(response.responseText);

你碰巧使用jQuery UI吗?如果这样做,您可以通过添加以下行来将该div转换为弹出窗口:

$("#contactresponse").dialog();