我嘗試通過「jquery $ .post」和「fwrite php」將數據放入js文件中,並將該數據返回到數組中。怎麼做?
這裏的HTML:
<!doctype html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<script>
$(document).ready(function() {
$("#button").click(function() {
if ($("#nameput").val() != "") {
$.post("processing.php", {
putname: $("#nameput").val()
});
var arr = [$.getScript("talk.js")];
alert(arr[0]);
}
})
})
</script>
</head>
<body>
<input type="text" id="nameput" />
<button id="button">send AJAX req</button>
</body>
</html>
這裏是PHP,我命名爲 「processing.php」:
<?php
$file = fopen("talk.js","a");
$text = $_POST["putname"];
fwrite($file,'"'.$text.'",');
fclose($file);
?>
和 「talk.js」 看起來就像這樣:
"a","b","c",
爲什麼我不能將數據從「talk.js」放到「var arr = [$ .getScript(」talk.js「)];」的數組中?「如上面的html文件中所示?
下面是我閱讀評論後嘗試的內容。我改變scirpt到這一點:
<script>
$(document).ready(function() {
$("#button").click(function() {
if ($("#nameput").val() != "") {
$.post("processing.php", {
putname: $("#nameput").val()
}, function() {
$.getScript("talk.js", function(data) {
var arr = data.split(",");
alert(arr[0]);
})
})
}
})
})
</script>
和PHP這個:
<?php
$file = fopen("talk.js","a");
$text = $_POST["putname"];
fwrite($file,$text);
fclose($file);
?>
但它仍然無法正常工作?
我發現它..我把** js **文件改成** txt **,並使用'$(selector).load()'方法使它**溢出**表文本。我認爲$ .getScript僅用於加載腳本。謝謝你們! –