【问题标题】:Passing a value from bootstrap data-id to a php variable将值从引导数据 ID 传递到 php 变量
【发布时间】:2014-05-27 20:36:18
【问题描述】:

我有一个包含几个月值的下拉菜单。这是其中之一的示例。

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

我想将“January”值传递给 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>

我不知道如何才能做到这一点?

【问题讨论】:

  • PHP is a server-side languageHTML is client-side。这意味着您不能将 data-id 从选择中传递给没有 javascript 的 php 变量,即 ajax 请求。
  • 我怎么能用 ajax 请求来做呢?还是在网上任何地方我都可以检查如何做到这一点?
  • @user3530310 搜索框位于页面的右上角。如果您有兴趣,Google 可能还会为您提供大约 8000 万条搜索结果。

标签: php jquery twitter-bootstrap modal-dialog


【解决方案1】:

详细说明我之前的评论。

您可以使用jQuery.ajax 甚至jQuery.post 来实现此目的。

例如,您的元素的 id 为 mymonth

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

现在使用 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);
    });
});

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

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
}

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-19
    • 2012-08-22
    • 1970-01-01
    • 2016-04-14
    • 2020-08-03
    • 2013-01-21
    相关资源
    最近更新 更多