1

I have a dropdown menu with several months values. This is an example of one of them.

<li data-toggle="modal" data-id="January" class="random " href="#AddMonth">January</li>

I would like to pass the "January" value to a php variable. Something like this

<div class="modal fade" id="AddMonth" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h4 class="modal-title" id="myModalLabel">Update your information</h4>
  </div>
  <div class="modal-body">
      <?php
         // $month  = ? // month will contain the variable that was pass from data-id
         MethodSendMonthData($month);

       ?>
  </div>
</div>

I am not sure how could I achieve this?

amphetamachine
  • 27,620
  • 12
  • 60
  • 72
user3530310
  • 13
  • 1
  • 1
  • 3
  • 3
    `PHP is a server-side language` while `HTML is client-side`. Meaning you can't pass the `data-id` from the selection to a php variable without the means of javascript, namely an ajax request. – Darren Apr 14 '14 at 01:47
  • How could I do it with an ajax request? or is it anywhere online I could check on how to do it? – user3530310 Apr 14 '14 at 02:05
  • 1
    @user3530310 The search box is at the top right of the page. Google may also have about 80 million search results for you, if you are interested. – Sverri M. Olsen Apr 14 '14 at 02:09

1 Answers1

1

To elaborate on my comment previously.

You can use jQuery.ajax or even jQuery.post to achieve this.

For examples sake, your element has an id of mymonth

<li id="mymonth" data-toggle="modal" data-id="January" class="random " href="#AddMonth">January</li>

Now with jQuery you could get the trigger:

$(document).on('click', 'li#mymonth', function(){
    // get month
    var val = $(this).attr('data-id');

    $.post('myphpfile.php', {month: val}, function(data){
        console.log(data);
    });
});

As you can see, we grab the attribute data-id and store it in the val variable. Then post it off to the example php file : myphpfile.php

The myphpfile.php would have your php function as such (example of course):

<?php 

if(isset($_POST['month']) && !empty($_POST['month'])) {
    // do your sanatizing and such....
    // do your php stuff
    MethodSendMonthData($month);

   // you can echo back what you need to and use that in jquery as seen above
}

?>
Darren
  • 13,050
  • 4
  • 41
  • 79