这是我的ajax.php我尝试将 source1 传递给其他 php 使用时出现错误$data = json_decode($_POST['source1']); 它说Undefined index。我该怎么办?<?php session_start();$data = json_decode($_POST['source1']); ?> <!DOCTYPE html><html><head><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <title></title></head><!-- <form action="sendinto.php" method="post"><center><button id="btnSelectedRows" type="button">test send</button></center></form> --><center> <button id="btnSelectedRows" >test</button> <!-- input type="submit" name="upload" value="Compare"> --> </center><body></body></html><script type="text/javascript">这是我的ajax代码$('#btnSelectedRows').on('click', function() { $.post({ type: "POST", url: 'sendinto.php', datatype : 'text', data: {source1 : "heyo"} , // or GET}).done(function(data) { //Redirect to another page if you want... window.location.href = "sendinto.php"; }); });这是我的sendinto.php<?php session_start(); $coba = $_SESSION['source1']; echo $coba;?>
2 回答

慕码人2483693
TA贡献1860条经验 获得超9个赞
您做错了,根据您的源代码,您将 source1 发送到 sendinto.php 并尝试在 ajax.php 中访问。首先,从 ajax.php 中删除以下行
<?php
session_start();
$data = json_decode($_POST['source1']);
?>
并将其放入 sendinto.php 中,例如:
<?php
session_start();
$data=null;
//you first need to check it
if(isset($_POST['source1']))
$data = json_decode($_POST['source1']);
?>

元芳怎么了
TA贡献1798条经验 获得超7个赞
您可以将 if 条件用于停止错误
if($_POST['source1']){
$data = json_decode($_POST['source1']);
}else{
$data = "";
}
- 2 回答
- 0 关注
- 127 浏览
添加回答
举报
0/150
提交
取消