jQuery / PHP POST not working -


everything seems correct, blank values continue inserted database. seems not pick values using $_post. have tried $_get , $_request well, values still don't seem picked up. know blank row getting added database due static "video" text inserted.

html:

<form>     <div class="form-group">         <input type="text" class="form-control" name="firstname" id="firstname"                placeholder="first name">     </div>     <div class="form-group">         <input type="text" class="form-control" name="lastname" id="lastname"                placeholder="last name">     </div>     <div class="form-group">         <input type="text" class="form-control" name="email" id="email"                placeholder="email">     </div>     <button type="submit" id="view" class="btn bds-button btn-block">view     </button> </form> 

javascript:

$(document).ready(function() {  $('form').validate({     rules: {         firstname: {             required: true         },         lastname: {             required: true         },         email: {             required: true,             email: true         }     },     messages: {         firstname: "please enter first name",         lastname: "please enter last name",         email: {             required: "please enter email",             email: "please enter valid email",         }     },     highlight: function(element) {         $(element).closest('.form-group').addclass('has-error');     },     unhighlight: function(element) {         $(element).closest('.form-group').removeclass('has-error');     },     errorelement: "span",     errorclass: 'help-block',     errorplacement: function(error, element) {         if(element.parent('.input-group').length) {             error.insertafter(element.parent());         } else {             error.insertafter(element);         }     },     submithandler: function(form) {          var firstname = $("#firstname").val();         var lastname = $("#lastname").val();         var email = $("#email").val();          $.ajax({             type: "post",             url: "php/post-viewer.php",             data: {'firstname' : firstname, 'lastname' : lastname, 'email' : email},             datatype: 'json',             success : function(text){              }         });      } });  }); 

php:

<?php  $servername = "xxxx"; $username = "xxxx"; $password = "xxxx"; $dbname = "xxxx";  // create connection $conn = new mysqli($servername, $username, $password, $dbname); // check connection if ($conn->connect_error) {     die("connection failed: " . $conn->connect_error); }  $first_name = $_post['firstname']; $last_name = $_post['lastname']; $email = $_post['email'];  $sql = "insert viewers (demo, first_name, last_name, email) values ('video', '$first_name', '$last_name', '$email')";  if ($conn->query($sql) === true) {     echo $first_name;     //echo "new record created successfully"; } else {     echo $first_name;     //echo "error: " . $sql . "<br>" . $conn->error; }  $conn->close();  ?> 

the problem running scripts via ajax when developing loose visiblility on whats happening when things go wrong.

so have come way of getting visiblilty back, there number of methods use, here couple.

write output file containing info

<?php  function dump($str) {     file_put_contents('mydump.txt', $str.php_eol, file_append ); }   $servername = "xxxx"; $username = "xxxx"; $password = "xxxx"; $dbname = "xxxx";  dump($_post);  // create connection $conn = new mysqli($servername, $username, $password, $dbname); // check connection if ($conn->connect_error) {     die("connection failed: " . $conn->connect_error); }  $first_name = $_post['firstname']; $last_name = $_post['lastname']; $email = $_post['email'];  $sql = "insert viewers (demo, first_name, last_name, email) values ('video', '$first_name', '$last_name', '$email')";  dump($sql);  if ($conn->query($sql) === true) {     dump("new record created successfully"); } else {     dump("error: " . $sql . php_eol . $conn->error); } $conn->close(); ?> 

now can check file happened. add using dump() function

generate json object hold info , return ajax call

<?php $response = new stdclass();  $servername = "xxxx"; $username = "xxxx"; $password = "xxxx"; $dbname = "xxxx";  // create connection $conn = new mysqli($servername, $username, $password, $dbname); // check connection if ($conn->connect_error) {     die("connection failed: " . $conn->connect_error); }  $response->dollarpost = $_post;      $first_name = $_post['firstname']; $last_name = $_post['lastname']; $email = $_post['email'];  $sql = "insert viewers (demo, first_name, last_name, email) values ('video', '$first_name', '$last_name', '$email')";  if ($conn->query($sql) === true) {     $response->status = 'ok';     $response->statusmsg = "new record created successfully"; } else {     $response->status = 'failed';     $response->statusmsg = "create failed";     $response->sqlerrmsg = $conn->error;     $response->sqlstatement = $sql; } $conn->close(); echo json_encode($response); ?> 

now when run ajax, @ responce returned

submithandler: function(form) {     $.ajax({         type: "post",         url: "php/post-viewer.php",         data: {'firstname' : $("#firstname").val(),                 'lastname' : $("#lastname").val(),                 'email' : $("#email").val()         },         datatype: 'json',         success : function(response){             if (response.status == 'failed') {                 alert(response.statusmsg + response.sqlerrormsg);             } else {                 alert('yippeee kia mum');             }         }     });  } 

also remember can set breakpoint on if (response.status == 'failed') { , use debugger view data returned in response object. saves having code lots of alerts etc


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -