如何在foreach循环中的隐藏字段中分配唯一值 [英] How to assign unique value in hidden fields in a foreach loop

查看:318
本文介绍了如何在foreach循环中的隐藏字段中分配唯一值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的问题是这样的:
我正在做一个foreach循环,它会生成一个链接列表。每个链接都有JavaScript,它提交表单。然而,我想附加到每个链接一个变量,以便我可以得到$ _POST变量,以查看哪个链接已被点击。



我尝试了一个foreach循环,那么就像:

  foreach($ matches as $ match){
?>
< div class =containerbla>
< form>
< h3>< a href =javascript:{}onclick =document.getElementById('matchesform')。submit(); return false;><?php echo $ match-> ;名称>?< / A>< / H3>
<?php echo $ match-> id; ?>
< input type =hiddenname =matchidvalue =<?php echo $ match-> id;?>>
< / form>
< / div>
<?php
}

它显示正确的用户名单和正确的用户ID在下面。但是,当我点击用户时,我总是将隐藏字段中的最新用户ID提交给我的view-messages php。



我该如何解决这个问题?

解决方案

  foreach($ matches as $ key => $ match){?> 

< div class =containerbla>
< form>
< h3>< a href =javascript:{}onclick =var poop = document.getElementById('matchesform'); poop.insertAdjacentHTML('< input type = \hidden'\\ name = \poop \value = \<?php echo $ key;?> \/>'); poop.submit(); return false;><? php echo $ match-> name?>< / a>< / h3>
<?php echo $ match-> id; ?>
< input type =hiddenname =matchidvalue =<?php echo $ match-> id;?>>
< / form>
< / div>
<?php
>

然后你就可以得到索引使用 $ matches [$ _ POST ['poop']];



已经创建了一个新的输入,但是你没有把它输入正确的格式,这就是为什么它没有被提交。


So my question is this: I am doing a foreach loop, and it generates a link list. Each link is having JavaScript in it, which submits the form. I want to, however, attached to EACH link a variable so that I can get the $_POST variable to see WHICH link has been clicked.

I tried a foreach loop and then something like:

foreach ($matches as $match) {
    ?>
    <div class="containerbla">
        <form>
            <h3><a href="javascript:{}" onclick="document.getElementById('matchesform').submit(); return false;"><?php echo $match->name?></a></h3>
            <?php echo $match->id; ?>
            <input type="hidden" name="matchid" value="<?php echo $match->id; ?>">
        </form>
    </div>
    <?php
}

It shows the correct user name list and the correct user ID's right below. But when I click on the user, I always get the LATEST user Id in the hidden field submitted to my view-messages php.

How can I solve that?

解决方案

foreach ($matches as $key=>$match) { ?>

                <div class="containerbla">
                    <form>
                        <h3><a href="javascript:{}" onclick="var poop = document.getElementById('matchesform'); poop.insertAdjacentHTML('<input type=\"hidden\" name=\"poop\" value=\"<?php echo $key; ?>\"/>'); poop.submit(); return false;"><?php echo $match->name?></a></h3>
                        <?php echo $match->id; ?>
                        <input type="hidden" name="matchid" value="<?php echo $match->id; ?>">
                    </form>
                </div>
            <?php
            }

Then you will be able to get the index using $matches[$_POST['poop']];

I see that you are creating a new input already, but you are not putting it in the correct form, that is why it isn't getting submitted.

这篇关于如何在foreach循环中的隐藏字段中分配唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆