10

Problem:

I would like to transfer by using jQuery a value in an link-attribute to PHP/SQL query.

HTML code:

<a data-toggle="modal" href="#myModal" id="1"><i class="pull-right icon-eye-open"></i>HTML</a>

PHP code:

<div id="myModal" class="modal hide fade">
    <div class="modal-header">
      <button class="close" data-dismiss="modal">&times;</button>
      <h3>Title</h3>
    </div>
    <div class="modal-body">            
        <?php
            $query = "SELECT * FROM table WHERE id = ID FROM JQUERY HERE";
            $result = mysql_query($query) or die ('Error (' . mysql_errno() . ') ' . mysql_error());
        ?>
    </div>
    <div class="modal-footer">
      <a href="#" class="btn btn-info" data-dismiss="modal" >Close</a>
    </div>
</div>

Scenario:

When the user clicks the link-element that has data-toggle="modal" then jQuery should take the value of the id-attribute (which is 1 in this case) and send it to the SQL-query so that the SQL query would look like:

$query = "SELECT * FROM table WHERE id = 1";

jQuery code:

$("a[data-toggle=modal]").click(function(){
    var essay_id = $(this).attr('id');
    //Find $essay set it to essay_id in PHP
    //Alternatively create a $_SESSION['EID'] here
});

Question:

How do I use jQuery to set a variable ($essay) in PHP? or how can I create a session variable in PHP through jQuery?

kexxcream
  • 5,873
  • 8
  • 43
  • 62

3 Answers3

20

here is the solution ,

<a href="#" id="1" class="push">click</a> 

use a div on your modal body , like this

    <div class="modal-body">  

             <div class="something" style="display:none;">
                    // here you can show your output dynamically 
             </div>
    </div>

now put data into the .something with ajax calling .please check http://api.jquery.com/jQuery.ajax/ to know more about jquery ajax.

   $(function(){

   $('.push').click(function(){
      var essay_id = $(this).attr('id');

       $.ajax({
          type : 'post',
           url : 'your_url.php', // in here you should put your query 
          data :  'post_id='+ essay_id, // here you pass your id via ajax .
                     // in php you should use $_POST['post_id'] to get this value 
       success : function(r)
           {
              // now you can show output in your modal 
              $('#mymodal').show();  // put your modal id 
             $('.something').show().html(r);
           }
    });


});

   });
Tareq Jobayere
  • 709
  • 7
  • 13
  • How does AJAX detect that it should get the ID from a[data-toggle=modal]? Shouldn't there also be a click() function? – kexxcream Jun 12 '12 at 20:12
  • yes, you can use click() . i just gave you idea how to do this thing . – Tareq Jobayere Jun 12 '12 at 20:23
  • Correct this line please data : 'post_id='+eassay_id The right variable is "essay_id". Stackoverflow doesn't allow my to change only one character! @TareqJobayere – George D. Mar 07 '13 at 11:49
8

Final solution

HTML code:

<div id="myModal" class="modal hide fade">
    <div class="modal-header">
      <button class="close" data-dismiss="modal">&times;</button>
      <h3>Title</h3>
    </div>
    <div class="modal-body">            
        <div id="modalContent" style="display:none;">

        </div>
    </div>
    <div class="modal-footer">
      <a href="#" class="btn btn-info" data-dismiss="modal" >Close</a>
    </div>
</div>  

jQuery code:

$("a[data-toggle=modal]").click(function() 
{   
    var essay_id = $(this).attr('id');

    $.ajax({
        cache: false,
        type: 'POST',
        url: 'backend.php',
        data: 'EID='+essay_id,
        success: function(data) 
        {
            $('#myModal').show();
            $('#modalContent').show().html(data);
        }
    });
});
kexxcream
  • 5,873
  • 8
  • 43
  • 62
1
  1. Get ID with jQuery
  2. Pass it to some script via AJAX
  3. Get your results back and do with them what you please

http://api.jquery.com/jQuery.ajax/

Stan
  • 25,744
  • 53
  • 164
  • 242
  • 1
    The SQL query is on the same page and it doesn't reload. I want to dynamically change the content of
    by changing the ID the user press. See updated explanation and jQuery code.
    – kexxcream Jun 12 '12 at 20:01