0

can I pass multiple variable to bootstrap modal?

here my code :

p>Link 1</p>
<a data-toggle="modal" id="1" class="push" href="#popresult">test</a>
<p>&nbsp;</p>
<p>Link 2</p>
<a data-toggle="modal" id="2" class="push" href="#popresult">test</a>

click the links will pop up a bootstrap modal

<div class="modal hide" id="popresult">
  <div class="modal-header">
    <button class="close" data-dismiss="modal">×</button>
    <h3>Modal header</h3>
  </div>
  <div class="modal-body" style="display:none;">
    <p>some content</p>

  </div>
  <div class="modal-footer"> <a href="#" class="btn" data-dismiss="modal">Close</a> <a href="#" class="btn btn-primary" >View full result</a> </div>
</div>

here the ajax code :

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

       $.ajax({
          type : 'post',
           url : 'yourScript.php', // in here you should put your query 
          data :  'post_id='+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 
             $('.modal-body').show().html(r);
           }
      });
   });
});

ajax code will post variable to another php file yourScript.php

<?php
 session_start();
     $_SESSION['myId'] = $_POST['post_id'];
     echo $_SESSION[myId];
?>

how can I pass multiple variables to yourScript.php?

3
  • perhaps you want to pass multiple variables to yourScript.php? Commented Jul 11, 2013 at 5:09
  • no, i want the ajax code get multiple variable when the link clicked Commented Jul 11, 2013 at 5:32
  • @ocit is yourScript.php a function to get data from database? Commented Jun 18, 2014 at 14:21

2 Answers 2

2

You can pass multiple variables like this

var param1 = 2;
var param2 = 5;

var data = 'param1='+param1+'&param2='+param2;

$.ajax({
         type: "POST",
         url: "yourScript.php",
         data: data,
         success: function(html){

         }
       });

SEE THIS EXAMPLE if you want to get more data from <a> tag instead of getting only the id

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

1 Comment

Is an ajax function the best way of getting multiple data? I'm a newbie.
0

easiest way to post multiple parameters would be to set the data element in the ajax call using an object literal

data: {
        'post_id':id,
        'myParam':"myvalue",
        'anotherOne':"again"
}

Then you would be able to access them as $_POST values in php

$post_id = $_POST["post_id"];
$myParam = $_POST["myParam"];
$anotherOne = $_POST["anotherOne"];

2 Comments

the ajax code get "1" from link id property <a data-toggle="modal" id="1" class="push" href="#popresult">test</a> can I put another property on link, so ajax can get multiple values when the link clicked?
yeah... and when building and add it to the data array... I would suggest a data- attribute.

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.