0

I'm trying to pass a variable to my modal using AJAX but it's not working.

PHP code

echo '<img src="./images/see.png" class="open-modalPropietari" data-target="#modalPropietari" data-id="' . $id . '" >';

JS code

$(document).on('click', '.open-modalPropietari', function() {
                var id = $(this).data('id');
                $.ajax({
                    type: "POST",
                    url: "propietaris.php",
                    data: {id: id},
                    success: function() {
                        $('#modalPropietari').modal('show');
                    }
                });
            });

it does execute the success (after 6-8 seconds) but it doesn't pass the variable.

I've looked at other posts on this forum but I can't find the solution.

Im pretty new at web development so I've no clue what's wrong.

5
  • What variable are you referring to? Commented Nov 5, 2013 at 15:08
  • what is the actual html markup of ` data-id="' . $id . '" >` when you view the source of the page in the console. Is it valid? When you inspect the post request what do you see being sent to the server? Commented Nov 5, 2013 at 15:09
  • What is the markup of the img tag when the page renders? In what way does this fail? You're not "passing a variable using AJAX" (which is an odd mix of terminology here), you're just rendering a page with a variable, sending that value back to the server, and then showing a modal (which has nothing to do with the variable). When you debug this, where does it fail? Commented Nov 5, 2013 at 15:11
  • console.log(id) before $.ajax call, and var_dump($_POST) on server side for debug Commented Nov 5, 2013 at 15:13
  • the var_dump returns "array(0) { } ", and i tried to see the id before the ajax call, and it's showing the variable Commented Nov 5, 2013 at 15:28

1 Answer 1

1

EDIT: It seems you wrote your question wrong so Ill try helping to debug:

First replace your $.ajax with this

  $.post( "propietaris.php", { id: id })
  .done(function( data ) {
    console.log(data);
  }).fail(function() {
    console.log("error");
  });

And var_dump($_POST); in your propietaris.php

When post is succesfull your console will show the post result page. See if it contains your postdata.

Sign up to request clarification or add additional context in comments.

7 Comments

actually it's echo '<img src="./images/see.png" class="open-modalPropietari" data-target="#modalPropietari" data-id="' . $id . '" >';
var_dump still "array(0) { } "
nvm: The vardump array must be seen AFTER post, Now you probably see it before POST. So do a var_dump($_POST);. Change console.log("succes") to console.log(data) This will show your return page after post in console.
yeah including your vardump somewhere...what does it say? If it contains your postvar, it worked.
Good ;) So youve succesfully sent the id from php->html from html->php ;)
|

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.