javascript - session not being passed to process.php -
i have looked @ similar questions none seems answer looking for. validation script doesnt recognise input fields - session not being passed.
code follows:
var data = {}; $(document).ready(function() { $('input[type="submit"]').on('click', function() { reseterrors(); var url = 'process.php'; $.each($('form input, form select'), function(i, v) { if (v.type !== 'submit') { data[v.name] = v.value; } }); //end each $.ajax({ datatype: 'json', type: 'post', url: url, data: data, success: function(resp) { if (resp === true) { //successful validation $('form').submit(); return false; } else { $.each(resp, function(i, v) { console.log(i + " => " + v); // view in console error messages var msg = '<label class="error" for="'+i+'">'+v+'</label>'; $('input[name="' + + '"], select[name="' + + '"]').addclass('inputtxterror').after(msg); }); var keys = object.keys(resp); $('input[name="'+keys[0]+'"]').focus(); } return false; }, error: function() { console.log('there problem checking fields'); } }); return false; }); }); function reseterrors() { $('form input, form select').removeclass('inputtxterror'); $('label.error').remove(); }
html form:
<form action="process.php" method="post" > <label>first name</label> <input name="first_name" type="text" /> <label>email</label> <input name="email" type="text" /> <input type="submit" value="submit" /> </form> session_start() @ top of page.
process.php code:
session_start(); if(isset($_post)){ if (empty($_post['first_name'])) { $_session['errors']['first_name'] = 'name missing'; } if (empty($_post['email'])) { $_session['errors']['email'] = 'mail missing'; } if(count($_session['errors']) > 0){ //this ajax requests: if(!empty($_server['http_x_requested_with']) && strtolower($_server['http_x_requested_with']) == 'xmlhttprequest') { echo json_encode($_session['errors']); exit; } //this when javascript turned off: echo '<ul>'; foreach($_session['errors'] $key => $value){ echo '<li>' . $value . '</li>'; } echo '</ul>';exit; }else{ //form validation successful - process data here!!!! } }
i new ajax (as can see) appreciated.
you have set array session,
if(isset($_post)){ $_session['errors'] = array(); if(empty($post['first_name'])) { $_session['errors'][] = "first name cannot blank."; } .... }
regards /saq
Comments
Post a Comment