0

I am new to Ajax. I have here a function that when a button is clicked, you should be redirected to id.php and at the same time pass the value of clicked_id.

Javascript code:

function clicked(clicked_id){
    window.alert("clicked");
    window.alert(clicked_id);

    $.post('id.php',{ID:clicked_id},
        function(data){
            window.alert("here");
            window.location='id.php';
        });
 }

Inside my id.php,

<?php
    $clickedID = $_GET['ID'];

    echo 'here at id.php';
    echo $clickedID;
?>

Now the problem is that ID in id.php cannot be identified.

Please help me. I already tried both $_POST and $_GET.

I believe that the problem here is in the passing of the variable.

4
  • You're accessing $_GET instead of $_POST, but you're making the request with $.post. Commented Apr 5, 2018 at 16:23
  • @4castle I already tried $_POST but still, it wont work Commented Apr 5, 2018 at 16:27
  • Change window.location='id.php'; to window.location.href='id.php'; Commented Apr 5, 2018 at 16:45
  • An AJAX request is meant for backend processing, not opening a page with variables. If you wanted to load a page with GET variables you could remove the POST / GET call and just use window.location='id.php&id='+clicked_id; See the Ajax Introduction for more info Commented Apr 5, 2018 at 17:01

2 Answers 2

1

there is no need for ajax if you wanna pass the id to the id.php after redirection

function clicked(clicked_id){
       window.alert("clicked");
       window.alert(clicked_id);
       window.location='id.php?id=' + clicked_id;
}

in id.php you can get the id like this:

<?php $id = $_GET['id'];
Sign up to request clarification or add additional context in comments.

2 Comments

You are a lifesaver! Thanks a lot. It works now without causing a problem with my buttons.
No problem just please mark the response as solved thanks
0
function buttonClicked() {

            $.ajax({
                url: "id.php", //url for php function
                type: "POST",
                data: {'clicked_id':clicked_id}, // data to sent
                dataType: 'json',
                success: function (data)
                {

                }
            });
        }

And in your id.php file:

$_REQUEST['clicked_id']

3 Comments

use $_POST or $_REQUEST
what is in the data type? I'm sorry. I am just starting to learn Ajax
datatype of data that you are sent

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.