猿问

将ajax variabel传递给其他php页面

这是我的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;

?>


千巷猫影
浏览 128回答 2
2回答

慕码人2483693

您做错了,根据您的源代码,您将 source1 发送到 sendinto.php 并尝试在 ajax.php 中访问。首先,从 ajax.php 中删除以下行<?php&nbsp;&nbsp;session_start();&nbsp;$data = json_decode($_POST['source1']);?>&nbsp;并将其放入 sendinto.php 中,例如:&nbsp;<?php&nbsp;&nbsp; session_start();&nbsp; &nbsp; $data=null;&nbsp; &nbsp;//you first need to check it&nbsp; &nbsp;if(isset($_POST['source1']))&nbsp; &nbsp;$data = json_decode($_POST['source1']);&nbsp;?>&nbsp;

元芳怎么了

您可以将 if 条件用于停止错误if($_POST['source1']){&nbsp; &nbsp;$data = json_decode($_POST['source1']);}else{&nbsp; &nbsp;$data = "";}
随时随地看视频慕课网APP
我要回答