javascript is not sending data through php code -


hi have requirement need execute mysql queries once user confirm ok confirmation box.in case ever data passing insert_details.php not working. 1 more thing bring notice need send data different script , navigate different page.kindly suggest problem?

     <script type="text/javascript">             var r=confirm("this email address exits in our database. want continue?");  if (r==true)  {    var dataobj = {     fname : document.getelementbyid('fname').value,                       phone : document.getelementbyid('phone').value,     pemail : document.getelementbyid('email').value }         var xhr = new xmlhttprequest();       xhr.open("post","insert_details.php", true);       xhr.setrequestheader('content-type', 'application/json; charset=utf-8');       xhr.send(json.stringify(dataobj));  xhr.onreadystatechange = function() {     if (xhr.readystate>3) {         window.location = 'http://localhost/myproject/test.php?eid=<?php echo $primary_email; ?>';     } }  alert("test");      } else   {    window.location = 'http://localhost/myproject/registration_request.php'    }      </script>   

code in insert_details.php

$date = "date:" . date("d/m/y h:i:s") . "\n"; //to today's date , time                           $logfile = "testing";          $fpath ="myproject/";         $filepath = $fpath .$logfile . '-log-' . date('y-m-d') . '.csv';  //path of error log file          $fh1 = fopen($filepath, 'a') or die("can't open file"); //opening error log file          fwrite($fh1, "******************index*******************" . "\n");         fwrite($fh1, $date);    $test=json_decode(file_get_contents('php://input')); fwrite($fh1, $test[0]);  

you not sending named pair. sending value of textbox.

what looks string.

xhr.send("myemail@example.com"); 

second have race condition between ajax call , window.location.href.

you need wait response come before doing redirection.

basic idea:

var dataobj = {     fname : document.getelementbyid('fname').value,                       phone : document.getelementbyid('phone').value,     pemail : document.getelementbyid('email').value } xhr.onreadystatechange = function() {     if (xhr.readystate>=3) {         window.location = 'http://localhost/myproject/test.php?eid=<?php echo $primary_email; ?>';     } } xhr.send(json.stringify(dataobj)); 

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -