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
21
  • i did't see href attribute in your a tag. Commented Apr 27, 2015 at 3:56
  • Ah, I can comment that code out, it still gives the error Commented Apr 27, 2015 at 4:00
  • weird, can you try replace var id = $(this).data('id'); into var id = $(this).attr('data-id');. Commented Apr 27, 2015 at 4:01
  • 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. Commented Apr 27, 2015 at 4: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. Commented Apr 27, 2015 at 4:11

2 Answers 2

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();
?>
Sign up to request clarification or add additional context in comments.

3 Comments

It says Error: id not exist
I didn't get your code: var id = $(this).data('id'); what do you want from this code ?
So when someone clicks either the image or the name, the id is transferred to the jquery variable id. ( think thats what it means)
0

you are already assuming data is posted. try if isset

if(isset($_POST['id']))
{
     $id = $_POST["id"];
  echo $id;
    }

3 Comments

This also does not return anything
It does not return anything because no data is getting passed. check this line of code var id = $(this).data('id'); instead try adding var id = $(this).data('data-id');
Makes no difference either

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.