PHP:如何在模态 URL 中放置和传递变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27027186/
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-08-25 23:08:50  来源:igfitidea点击:

PHP : How to put and pass variable in modal URL

php

提问by Monk

So, I have a button to give a direct link to modal in same page.

所以,我有一个按钮可以直接链接到同一页面中的模态。

this is the button and url

这是按钮和网址

<a data-toggle="modal" 
   href="main_user.php?user_id=<?php echo $user['user_id']; ?>#myModal" 
   class="btn btn-warning">

( I try to echo the $user_idon before #modal) is it right ?

(我试着$user_id在之前回应#modal)是吗?

and after I click the button, the modal will appear. This is the modal with the form.

单击按钮后,将出现模态。这是带有表单的模态。

<form class="form-login" action="action/doEditUserStatus.php" method="post">
          <div class="login-wrap">
               <div class="changestatus">
                   <p>Banning or Activing User Status</p></div>
                        <div class="btn-group" data-toggle="buttons">
                            <label class="btn btn-success active">
                               <input type="radio" name="options" value="1" autocomplete="off"  checked> Actived
                            </label>
                            <label class="btn btn-primary">
                              <input type="radio" name="options" value="2" autocomplete="off"> Banned
                            </label>
                        </div>
          </div>
           <div class="modal-footer">
                <button data-dismiss="modal" class="btn btn-default" type="button">Cancel</button>
                <button class="btn btn-theme" name="modalSubmit" type="submit">Submit</button>
           </div>
    </form>

And then I try to submit the modal form, but the action cannot read the $user_idwhich I put before #modal.

然后我尝试提交模态表单,但该操作无法读取$user_id我之前放置的#modal.

UPDATE : Table code :

更新:表代码:

So this is my table :

所以这是我的表:

<tr class="success">
   <th class="numeric">ID</th>
   <th class="numeric">E-mail</th>
   <th class="numeric">Name</th>
   <th class="numeric">Phone</th>
   <th class="numeric">Picture</th>
   <th class="numeric">Status</th>
   <th colspan="2" class="numeric">Action</th>
</tr>


<tr>
   <td class="numeric"><?php echo $user['user_id']; ?></td>
   <td class="numeric"><?php echo $user['email']; ?></td>
   <td class="numeric"><?php echo $user['name']; ?></td>
   <td class="numeric"><?php echo $user['phone']; ?></td>
   <td class="numeric"><?php echo $user['picture']; ?></td>
   <td class="numeric">
       <a data-toggle="modal" href="main_user.php?user_id=<?php echo $user['user_id']; ?>#myModal" class="btn btn-warning">
          <span class="glyphicon glyphicon-edit" aria-hidden="true"></span>&nbsp;Change Status
        </a>
   </td>
</tr>

The main problem is : When I click the button, then the modal will appear but it can't get the $user_idfrom that button?

主要问题是:当我单击按钮时,会出现模态但无法$user_id从该按钮获取?

回答by hellcode

Put a hidden input field with the user_id into your form:

将带有 user_id 的隐藏输入字段放入表单中:

<input type="hidden" name="user_id" value="<?php echo $user['user_id']; ?>">


EDIT: If you mean Bootstrap try this:

编辑:如果你的意思是引导程序试试这个:

Link/Button:

链接/按钮:

<a data-toggle="modal" data-userid="<?php echo $user['user_id']; ?>"
   href="main_user.php#myModal" 
   class="btn btn-warning">

Hidden Form Field:

隐藏表单字段:

<input type="hidden" name="user_id" value="">

Javascript:

Javascript:

$('#myModal').on('show.bs.modal', function(e) {
    var userid = $(e.relatedTarget).data('userid');
    $(e.currentTarget).find('input[name="user_id"]').val(userid);
});

See also http://getbootstrap.com/javascript/#modals-related-targetand Passing data to a bootstrap modal

另请参阅http://getbootstrap.com/javascript/#modals-related-target和将数据传递给引导模式

You should have mentioned Bootstrap in your question. PHP is not the appropriate tag.

您应该在问题中提到 Bootstrap。PHP 不是合适的标签。

回答by Brewal

This will work if your link will do a redirection :

如果您的链接将进行重定向,这将起作用:

You will need to add an hidden input within your form with the $_GET['user_id']variable (that contains the value of the parameter in the url) :

您需要使用$_GET['user_id']变量(包含 url 中参数的值)在表单中添加一个隐藏输入:

<input type="hidden" name="user_id" value="<?php echo (int)$_GET['user_id'] ?>" />

but, if I'm right, this link is handle by twitter bootstrap that will prevent its default behaviour. So there is no actual redirection. What you can do, is use a custom data attribute in the link :

但是,如果我是对的,这个链接是由 twitter bootstrap 处理的,这将阻止其默认行为。所以没有实际的重定向。您可以做的是在链接中使用自定义数据属性:

<a data-toggle="modal" 
   data-userid="<?php echo $user['user_id']; ?>"
   href="#myModal" 
   class="btn btn-warning">

And handle this with a bit of javascript, to add the hidden input field to the form :

并使用一些 javascript 处理此问题,将隐藏的输入字段添加到表单中:

$(document).ready(function(){
    $('[data-userid]').click(function(){
        var userid = $(this).data('userid');
        var form = $('form.form-login');
        var input = $('<input type="hidden" name="user_id" value="'+userid+'" />');
        if (form.find('input[name="user_id"]').length) {
            form.find('input[name="user_id"]').val(userid);
        } else {
            form.prepend(input);
        }
    });
});

回答by jerryurenaa

how about this add the id to the data-target

这如何将 id 添加到数据目标

<a href="#"  data-toggle="modal" data-target="#myModal-<?php echo $user['user_id'];?>" class="btn btn-warning btn-lg">click me</a>

now in your modal id add this

现在在你的模态ID中添加这个

myModal-<?php echo $user['user_id'];?>

and you can load the data dynamically base on that id

并且您可以根据该 ID 动态加载数据