Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么请求不';不创建文件?_Javascript_Php_Jquery_Ajax - Fatal编程技术网

Javascript 为什么请求不';不创建文件?

Javascript 为什么请求不';不创建文件?,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我创建了一个php文件submit_request.php,其中包含以下代码: $tx_hash = $_POST['tx_hash']; $home-address = $_POST['home-address']; $email = $_POST['email']; $file = fopen($tx_hash, 'w'); fwrite($file, $home-address); fwrite($file, $email); fwrite($file, $tx_hash); fcl

我创建了一个php文件submit_request.php,其中包含以下代码:

$tx_hash =  $_POST['tx_hash'];
$home-address =  $_POST['home-address'];
$email =  $_POST['email'];
$file = fopen($tx_hash, 'w');
fwrite($file, $home-address);
fwrite($file, $email);
fwrite($file, $tx_hash);
fclose($file);
因此,在我的index.html文件中,根据以下代码调用此文件:

$.ajax ({
type: 'POST',
url: 'submit_request.php?tx_hash=document.getElementById("tx-
hash").value&home-address=document.getElementById("home-
address").value&email=document.getElementById("email").value',
success: function(data){

}

});
但它不会像调用excepted后那样创建文件。为什么?请给我解释一下如何让这段代码工作;)

谢谢,
Christian

您需要对引号进行转义,以实际包含HTML元素的值,否则您将使用
document.getElementById(…)
发送错误的字符串作为请求的一部分

$.ajax ({
    type: 'POST',
    url: 'submit_request.php?tx_hash='+document.getElementById("tx-hash").value+'&home-address='+document.getElementById("home-address").value+'&email='+document.getElementById("email").value,
    success: function(data){
        alert(data)
    }

});

尝试单独发送数据。我对变量名做了一些更改。请尝试下面的代码

     <?php
       $tx_hash =  $_POST['tx_hash'];
       $home_address =  $_POST['home_address'];
       $email =  $_POST['email'];
       $file = fopen($tx_hash, 'w');
       fwrite($file, $home_address);
       fwrite($file, $email);
       fwrite($file, $tx_hash);
       fclose($file);

请检查$tx_散列变量的值,然后检查路径是否正确?fopen url数据中有
document.getElementById
语句没有转义,因此它们的值没有被识别。我只是检查了您发布的问题。如果有人帮了忙&如果你认为它对你有用,那么请随意接受答案,这样它将有助于其他so用户将来的参考。这就是它的工作原理。请查收,
       $.ajax ({
          type: 'POST',
          url: 'submit_request.php',
          data: {
             tx_hash:document.getElementById("tx_hash").value,
             home_address:document.getElementById("home_address").value,
             email:document.getElementById("email").value
           },
          success: function(data){

          }
       });