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 に答える