-1

This is my ajax.php

I try to pass source1 to other php I got error when I use

$data = json_decode($_POST['source1']); It says Undefined index. What should I do?

<?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">

this is my ajax code

$('#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";
   });
  });

this is my sendinto.php

<?php 

  session_start();
  $coba = $_SESSION['source1'];
  echo $coba;

?>

Misha Akopov
  • 12,241
  • 27
  • 68
  • 82

2 Answers2

0

you are doing it wrong, as per your source code, you are sending source1 to sendinto.php and trying to access in ajax.php. first, remove the following lines from ajax.php

<?php 
 session_start();
 $data = json_decode($_POST['source1']);
?> 

and put it in sendinto.php like:

 <?php 
  session_start();
    $data=null;
   //you first need to check it
   if(isset($_POST['source1']))
   $data = json_decode($_POST['source1']);
 ?> 
Jatin Parmar
  • 2,759
  • 5
  • 20
  • 31
0

you can use if condition for stop error

if($_POST['source1']){
   $data = json_decode($_POST['source1']);
}else{
   $data = "";
}
Darshan Patani
  • 208
  • 1
  • 9