Javascript 将 ahref 的 id 值传递给引导模式 php

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

Pass id value from ahref to bootstrap modal php

javascriptphpjquery

提问by kya

I have a php code that creates a list from the database. One of the items I retrieve from the database is the row id. I create the list in the following code:

我有一个从数据库创建列表的 php 代码。我从数据库中检索的项目之一是行 ID。我在以下代码中创建列表:

    <th>Cover Name</th>
      <th>Sum Insured</th>
      <th>Info</th>
      <th style="width: 3.5em;"></th>
    </tr>
  </thead>
  <tbody>
  <?php while($row = mysql_fetch_array($query_set)) { ?>
    <tr>

      <td><?php echo $row['cover_name'] ?></td>
      <td><?php echo 'R '.$row['sum_insured'] ?></td>
      <td><?php echo $row['info'] ?></td>
      <td>
          <a href="cover-type.php?id=<?php echo $row['coverid']?>"><i class="fa fa-pencil"></i></a>
         <a href="#myModal" onclick="<?php $coverid = $row['coverid'] ?>" role="button" data-toggle="modal"><i class="fa fa-trash-o"></i></a>
      </td>
    </tr>
    <?php } ?>
      </tbody>
</table>

The syntax for calling #myModal

调用的语法 #myModal

<a href="#myModal" onclick="<?php $coverid = $row['coverid'] ?>" role="button" data-toggle="modal"><i class="fa fa-trash-o"></i></a>

as you can see the anchor #myModaland onclick

正如你所看到的锚点#myModalonclick

After clicking anchor I would like to pass $coveridto the following myModal pop up

单击锚点后,我想传递$coverid到以下 myModal 弹出窗口

    <div class="modal small fade" id="myModal" 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>
            <h3 id="myModalLabel">Delete Confirmation</h3>
        </div>
        <div class="modal-body">
            <p class="error-text"><i class="fa fa-warning modal-icon"></i>Are you sure you want to delete the cover?<br>This cannot be undone.</p>
        </div>
        <div class="modal-footer">
            <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">Cancel</button>
            <a href="delete-cover.php?id=<?php echo $coverid ?>" class="btn btn-danger"  >Delete</a>
        </div>
      </div>
    </div>
</div>

I under the impression that I need to have some form a hidden javascript variable in

我的印象是我需要某种形式的隐藏 javascript 变量

<a href="#myModal" onclick="<?php $coverid = $row['coverid'] ?>" role="button" data-toggle="modal"><i class="fa fa-trash-o"></i></a> and pass it to myModal window, but how do I do that ?

采纳答案by dm4web

  1. uses a single modal
  2. use attribute data-id to store cover id: data-id="< ?php $coverid = $row['coverid'] ?>"
  3. add class to trash icon, for example class="trash"
  4. add id for Delete button in modal, for example id="modalDelete"
  1. 使用单一模态
  2. 使用属性 data-id 来存储封面 ID: data-id="< ?php $coverid = $row['coverid'] ?>"
  3. 将类添加到垃圾桶图标,例如 class="trash"
  4. 例如,在模态中为删除按钮添加 id id="modalDelete"

and in JQ:

在 JQ 中:

// on clik trash icon    
$('.trash').click(function(){
    //get cover id
    var id=$(this).data('id');
    //set href for cancel button
    $('#modallCancel').attr('href','delete-cover.php?id='+id);
})

http://jsfiddle.net/4j59z60e/4/

http://jsfiddle.net/4j59z60e/4/

回答by Andrew Dunai

PHP is executed on server while JavaScript is executed on client. This code

PHP 在服务器上执行,而 JavaScript 在客户端执行。这段代码

<a href="#myModal" onclick="<?php $coverid = $row['coverid'] ?>" role="button" data-toggle="modal">...</a>

will actually transform into

实际上会变成

<a href="#myModal" onclick="" role="button" data-toggle="modal">...</a>

because the PHP part didn't echo anything.

因为 PHP 部分没有回显任何内容。

So, you'll rather have to so something like this:

所以,你宁愿不得不这样做:

<a href="#myModal" onclick="show_dialog('<?php echo $row['coverid']; ?>')" role="button" data-toggle="modal">...</a>

Assuming you have PHP variable $rowdefined and $row['coverid']is, say, 1337, then this HTML will be rendered to browser:

假设您$row定义了PHP 变量并且$row['coverid']是 1337,那么此 HTML 将呈现给浏览器:

<a href="#myModal" onclick="show_dialog(1337)" role="button" data-toggle="modal">...</a>

thus, if you define JavaScript function show_dialog, it will get called with value from $row['coverid']. The only way to pass variables from PHP to JavaScript is to echo their values from PHP in the place where you have JavaScript code.

因此,如果你定义了 JavaScript 函数show_dialog,它会被调用的值为 from $row['coverid']。将变量从 PHP 传递到 JavaScript 的唯一方法是在您拥有 JavaScript 代码的地方从 PHP 回显它们的值。

A good practice for this is to use a <script>block somewhere on the top of your page with following content:

一个好的做法是<script>在页面顶部的某处使用一个包含以下内容的块:

window.php_vars = window.php_vars || {};
window.php_vars.some_value = '<?php echo $some_value; ?>';
window.php_vars.some_other_value = '<?php echo $some_other_value; ?>';
window.php_vars.foo = '<?php echo 'bar'; ?>';

...but this approach works only for scalar types (ints, strings etc).

...但这种方法仅适用于标量类型(整数、字符串等)。