如何在表单提交后以可自定义的模式显示PHP if或else


How to display PHP if or else in customizable modal after form submit

我在一个单页网站上有一个可用的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不是很熟练。我从这里的另一个用户那里得到了第二段代码,这对我有帮助。谢谢。

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

页面上的某个位置:

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

然后将包含警报呼叫的线路更改为:

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

你碰巧使用jQuery UI吗?如果你这样做了,你可以通过添加以下行将div变成一个弹出窗口:

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