twitter-bootstrap 将 bootstrap data-id 中的值传递给 php 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23050700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 20:29:22  来源:igfitidea点击:

Passing a value from bootstrap data-id to a php variable

phpjquerytwitter-bootstrapmodal-dialog

提问by user3530310

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

我想将“一月”值传递给一个 php 变量。像这样的东西

<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?

我不知道我怎么能做到这一点?

采纳答案by Darren

To elaborate on my comment previously.

详细说明我之前的评论。

You can use jQuery.ajaxor even jQuery.postto achieve this.

您可以使用jQuery.ajax甚至jQuery.post来实现这一点。

For examples sake, your element has an id of mymonth

例如,您的元素的 id 为 mymonth

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

Now with jQuery you could get the trigger:

现在使用 jQuery 你可以得到触发器:

$(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-idand store it in the valvariable. Then post it off to the example php file : myphpfile.php

如您所见,我们获取属性data-id并将其存储在val变量中。然后将其发布到示例 php 文件中:myphpfile.php

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

myphpfile.php会对你的PHP函数本身(当然为例):

<?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
}

?>