2017-05-06 48 views
0

我創建一個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){ 

} 

}); 

但它不創造該文件像調用後除外。爲什麼?請給我一個解釋如何得到這個代碼的工作;)

感謝, 基督教

+0

請檢查$ tx_hash變量的值,然後檢查路徑是否正確? fopen http://php.net/manual/en/function.fopen.php –

+0

url數據具有'document.getElementById'語句不會被轉義,因此它們的值不會被髮送 – RamRaider

回答

0

嘗試單獨發送數據。我對變量名做了一些修改。請嘗試下面的代碼,

 <?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); 

你的Ajax代碼在這裏

 $.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){ 

      } 
     }); 
+0

好吧,我試試看,謝謝 – modifyer

+0

非常感謝它只是工作! – modifyer

+0

我很高興它的工作。快樂編碼:) – manian

0

你需要逃避的報價實際上包括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) 
    } 

});