0

Adapted from this question: Passing Jquery variable to php within a modal

I have it set up, and it should be working, but I am getting the undefined index error.

Inside a while loop:

print '<li>';
        print '<a class="btn btn-default" data-toggle="modal" data-id='.$row['id'].' data-target=".bs-example-modal-lg"><img src="'.$row["image"].'" /></a><br>';
        print '<a class="btn btn-default" data-toggle="modal" data-id='.$row['id'].' data-target=".bs-example-modal-lg"><h4>'.$row['name'].'</h4></a>';
print '</li>';

All of my jquery, including the ajax code:

 $(document).ready(function() {
            $("body").css("display", "none");
            $("body").fadeIn(1000);
            $("a.transition").click(function(event){
                event.preventDefault();
                linkLocation = this.href;
                $("body").fadeOut(1000, redirectPage);      
            });
            function redirectPage() {
                window.location = linkLocation;
            }
        }); 
$(document).on("click", ".btn", function (e) {
            e.preventDefault();
            alert (id);
            var id = $(this).data('id');
            alert (id);
            $("h4.modal-title").text(id);

            //$($(this).attr('href')).modal('show');    
            $.post('food.php',{id:id},function(data){
                alert(data);
                alert (id) // <--THIS ALERT WORKS TOO
            });
        });

The php:

<?php
        $id = $_POST["id"];
        print '<h4>'.$id.'</h4>'

    ?>

I keep getting an index undefined error on $id = $_POST["id"];.

I cant seem to figure out why, I have adapted my code to so many different ways of doing it, but none of them work.

The first alert shows up, but the second does not. The text does get placed within the <div>, however it does not get sent to the variable. All of this is in one file. I have a feeling my ajax code is missing something.

A Var dump shows:

array (size=0)
   empty
Community
  • 1
  • 1
user2883071
  • 960
  • 1
  • 20
  • 50
  • i did't see href attribute in your a tag. – Norlihazmey Ghazali Apr 27 '15 at 03:56
  • Ah, I can comment that code out, it still gives the error – user2883071 Apr 27 '15 at 04:00
  • weird, can you try replace `var id = $(this).data('id');` into `var id = $(this).attr('data-id');`. – Norlihazmey Ghazali Apr 27 '15 at 04:01
  • Shouldn't the first `id` should be in quotes that is sending to the PHP page I think otherwise it is the variable. `{'id'` or look at the data be transmitted is it as expected? – chris85 Apr 27 '15 at 04:02
  • @chris85 I don't think that makes a difference –  Apr 27 '15 at 04:03
  • @NorlihazmeyGhazali I tried it.. still gives the error – user2883071 Apr 27 '15 at 04:04
  • @chris85, I thought so too, but I removed my answer. I would guess you can't use variables in the key since it automatically evaluates that as a string. – Devon Bessemer Apr 27 '15 at 04:07
  • 1
    @Devon You'd have to use square bracket notation for that `obj[id] = id`, otherwise yeah, it doesn't take the variable's value into account. –  Apr 27 '15 at 04:09
  • 1
    What if you try sending the data as `{id: 'test'}` just to test it out? And check the parameters being sent in the network tab. –  Apr 27 '15 at 04:11
  • still returns an error, how do i get to the network tab? im a noob... still learning – user2883071 Apr 27 '15 at 04:14
  • I am sending this to a modal, if it makes a difference, and also all of this is on the same page.. not replicating server side or anything.. all I want to do is transfer the clicked id to the modal so I can run a query on it – user2883071 Apr 27 '15 at 04:20
  • To check the network tab, open up the developer's console by pressing `CTRL` + `SHIFT` + `i` or `F12` and then click on `Network`. Trigger the POST request and it should be logged in there. Click on it. If you are in Chrome, then click on the `Headers` tab and scroll to the bottom, you should see something that says parameters or the like. If you are in Firefox, there's a nice tab that says `Params`, click on that. –  Apr 27 '15 at 04:29
  • Under form data, i see it: `id:test`, I tried it with the actual variable and I got: 1 (which is expected). – user2883071 Apr 27 '15 at 04:39
  • Hmm, so it is being sent. Do you see any further redirects? I think that maybe your server is redirecting the POST, and the request ends up in a GET request. Do a `var_dump` of `$_GET` as the first line in your code and see what you get. –  Apr 27 '15 at 04:42
  • A var dump as `GET` returns the same result as a var dump of `POST`, which is basically an array `(size=0) empty`, this is so frustrating.. been at it for 3 days – user2883071 Apr 27 '15 at 04:44
  • Aaaah... What if you add a '/' at the end of the URL to your file? As in `'food.php/'`, also, try with the absolute path: `'/some_location/food.php'` and `'/some_location/food.php/'` (but using `$_POST`, of course) –  Apr 27 '15 at 04:49
  • Still gives the error.. zzz – user2883071 Apr 27 '15 at 04:51
  • Geez... I've ran out of ideas then, haha. So, the parameters are being sent, I think that then there's something fishy going on in your PHP file (you didn't post the full PHP code, did you?). You could also try using the `$.ajax` method, although I'm not sure that'd make much of a difference. –  Apr 27 '15 at 04:53
  • In my whole code.. theres only 2 php sections.. one which prints out the information (the while loop) and the second section is basically to take in the variable from jquery.. and yeah i tried the ajax method too – user2883071 Apr 27 '15 at 04:56
  • Is there another way of doing this whole thing? – user2883071 Apr 27 '15 at 05:10
  • @user2883071 check out the answer on your other question at http://stackoverflow.com/questions/29881307/passing-jquery-variable-to-php-within-a-modal , it will walk you through the whole process with a working example – Wesley Smith Apr 28 '15 at 11:12

2 Answers2

0

Use the following php code and try again

<?php
    if( isset( $_POST["id"] ) ) 
        $id = $_POST["id"];
    else
        $id = 'Error: id not exist';
    print '<h4>'.$id.'</h4>';
    die();
?>
Prakash
  • 171
  • 7
0

you are already assuming data is posted. try if isset

if(isset($_POST['id']))
{
     $id = $_POST["id"];
  echo $id;
    }
shadab
  • 347
  • 1
  • 10