HI i want simple pass object using ajax to other php page but when trying retrieve its does not fine. Give me Undefined error. check my code to get clarification. See my error screen shot :

enter image description here

Edit.php

$('#regForm').on('submit', function (e)
{
    var serialData = $(this).serialize(), 
        s = location.search.split('='), 
        searchId = s[s.length-1];

    console.log(serialData);
    console.log(searchId);

    $.ajax({
        method:'POST', 
        url:'update.php',
        dataType:'json', 
        data:{data:serialData, id:searchId},
        success:function(jsonObj){
            console.log(jsonObj);
        }
    });
});

update.php

<?php 
    if(isset($_POST['submit'])){
        var_dump($_POST['data']);
        var_dump($_POST['id']);

        exit();

        $phpObj = json_decode($_POST['data']);
        echo json_encode($phpObj);
    } 
?>

2 Answers

-2
Nilesh Daladra On Best Solutions

problem is in your ajax call

$.ajax({
          method:'POST', 
          url:'update.php',
          dataType:'json', 
          data:{data:serialData, id:searchId},
          success:function(jsonObj){
              console.log(jsonObj);
          }
      });

its should be type:'POST' not a method. that's why your data not getting by PHP file there. Your PHP code is ok.

-1
Community On

try it...

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
</head>
<body>
  <form>
    <input name="name"> <br>
    <input name="surname"> <br>
    <input type="submit" name="">
  </form>
  <button>send</button>
</body>
<script type="text/javascript">
    $('button').click(function(){
        var serialData = $('form').serialize(), 
        s = location.search.split('='), 
        searchId = s[s.length-1];

      $.ajax({
          method:'POST', 
          url:'update.php',
          dataType:'json', 
          data:{data:serialData, id:searchId},
          success:function(jsonObj){
              console.log(jsonObj);
          }
      });
    });
</script>
</html>