0
有人可以幫我弄清楚如何讓我的表單在jQuery驗證後提交給php。在PHP包含頁面的jQuery驗證後無法驗證PHP
我找到了一個教程「構建一個整潔的HTML5動力聯繫表單」,並且如果我構建表單以便它是一個完整的頁面,並且頭部和身體驗證。但是我的網站被設計頁眉和頁腳是所有頁面和我一樣包括中央例如不同的內容:
[HTTP://mysite/index.php頁=接觸]
這是JS腳本:
`$(function(){
$("#refreshimg").click(function(){
$.post('newsession.php');
$("#captchaimage").load('image_req.php');
return false;
});
$("#contact_form").validate({
rules: {
name: {
required: true,
minlength: 2
},
//telephone, email & message rules removed
captcha: {
required: true,
remote: "process.php"
}
},
//messages removed
errorContainer: $('#errors'),
errorLabelContainer: $('#errors ul'),
wrapper: 'li',
var dataString = 'name='+ name + '&email=' + email + '&telephone=' + telephone + '&message=' + message;
//alert (dataString);return false;
submitHandler: function(form) {
$(form).ajaxSubmit({
type: "POST",
url: "pages/processC.php",
data: dataString,
success: function() {
$('#contactForm').html("<div id='message'></div>");
$('#message').html("<h2>Contact Form Submitted!</h2>")
.append("<p>We will be in touch soon.</p>")
.hide()
.fadeIn(1500, function() {
$('#message').append("<img id='checkmark' src='images/check.png' />");
});
}
});
}
return false;
onkeyup: false
}); `
The tutorial I followed came with the following submitHandler:
` /*submitHandler: function() {
alert("Correct captcha!");
},
success: function(label) {
label.addClass("valid").text("Valid captcha!")
},*/`
其中我註釋掉,當它是一個完整的頁面表單提交。
我在另一個教程中找到了上面的ajaxSubmit,但是當我點擊提交時,它會導航到我的索引頁面。
這是我的PHP文件:
` >
<?php
if(isset($_POST)){
//form validation vars
$formok = true;
$p_errors = array();
//sumbission data
$ipaddress = $_SERVER['REMOTE_ADDR'];
$date = date('d/m/Y');
$time = date('H:i:s');
//form data
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$telephone = filter_var($_POST['telephone'], FILTER_SANITIZE_NUMBER_INT);
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
$captcha = filter_var($_POST['captcha'], FILTER_SANITIZE_STRING);
//validate form data
//validate name is not empty
if(empty($name)){
$formok = false;
$errors[] = "You have not entered a name";
}
//validate telephone is numbers
if(empty($telephone)){
$formok = true;
}
//validate email address is not empty
if(empty($email)){
$formok = false;
$errors[] = "You have not entered an email address";
//validate email address is valid
}
//validate message is not empty
if(empty($message)){
$formok = false;
$errors[] = "You have not entered a message";
}
//validate captcha is not empty
session_start();
if(empty($captcha)){
$formok = false;
$errors[] = "Please answer captcha question";
}
elseif(strtoupper($_POST['captcha']) == $_SESSION['captcha_id'])
{
//Do your stuff
unset ($_SESSION["captcha_id"]);
}
else
{
$formok = false;
$errors[] = "Wrong code entered";
}
//send email if all is ok --REMOVED
//what we need to return back to our form
$returndata = array(
'posted_form_data' => array(
'name' => $name,
'email' => $email,
'telephone' => $telephone,
'message' => $message
),
'form_ok' => $formok,
'errors' => $errors
);
//if this is not an ajax request
if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest'){
//set session variables
session_start();
$_SESSION['cf_returndata'] = $returndata;
//redirect back to form
header('location: ' . $_SERVER['HTTP_REFERER']);
}
}`
我希望我沒有把太多的代碼,但我的PHP和jQuery的知識是非常有限的,但我真的需要解決?這樣所以任何建議將非常感激。
由於提前, 詹姆斯
那麼最新的問題?它在哪裏失敗或者哪裏驗證失敗? – immulatin