Home > Software design >  How to send a value to php document and redirect to it?
How to send a value to php document and redirect to it?

Time:10-16

I have a button with "orderdetail" class in a html document . I want to get it's id by jquery and send it to the php document then redirect to that php document and then I process that id by POST , but I always get "Notice: Undefined index: id in C:\laragon\www\portal\orderDetail.php on line 9" error. this is my code :

in html doc:

$(document).on('click', '.orderdetail', function () {
    var _id = $(this).attr("id");
    console.log(_id);   //I got the true result here              
    $.post("orderdetail.php", { id: _id }, function (data, status, xhr) {
        window.location.href = "orderdetail.php";
    })
    .... more code not shown

in php doc: echo $_POST['id'];

What's the problem?

CodePudding user response:

You can use jquery.redirect library to send POST data with redirect in the same time.

After importing jquery, add this file: https://github.com/mgalante/jquery.redirect/blob/master/jquery.redirect.js

Then use it in JS as so:

$.redirect('file.php', {'First': 'Hello', 'Second': 'World'});

In your case:

$.redirect('orderdetail.php', {'id': _id});

This POSTs data along with the redirect.

If this answer helped you, please upvote and select it as accepted answer.

  • Related