[英]Creating a alert box
我是網頁設計的初學者,我設計了一個聯系頁面,其頁面編碼為:
<div class="col-md-6">
<h4 class="uppercase weight-700">Send us a message</h4>
<form name="contactform" method="post" action="send_form_email.php">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="first_name">Tell us your name</label>
<input type="text" name="first_name" maxlength="50" size="30">
</div>
</div>
</div>
<div class ="row">
<div class="col-md-6">
<div class="form-group">
<label for="email">Tell us your email</label>
<input type="text" name="email" maxlength="80" size="30">
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="telephone">What's your number?</label>
<input type="text" name="telephone" maxlength="30" size="30">
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="comments">Give us the details</label>
<textarea name="comments" maxlength="1000" cols="28" rows="6"></textarea>
</div>
<div class="text-center">
<br/>
<button class="btn btn-primary btn-round" type="submit" onclick="myFunction()">Send Message </button>
</div>
</div>
</div>
</div>
</div>
並且php代碼是(send_form_email.php):
<?php
if(isset($_POST['email'])) {
$email_to = "email@mydomain.com";
$email_subject = "Form Data";
function died($error) {
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();
}
if(!isset($_POST['first_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not 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,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
$string_exp = /^[0-9]+$/';
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The telephone you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do 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 .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
當用戶單擊“提交”按鈕時,他將被重定向到send_form_email.php頁面。 現在,我只希望代替用戶轉到另一個頁面,錯誤消息和成功消息都應顯示在警報框中的同一頁面上。
頁面繪制完成后,請執行以下操作:
<script>
alert(<? echo $error_message ?>);
</script>
如果我將“ $ error_message”放在哪里,則可以在頁面上設置的任何PHP變量都可以使用。
您有兩個選擇。
您可以使表單提交到同一頁面,即將表單操作設置為與表單頁面相同,然后處理表單數據。 一旦出現錯誤,就可以適當顯示它們。 這意味着將html和php放在同一頁面上,這不是最干凈的選擇。
使用ajax使用jQuery提交表單。 由於您剛開始,這可能令人生畏。 盡管這樣做將是一種更干凈,更流暢的方式,因為它不會涉及頁面刷新。
就像這樣:
$.ajax({
type: "POST",
url: url,
data: data,
success: success,
dataType: dataType
});
jQuery並不是那么難找,它將是一個很好的學習工具。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.