gbestard
gbestard

Reputation: 1177

Passing a php variable to a modal using AJAX

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.

Upvotes: 0

Views: 990

Answers (1)

Timmetje
Timmetje

Reputation: 7694

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.

Upvotes: 1

Related Questions