0

嗨,我有一个要求,一旦用户从确认框中确认 Ok,我需要执行 mysql 查询。在我的情况下,我传递给 insert_details.php 的任何数据都不起作用。我想提醒您的另一件事是,我需要将数据发送到不同的脚本并将其导航到不同的页面。请建议问题出在哪里?

     <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>  

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]); 
4

1 回答 1

2

您没有发送命名对。您只是发送文本框的值。

它看起来像一个字符串。

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

其次,Ajax 调用和 window.location.href 之间存在竞争条件。

在进行重定向之前,您需要等待响应返回。

基本思路:

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));
于 2013-04-01T13:03:27.240 回答