-2

I realize there have been other answers to similar questions before, however I'm trying to make a collection of links unique in a table and can't seem to get the id of the tag to post to a php page.

See below:

            echo("<p>To reserve a book, click the Book Title.</p>");
            echo "<table class='formfield' border='1'>
            <tr>
            <th>Book ID</th>
            <th>ISBN</th>
            <th>Book Title</th>
            <th>Author</th>
            <th>Edition</th>
            <th>Year</th>
            <th>Category</th>
            <th>Reserved</th>
            </tr>";

            $lnkCount = 0;

            while($row = mysqli_fetch_array($sql)) {
                $lnkCount++;
                echo "<tr>";
                echo "<td>" . $row['bookID'] . "</td>";
                echo "<td>" . $row['ISBN'] . "</td>";
                echo "<td><a class='anchor' id='$lnkCount' href='reservation.php'>" . $row['BookTitle'] . "</a></td>";
                echo "<td>" . $row['Author'] . "</td>";
                echo "<td>" . $row['Edition'] . "</td>";
                echo "<td>" . $row['Year'] . "</td>";
                echo "<td>" . $row['CategoryDept'] . "</td>";
                echo "<td>" . ($row['Reserved'] ? 'Yes' : 'No') . "</td>";
                /*echo "<td><input id='$lnkCount' type='button' value='Reserve Now' onClick='post();'></td>";*/ // display yes/no rather than boolean equivalent...
            }
            echo "</table>";

Then my jQuery:

       <script lang="JavaScript" type="text/javascript">


            $(".anchor").click(function() {

                var clkID = $(this).attr("id");
                alert(clkID);

                $.post('reservation.php'), {clkID:postid}, function(){/*do something*/};

           });

        </script>

And finally my php page, which isn't really relevant but I'll post for clarity in my question.

            $id = $_POST['postid'];

            echo("<p>Value detected was: $id</p>"); // this is just to test...

Now when I click on one of the links, the page alerts with the correct id of the link I clicked. But then when it connects to the php page (reservations.php) it gives me the following error:

Notice: Undefined index: postid in C:\xampp\htdocs\webDevProj\reservation.php on line 41 Value detected was:

As I'm sure >=1 of you will know I'm not very experienced with jQuery - so if you understand my problem and think there's a much easier way to do it I would really appreciate your input!

p.s. it's for a college assignment :)

EDIT #1 -- Don't see how this question has any relevance to the one reported as answered. It involves jQuery .post methods. The "solution" provided gives various definitions to what "undefined index" means in regards to php but does still not have any significance on my question.

EDIT #2 -- So I changed the variable and the key around in my JS code without any change. I was hopeful for that split second lol - guess I'll keep looking...

  • 3
    in PHP use:- $id = $_POST['clkID']; – Manoj Singh Nov 27 '18 at 13:46
  • 1
    is this not a typo issue? Should be VTC, no? – treyBake Nov 27 '18 at 13:51
  • 1
    Check your usage of `$.post()`. You're only passing one argument to the function. – Patrick Q Nov 27 '18 at 14:07
  • Patrick Q - yeah added a function '.done' still no joy! Aren't many of the arguments non-mandatory? I mean all I'm looking for is a post request and nothing in return like insert to the HTML or anything so I didn't see the point – shaka lakaboom Nov 27 '18 at 14:25
  • @shakalakaboom Please take a second and actually look at the code that you have. Then look at how the function is supposed to be called. How are you expecting your postid to be posted, if you're not passing it to the function? – Patrick Q Nov 27 '18 at 14:29
  • Not sure, I guess that's why I posted up here on stack exchange in the first place. Seems like you have a solution to my problem and don't want to share... – shaka lakaboom Nov 27 '18 at 14:36
  • @shakalakaboom Since this was marked as a duplicate, no more answers can be posted. However, compare this example, from [the documentation](https://api.jquery.com/jquery.post/) `$.post( "test.php", { name: "John", time: "2pm" } );` to what you have `$.post('reservation.php'), {clkID:postid}`. Notice the difference? – Patrick Q Nov 27 '18 at 15:00

5 Answers5

2

Add this changes to your PHP script

From:

$id = $_POST['postid'];

To:

$id = $_POST['clkID'];

Or edit JS request:

$.post('reservation.php'), {postid:clkID}, function(){/*do something*/};
  • Hi, thanks for the quick reply. I realize I made an error there but unfortunately the incorrect key doesn't seem to be the issue. Indeed that is probably incorrect because I've playing around with this damn thing for so long !! – shaka lakaboom Nov 27 '18 at 14:03
1

You're looking for the wrong key in $_POST. In your AJAX call you set the parameter to have the name clkId. So:

$id = $_POST['postid'];

should be

$id = $_POST['clkId'];
Mitya
  • 33,629
  • 9
  • 60
  • 107
  • The change should actually be done in JS, since the value is assigned to the `clkID` variable. Changing the PHP will remove the Notice, but there won't be any value there. – Patrick Q Nov 27 '18 at 14:00
  • Yup, changed them around in the JS rather, and still no success! Same error "undefined index: postid" – shaka lakaboom Nov 27 '18 at 14:04
1

In your javascript you are storing the retrieved value in clkID, so your jQuery needs to be

$.post('reservation.php'), {postid:clkID}, function(){/*do something*/};

which will pass that variable to PHP with the name postid.

Brenda
  • 273
  • 4
  • 12
0

Problem with your variables name.

Please write like below code:

In JS:

var clkID = $(this).attr("id");    
$.post('reservation.php'), {postid:clkID}, function(){/*do something*/};

Because your data are stored in clkID. Ant then call in controller.

$id = $_POST['postid'];

If you changed variable in PHP script then you can get empty data because there is no data defined in postid in javascript.

Madhuri Patel
  • 1,270
  • 12
  • 24
0

$.post( "reservation.php", { postid: clkID }) .done(function( data ) { alert( "Data Loaded: " + data ); });

And in your php page : $id = $_POST['postid']; return $id; // just to test to see if you get this in data of done function

{ postid: clkID } - This means name of variable should comes first and then you can send value of your variable.

https://api.jquery.com/jquery.post/ // for your reference

Heena Manglani
  • 356
  • 2
  • 11