0

Hi i have a requirement where i need to execute mysql queries once user will confirm Ok from confirmation box.In my case what ever data i am passing to insert_details.php is not working. one more thing i would like to bring to your notice that i need to send data to different script and navigate it to different page.kindly suggest where is the problem?

     <script type="text/javascript">
            var r=confirm("This email address already exits in our database. Do you want to 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 get today's date and 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 the error log file 
        fwrite($fh1, "******************index*******************" . "\n");
        fwrite($fh1, $date); 


$test=json_decode(file_get_contents('php://input'));
fwrite($fh1, $test[0]); 
5
  • How are you accessing it in PHP? Since you're using JSON data, you can't use $_POST variables inside PHP. You'd have to use json_decode(file_get_contents('php://input')); Commented Apr 1, 2013 at 12:53
  • yeah i am using the same line of code in insert_details.php page. but it is not working Commented Apr 1, 2013 at 12:59
  • "it is not working" is an extremely vague description. What happens when you var_dump(file_get_contents('php://input'));? Commented Apr 1, 2013 at 13:03
  • Sorry Colin. i put it as //$str_json = file_get_contents('php://input'); then i am trying to this $email = json_decode($_POST['pemail']); mysql_query("UPDATE P_P SET First_Name='$fname', Updated_By='user',Updated_Date=Now() WHERE Email_Address_Primary='$email'"); is it fine? Commented Apr 1, 2013 at 13:10
  • No, not fine. By posting the data as JSON, you can no longer use $_POST. $_POST is auto-parsed by PHP when PHP sees an incoming request with an application/x-www-form-urlencoded or multipart/form-data value for the Content-Type header. Follow up with epascarello below Commented Apr 1, 2013 at 13:12

1 Answer 1

2

You are not sending up a named pair. You are just sending up the value of the textbox.

what is looks like as a string.

xhr.send("[email protected]");

Second you have a race condition between the Ajax call and the window.location.href.

You need to wait for the response to come back before doing the 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));
Sign up to request clarification or add additional context in comments.

11 Comments

If he's using json_decode with php://input the named-pair theory should be a non-issue. But hah, I didn't even pay attention to the window.location assignment.
Then what should be the proper way of doing this?How to send it as named pair? i am novice in this. kindly help.
Thank you Epascarello. do i need to use this also xhr.open("POST","insert_details.php", true); where i need to send dataObj?
Yes, you still need all of that, I just put in the code that needed to change.
Still it is not working:( i have reposted my code in my question. it is neither going to insert_details.php nor to test.php page
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.