gpt4 book ai didi

php - undefined variable $_POST ['data' ] PHP/Ajax jquery

转载 作者:行者123 更新时间:2023-12-01 02:45:31 25 4
gpt4 key购买 nike

大家好,这不正常:) !!

foo.php

     <?php 
if (isset($_POST['data']))
$stringData = $_POST['data'];
$file = "ciao.txt";
$fh = fopen($file, 'w') or die("can't open file");
fwrite($fh, $stringData);
fclose($fh);

?>

file.js

function WriteToFile() {
var dataa = "foo bar";
$.post("foo.php", {data: dataa}, function(result){ alert("ciaoooo!!");}
, "json");
}

这是错误,我无法在我的 file.txt 上写入

注意: undefined variable :stringData

我也尝试过这种功能

function WriteToFile() {
var data = "foo bar";
$.ajax({
url: 'foo.php',
type: 'POST',
data: { 'data' : 'data'},
success: function() {
alert('the data was successfully sent to the server');
}
});

but the result is the same!! Anyone have some idea???

最佳答案

您缺少大括号:

if (isset($_POST['data'])) {
$stringData = $_POST['data'];
$file = "ciao.txt";
$fh = fopen($file, 'w') or die("can't open file");
fwrite($fh, $stringData);
fclose($fh);
}

没有它们,你基本上就会得到这个:

 if (isset($_POST['data'])) {
$stringData = $_POST['data'];
}
$file = "ciao.txt";
$fh = fopen($file, 'w') or die("can't open file");
fwrite($fh, $stringData);
fclose($fh);

这解释了为什么您会得到未定义的 $stringData,因为 POST 未正确执行。

请注意,这并不能解决您的 JS/jQuery 问题。为此,请参阅其他答案。

关于php - undefined variable $_POST ['data' ] PHP/Ajax jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11692393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com