2011-07-24 38 views
1

我有一個聯繫表> www.bgv.co.za/testspace/contact_3.php 它使用jQuery驗證和PHP。我有一些添加/刪除類位來隱藏或顯示謝謝標題和一些PHP如果isset回顯成功的表單提交輸入。Jquery PHP表單提交 - 如何獲取表單輸入以顯示?

我的問題是,在提交你看到的感謝頁面,但DIV>聖人沒有顯示用戶輸入... INFACT它不是作爲顯示一個div中的數據 - 請幫助

這裏是我的jQuery的:

$(document).ready(function(){ 
$('#contactform').validate({ 
showErrors: function(errorMap, errorList) { 
    //restore the normal look 
    $('#contactform div.xrequired').removeClass('xrequired').addClass('_required'); 
    //stop if everything is ok 
    if (errorList.length == 0) return; 
    //Iterate over the errors 
    for(var i = 0;i < errorList.length; i++) 
    $(errorList[i].element).parent().removeClass('_required').addClass('xrequired'); 
}, 
submitHandler: function(form) {    
    $('h1.success_').removeClass('success_').addClass('success_form'); 
    $("#content").empty(); 
    $("#content").append('#sadhu'); 
    $('#contactform').hide(); 
    var usr = document.getElementById('contactname').value; 
    var eml = document.getElementById('email').value; 
    var msg = document.getElementById('message').value; 
    document.getElementById('out').innerHTML = usr + " " + eml + msg; 
    document.getElementById('out').style.display = "block"; 
    form.submit(); 
} 
}); 
}); 

這是我的PHP:

$subject = "Website Contact Form Enquiry"; 

//If the form is submitted 
if(isset($_POST['submit'])) { 

//Check to make sure that the name field is not empty 
if(trim($_POST['contactname']) == '') { 
    $hasError = true; 
} else { 
    $name = trim($_POST['contactname']); 
} 

//Check to make sure sure that a valid email address is submitted 
if(trim($_POST['email']) == '') { 
    $hasError = true; 
} else if (!eregi("^[A-Z0-9._%-][email protected][A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) { 
    $hasError = true; 
} else { 
    $email = trim($_POST['email']); 
} 

//Check to make sure comments were entered 
if(trim($_POST['message']) == '') { 
    $hasError = true; 
} else { 
    if(function_exists('stripslashes')) { 
     $comments = stripslashes(trim($_POST['message'])); 
    } else { 
     $comments = trim($_POST['message']); 
    } 
} 

//If there is no error, send the email 
if(!isset($hasError)) { 
    $emailTo = '[email protected]'; //Put your own email address here 
    $body = "Name: $name \n\nEmail: $email \n\nComments:\n $comments"; 
    $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email; 

    mail($emailTo, $subject, $body, $headers); 
    $emailSent = true; 

} 
} 

這裏是我的表格:

  <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactform" > 
     <div class="_required"><p class="label_left">Name*</p><input type="text" size="50" name="contactname" id="contactname" value="" class="required" /></div><br/><br/> 
     <div class="_required"><p class="label_left">E-mail address*</p><input type="text" size="50" name="email" id="email" value="" class="required email" /></div><br/><br/> 
     <p class="label_left">Message</p><textarea rows="5" cols="50" name="message" id="message" class="required"></textarea><br/> 
     <input type="submit" value="submit" name="submit" id="submit" /> 
     </form> 

回答

0

<div id='sadhu'>沒有在任何地方定義。你用Javascript代碼將它追加到#content,但它不存在。

$("#content").append('#sadhu'); 

您可能要像

$("#content").append("<div id='sadhu'>stuff in here...</div>"); 

你已經把表單提交到的東西用id out。如果這東西也應該在<div id='sadhu'>,那麼你可以放置的out.html()進去:

document.getElementById('out').innerHTML = usr + " " + eml + msg; 
document.getElementById('out').style.display = "block"; 
$("#content").append("<div id='sadhu'>" + $("#out").html() + "</div>"); 
+0

右感謝邁克爾....但繼承人的事情的東西之間應該什麼是在表單中輸入由用戶。我應該如何做到這一點 - 至今我已將它作爲php echo變量。 – brett