Unable to validate with PHP after jquery validation in an PHP included page -


could please me figure out how form submit php after jquery validation.

i found tutorial "build neat html5 powered contact form" , if build form 1 complete page head , body validates. site designed header , footer same pages , include different content in centre example:

[http://mysite/index.php?page=contact]

this js script:

`$(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 in touch soon.</p>")                   .hide()                   .fadein(1500, function() {                     $('#message').append("<img id='checkmark' src='images/check.png' />");                   });                 }               });               }             return false;              onkeyup: false         }); `  tutorial followed came following submithandler:  ` /*submithandler: function() {             alert("correct captcha!");         },         success: function(label) {             label.addclass("valid").text("valid captcha!")         },*/` 

which commented out , form submits when 1 complete page.

i found above ajaxsubmit in tutorial when click submit navigates index page.

here php file:

` ?>

<?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 not empty     if(empty($name)){         $formok = false;         $errors[] = "you have not entered name";     }         //validate telephone numbers      if(empty($telephone)){         $formok = true;     }      //validate email address not empty     if(empty($email)){         $formok = false;         $errors[] = "you have not entered email address";     //validate email address valid     }      //validate message not empty     if(empty($message)){         $formok = false;         $errors[] = "you have not entered message";     }      //validate captcha not empty     session_start();     if(empty($captcha)){         $formok = false;         $errors[] = "please answer captcha question";     }     elseif(strtoupper($_post['captcha']) == $_session['captcha_id'])     {     //do stuff     unset ($_session["captcha_id"]);     }     else     {     $formok = false;     $errors[] = "wrong code entered";     }      //send email if ok --removed       //what need return our form     $returndata = array(         'posted_form_data' => array(             'name' => $name,             'email' => $email,             'telephone' => $telephone,             'message' => $message         ),         'form_ok' => $formok,         'errors' => $errors     );      //if not 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 form         header('location: ' . $_server['http_referer']);     } }`   

i hope have not put code knowledge of php , jquery limited need solve suggestions appreciated.

thanks in advance, james

don't try building post body hand. there lot of encoding issues be introduced using method.

instead of:

var datastring = 'name='+ name + '&email=' + email + '&telephone=' + telephone + '&message=' + message;  

just

var dataobj = {"name":name, "email":email, "telephone":telephone}; 

then on ajax call change

data: datastring, 

to

data: dataobj; 

this dodge lot of corner cases , encoding issues.


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -