我需要從php文件中獲取字符串有人知道我該怎麼做?使用jQuery將字符串從php發送到js文件
My.php即寫入和讀取一個文件的contenute使用該梅索德
<?php
$dir = '/var/www/devData/test';
// create new directory with 777 permissions if it does not exist yet
// owner will be the user/group the PHP script is run under
if (!file_exists($dir)) {
mkdir ($dir, 0777);
}
if (isset($_POST['data'])) {
$stringData = $_POST['data'];
$file = "/var/www/devData/test/ciao.txt";
$fh = fopen($file, 'r+') or die("can't open file");
fwrite($fh, $stringData);
$theData = fread($fh, filesize($myFile)); //this is the string that i have to pass
fclose($fh);
return $theData;
}
?>
my.js SCRPT得到從我的PHP文件retrive的strng
function addLabelCustom_options() {
var select = document.getElementById('label_custom');
$.get("JS/foo.php", function(result){ alert(result) }, "json");
/* $.ajax({
url:'/var/www/devData/test/ciao.txt',
success: function (data){
//parse ur data
//you can split into lines using data.split('\n')
//use regex functions to effectivley parse
var label_parsed = data.splitCSV();
select.options[0] = new Option("-- Select Label --",0);
var label_sort = new Array();
for (var i=0; i<label_parsed.length-1; i++)
label_sort.push(label_parsed[i][1]);
label_sort.sort();
for (var j=0; j<label_sort.length-1; j++)
select.options[j+1] = new Option(label_sort[j],j+1);
}
});
*/
}
我只需要得到字符串的含義$ theData 看起來好像我不回收任何字符串....
您是否嘗試過向獲取請求添加成功,錯誤和完成以確保您正確獲取數據? – 2012-07-31 10:01:02
是的,即使我發送那樣的回聲$ theData「ciao」警報框仍然是空的 – Edivad 2012-07-31 10:36:07