将值从引导数据ID传递到php变量 [英] Passing a value from bootstrap data-id to a php variable
问题描述
我有一个带有数月值的下拉菜单.这是其中之一的示例.
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>
我想将"January"值传递给php变量.像这样
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">×</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>
我不确定该如何实现?
推荐答案
要在之前详细说明我的评论.
To elaborate on my comment previously.
您可以使用 jQuery.ajax 甚至是
You can use jQuery.ajax or even jQuery.post to achieve this.
例如,您的元素的ID为 mymonth
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>
现在使用jQuery,您可以获取触发器:
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);
});
});
如您所见,我们获取属性data-id
并将其存储在val
变量中.然后将其发布到示例php文件:myphpfile.php
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
myphpfile.php
将具有您的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
}
?>
这篇关于将值从引导数据ID传递到php变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!