javascript - Why is this AJAX form not submitting? -
whenever press "submit request" button within form, no error pops , not redirect me page noted in php script. ideas why occurring?
code webpage w/ form:
<form id="consultation-reservation" action="actions/consultation-request.php" method="post"> <input name="fullname" type="text" placeholder="full name (required)" class="mt5" /> <input name="phonenumber" type="text" placeholder="phone number (required)" class="mt5" /> <input name="emailaddress" type="text" placeholder="e-mail address (required)" class="mt5" /> <textarea name="comments" placeholder="additional comments/questions" class="mt10"></textarea> <p class="hidden" id="consultation-reservation-error">please verify required fields complete.</p> <a class="button" id="consultation-reservation-submit">submit request</a> </form> <script type="text/javascript"> $(document).ready(function(){ $("#consultation-reservation-submit").click(function(){ $("#consultation-reservation").submit(); }); }); $('#consultation-reservation').submit(function(e) { register(); e.preventdefault(); }); function register() { jquery.ajax({ method: "post", url: "actions/consultation-request.php", data: $('#consultation-reservation').serialize(), datatype: "json", success: function(msg){ if(parseint(msg.status)==1) { window.location=msg.txt; } else if(parseint(msg.status)==0) { error(1,msg.txt); } } }); } function hideshow(el,act) { if(act) $('#'+el).hide(0).slidedown(500, 'linear'); else $('#'+el).hide(); } function error(act,txt) { if(txt) $('#consultation-reservation-error').html(txt); $('#consultation-reservation-error').hide(0).slidedown(500, 'linear'); } </script>
php script supposed executed via ajax: (i going apply mysql_real_escape_string variables containing form data, haven't yet)
<?php if(empty($_post['fullname']) || empty($_post['phonenumber']) || empty($_post['emailaddress'])) { die('{status:0,"txt":"please verify required fields complete."}'); } $name = $_post['fullname']; $phonenumber = $_post['phonenumber']; $email = $_post['emailaddress']; $comments = $_post['comments']; if(!(preg_match("/^[\.a-z0-9_\-\+]+[@][a-z0-9_\-]+([.][a-z0-9_\-]+)+[a-z]{1,4}$/", $_post['emailaddress']))) die('{status:0,"txt":"please provide valid e-mail address."}'); echo '{status:1,txt:"consultation-request-successful"}'; ?>
echo '{status:1,txt:"consultation-request-successful"}';
the above invalid json because doesn't use double quotes around txt
, status
key. specified response data type json
, jquery try parse response json, , if fails run error handler (if set) instead of success handler.
so either add double quotes, or build json in way:
$response = array('status' => 1, 'txt' => 'consultation-request-successful'); echo json_encode($response);
building json in way better because of quotes automatically handled, special characters within values escaped necessary.
Comments
Post a Comment