【问题标题】:how to send hidden details when click on image in while loop在while循环中单击图像时如何发送隐藏的详细信息
【发布时间】:2017-08-18 07:54:48
【问题描述】:

这是我的代码

while ($row = $result->fetch_assoc())
{
$imgpath=$row['image_name'];
$id=$row['id'];
<input type="hidden" name="Id" value="' . $row['Id'] . '" >

这里当我点击发送 ID 到下一页时

<img src="'.$imgpath.'" height="170" width="600" title="album-name" class="img-responsive" alt=" " />
}

我可以使用表格吗?

【问题讨论】:

  • 你可以使用Javascript
  • 您要发送的隐藏字段是什么?你想把它寄到哪里?
  • @HamzaAbdaoui 先生再看一遍我刚刚更新..我想将其发送到下一页,例如 text.php 之类的任何页面

标签: php hidden-field


【解决方案1】:

jQuery AJAX 将完成这项工作。您只需要将 click 事件的事件处理函数附加到图像。我们不需要hidden element,我们将$row['id'] 附加到&lt;img&gt; 元素的id 属性,然后在点击时,我们通过Ajax 发布它。

试试这个:

//First, make the id attribute of the <img> equal to $id($row['id'])
<img src="'.$imgpath.'" id="$id" height="170" width="600" title="album-name" class="img-responsive" alt=" " />

然后,添加事件处理程序:

$('body').on('click','img',function(){
  var id = $(this).attr('id');// Get the id
  $.post( "target.php", {id : id}, function( data ) {//Post the id to the target page
     //Do whatever..
  });
});

【讨论】:

    【解决方案2】:

    试试这个:

    <a href="pageName.php"><img src="'.$imgpath.'" height="170" id=id="requestthis" width="600" title="album-name" class="img-responsive" alt=" " /></a>
    

    &lt;input type="hidden" id="hidden_user_id"&gt;

    你可以像这样使用JS:

    <script type="text/javascript">
    $(function() { //ready function
        $('#requestthis').on('click', function(e){ //click event
            e.preventDefault(); //prevent the click from redirecting you to "submitrequest.php"
    
            var hidden_id = $('#hidden_user_id').val();
            var url = "submitrequest.php?hidden_id="+hidden_id;
    
            window.location.replace(url);
            //OR 
            //window.location.href = url;
        })
    })
    

    【讨论】:

    • eddy 先生,但我希望这个 $id 是隐藏的。顶部可见的查询字符串
    • @krish 那又怎样?我希望您不要认为通过尝试“隐藏”此类 ID 会以任何方式提高安全性……因为那将是完全无稽之谈。
    • @CBroe 有些人愿意隐藏他们的变量,所以我们必须按照他们说的去做..
    【解决方案3】:

    您可以使用 .base64_encode 这将使 id 安全:

    <a href="pageName.php?id='.base64_encode($id)'"><img src="'.$imgpath.'" height="170" width="600" title="album-name" class="img-responsive" alt=" " /></a>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-05
      • 1970-01-01
      相关资源
      最近更新 更多