2013-08-05 58 views
2

我不認爲我的標題能夠解決這個問題,但它可能會引起混淆,我不想將標題擴展到多行。如何使用PHP顯示Bootstrap模式?

這裏所說:

我有一個單頁網站,與下面的代碼聯繫方式:

<form class="move-down" name="contactform" method="post" action="contact-form-handler.php"> 
<div class="controls controls-row"> 
<input id="name" name="name" type="text" class="span3" placeholder="Name"> 
<input id="email" name="email" type="email" class="span3" placeholder="Email address"> 
</div> 
<div class="controls"> 
<textarea id="message" name="message" class="span6" placeholder="Your Message" rows="7"></textarea> 
</div> 
<div class="controls pull-right"> 
<button id="contact-submit" type="submit" class="btn btn-default">Send it!</button> 
</div> 
</form> 

正如你可以看到它的作用是調用稱爲外部PHP文件「接觸 - 形式處理程序」,這是如下圖所示:

<?php 
$errors = ''; 
$myemail = '[email protected]';//<-----Put Your email address here. 
if(empty($_POST['name']) || 
    empty($_POST['email']) || 
    empty($_POST['message'])) 
{ 
    $errors .= die(header('Location: #errorModal')); 
} 
$name = $_POST['name']; 
$email_address = $_POST['email']; 
$message = $_POST['message']; 
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", 
$email_address)) 
{ 
    $errors .= die(header('Location: #errorModal')); 
} 

if(empty($errors)) 
{ 
$to = $myemail; 
$email_subject = "Contact form submission: $name"; 
$email_body = "You have received a new message. ". 
" Here are the details:\n Name: $name \n ". 
"Email: $email_address\n Message \n $message"; 
$headers = "From: $myemail\n"; 
$headers .= "Reply-To: $email_address"; 
mail($to,$email_subject,$email_body,$headers); 
//redirect to the 'thank you' modal 
header('Location: #thanksModal'); 
} 
?> 

<?php 
?> 

這一直工作正常,但由於我的新網站是一個網頁,我不想單獨的頁面加載,我也不只是想呼應的黑色T在白色背景上的內線。

所以我的問題是 - 當點擊提交按鈕時,如何顯示Bootstrap模式窗口,從外部文件內部使用php代碼,並且沒有頁面重新加載?

我希望能做到。如果它不能幫助我啓動一個表示錯誤的模式,或者當點擊提交按鈕時表示感謝?

+0

我想你應該使用jQuery AJAX –

回答

2

是的,你可以。既然你不使用面向對象的方法可以做到這一點

1)網頁上提交你會分配一些$mail_send = true;之後電子郵件發送

2)您填寫然後說<?php if ($mail_send) { ?> code for modal here <?php } ?>

3)放棄這一header('Location: #thanksModal');你不需要那個。

$mail_send = true;而不是那個。

就是這樣。