skip to Main Content

This function will get the value from the input and sent data with ajax to another php file. But I cannot get the result back from php file when I echo and store in ajax success function result.

<script>

$(document).ready(function(){
    $('.edit-button').click(function(){
        let clickpostid = $(this).prev().val();
        console.log(clickpostid);// I can see the input value
        $.ajax({
            url: "post/edit.php",
            method: "POST",
            data: { clickpostid },
            success: function(result){
                alert(result);
                //When I alert the result is blank not null or undefied.
            }
        })
    });
})
    

</script>

Below code will be in another file such as edit.php.

<?php 
include('../db.php');
if(isset($POST_['clickpostid'])){
      $id = $POST_['clickpostid'];
      echo $id;
   
}

?>

2

Answers


  1. Your edit.php should be this:
    You should use $_POST instead of $POST_

    <?php 
    include('../db.php');
    if(isset($_POST['clickpostid'])){
          $id = $_POST['clickpostid'];
          echo $id;
       
    }
    
    ?>
    
    Login or Signup to reply.
  2. Please try this:

    js:

    $(document).ready(function(){
        $('.edit-button').click(function(){
            let clickpostid = $(this).prev().val();
            console.log(clickpostid);// I can see the input value
            $.ajax({
                url : "post/edit.php",
                type: "POST",
                data: { clickpostid: clickpostid },
                success : function (result) {
                   alert(result);
                }
            });
        });
    });
    

    php:

    <?php 
    include('../db.php');
    if(isset($_POST['clickpostid'])){
          $id = $_POST['clickpostid'];
          echo $id;
       
    }
    
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search