Javascript 将id值从ahref传递到引导模式php

Javascript 将id值从ahref传递到引导模式php,javascript,php,jquery,Javascript,Php,Jquery,我有一个从数据库创建列表的php代码。我从数据库中检索到的一项是行id。我使用以下代码创建列表: <th>Cover Name</th> <th>Sum Insured</th> <th>Info</th> <th style="width: 3.5em;"></th> </tr> </thead> <tbody

我有一个从数据库创建列表的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>
如您所见,锚定
#myModal
onclick

单击锚定后,我想将
$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>

×
删除确认

是否确实要删除封面?
此操作无法撤消

取消
我的印象是我需要某种形式的隐藏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 ?
并将其传递到myModal窗口,但我该怎么做?

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

<a href="#myModal" onclick="<?php $coverid = $row['coverid'] ?>" role="button" data-toggle="modal">...</a>
因此,如果您定义JavaScript函数
show_dialog
,它将被调用,其值来自
$row['coverid']
。将变量从PHP传递到JavaScript的唯一方法是在有JavaScript代码的地方从PHP回显它们的值

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

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'; ?>';
window.php_vars=window.php_vars|124;{};
window.php_vars.some_value='';
window.php_vars.some_other_值=“”;
window.php_vars.foo='';
…但这种方法仅适用于标量类型(整数、字符串等)

  • 使用单一模式
  • 使用属性数据id存储封面id:
    数据id=“<?php$coverid=$row['coverid']?>”
  • 将类添加到垃圾箱图标,例如
    class=“垃圾箱”
  • 在模式中为删除按钮添加id,例如
    id=“modalDelete”
  • 在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);
    })
    

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

    谢谢,我现在明白多了。现在在这行中放什么?@kya I更新了响应并添加了第四个代码部分。onclick=“show\u dialog(“”)”这行不通@xe4me您能解释一下为什么吗?如何将变量分配给窗口对象,这是一个“好做法”?你在开玩笑吗?首先,我没有抄你的答案。其次,我的响应与设置href完全一样,而不是输入字段的值。第三,我在JSFIDLE上设置了一个示例,并解释了代码的重要部分。因此,它接受了我的回答。在这个网站上,我有很多答案,所以这应该是为了表明我有一些知识。我也遇到过类似的情况,但我没有抱怨。最后,不管你是否正确,你都有权发表自己的意见。
    <a href="#myModal" onclick="show_dialog(1337)" role="button" data-toggle="modal">...</a>
    
    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'; ?>';
    
    // 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);
    })
    
    <a href="#myModal" onclick="<?php $coverid = $row['coverid'] ?>" role="button" data-toggle="modal">
        <i class="fa fa-trash-o"></i>
    </a>
    
    <a href="#myModal" class="trash" data-id="<?php echo $row['coverid']; ?>" role="button" data-toggle="modal">
        <i class="fa fa-trash-o"></i>
    </a>
    
    <input id="feed_id" value="" />
    
    $(document).ready(function () {
        $('body').on('click', '.trash',function(){
            document.getElementById("feed_id").value = $(this).attr('data-id');
                console.log($(this).attr('data-id'));
            });
        });
    }