douyuan4697 2013-08-15 15:41
浏览 31

将变量发送到Bootstrap模式

Here is my php function:

 function displayrecords(){
 $sql = "SELECT * FROM mytable";
 $QueryResult = @mysql_query($sql) or die (mysql_error());
 // more code...

At this point, the function prints out the retuned data in a table:

 echo "<table class='table table-striped table-bordered table-hover'>
";
 while(($Row = mysql_fetch_assoc($QueryResult)) !== FALSE) {

And here is where the issue begins:

 echo "<tr><td><a class=\"open-EditRow btn btn-primary btn-mini\" 
   data-toggle=\"modal\" href=\"myEditModal\" id=\"".$Row[pk_tId]."\" 
   title=\"Edit this row\" \">Delete/Edit</a></td>";

There are more printed td tags after this, but the issue is transferring the id to the modal window. I was told this could be done with straight php and without javascript or ajax.

This is what the code looks like in the modal:

 <div class="modal hide fade" id="myEditModal" tabindex="-1" role="dialog" 
  aria-labelleby="myModalLabel" aria-hidden="true">
 <div class="modal-body">
 <?php
   $id = "<input type=\"text\" name=\"groupId\" id=\"groupId\" value=\"\" />";
   $sql = "SELECT resgroup FROM restable WHERE pk_tId = '" . $id . "'";
   $result = mysql_query($sql);

As you will notice, the $sql is reading $id as an input tag, and therefore, it cannot find the id in the database, and thus returns nothing. I know it starts with the anchor tag inside the echoed table above.

I need to utilize a $_GET (or POST) in the anchor tag, but I do not know where to put it. I'm guessing it should go in the id=\"".$Row[pk_tId]."\" , but how when php is already being used in that id?

I am a visual person, so if you could provide an example, I would be grateful.

  • 写回答

1条回答 默认 最新

  • dpl57372 2013-08-15 16:03
    关注

    You will need a bit javascript to manipulate your modal depends on which button you clicked. I think this solution will help you.

    First, I supposed that there is a form inside your modal and you will submit it with your selected id. The modal body should be like this:

    <div class="modal-body">
      <form action="//your/path/to/php" method="post">
        <input type="hidden" name="selected_id" id="selected_id" />
        <input type="submit" name="commit" value="Confirm your action?" />
      </form>
    </div>
    

    Second, I think your should remove the data-toggle property from your buttons, add another data-* property to hold your id and use explicit javascript call to show your modal with a bit update.

    A button should be echo like this

    echo "<tr><td><a class=\"open-EditRow btn btn-primary btn-mini\" data-id=\"".$Row[pk_tId]."\" title=\"Edit this row\" \">Delete/Edit</a></td>";
    

    The javascipt

    $('.open-EditRow').click(function(){
       var selected_id = $(this).attr('data-id');
       $('#myEditModal #selected_id').val(selected_id);
       $('#myEditModal').modal('show');
    });
    

    Now, in the php file declared inside the form in the modal - which receives your post data, you can var_dump the $_POST.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度