1

i need help because i'm stuck and don't know what's wrong ,i try to send user clicked button "id" to php to get related data from database in the same page

$(".button_class").on("click", function() {
        ToEditId = $(this).attr('id');

        console.log(ToEditId ); //to check clicked id is Ok

      $.ajax({
            type: "POST",
            url: same/php/page/path,
            data: { 
                ToEditId: ToEditId 
            },
            success: function(res, data) {
                console.log(res, data);
            },
            error: function(err) {
                alert(err);
            }

        });
    });

the ajax print success in console log ,here is php code to get the value if clicked id

<?php
if(isset($_POST['ToEditId'])){
    $to_edit_id=$_POST['ToEditId'];
    var_dump($to_edit_id);
}

but nothing happen in php file !!

4
  • You mean nothing happens when you do a POST request manually? Commented Feb 21, 2018 at 22:05
  • try echo $to_edit_id; in php file, so in your console you can see data Commented Feb 21, 2018 at 22:09
  • i mean , it suppose when jQuery ajax send the id by POST to the server same php file it should do what inside if (isset($_POST['ToEditId'])... Commented Feb 21, 2018 at 22:10
  • Your code JS look OK. Check the ajax url of php. Commented Feb 22, 2018 at 2:52

1 Answer 1

1

Which is the expected behaviour.

PHP is not dynamic. It doesn't "update". PHP only runs once. This means that once your page is rendered, you cannot use PHP to change it again. You actually would have to use javascript to change the page, like so;

PHP side:

<?php
  if(isset($_POST['ToEditId'])){
    echo $_POST['ToEditId'];
    $to_edit_id=$_POST['ToEditId'];
    var_dump($to_edit_id);
    die(); // prevent entire page from re-rendering again.
  }

JS side:

$(".button_class").on("click", function() {
    ToEditId = $(this).attr('id');

    console.log(ToEditId ); //to check clicked id is Ok

  $.ajax({
        type: "POST",
        url: same/php/page/path,
        data: { 
            ToEditId: ToEditId 
        },
       success: function(res, data) {
          //Add your PHP file's response to the body through javascript.
          $('body').append(res);
        },
        error: function(err) {
            alert(err);
        }

    });
});

As @IncredibleHat mentioned, you should make sure your page doesn't render any of its usual HTML, so it won't return the entire page back to your ajax call. So put the PHP all the way above your html!

Sign up to request clarification or add additional context in comments.

4 Comments

Unless he had his php block on the bottom of the script ;) As I should say, its important to note the php portion needs to be well ahead of any html output (if ajax is calling the same php).
Agreed, I'll add that.
thanks bro, it's clear now ,please modify your code and put body betwenn "" like $('body').append(res)
@IBRAHIMEZZAT oops, my bad. I fixed it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.