只能发布 while 循环的第一个结果 [英] Can only post first result of while loop

查看:29
本文介绍了只能发布 while 循环的第一个结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 while 循环来显示查询结果.while 循环工作正常.在隐藏字段中,我想将 userID 和 accessID 的值发布到用户详细信息页面.我正在使用 javascript 提交表单以从链接提交.我的问题是,无论我单击什么用户名,我都只能发布第一条显示记录的值.我做错了什么?

I am using a while loop to display results from a query. The while loop is working fine. In hidden fields I would like to post the values of userID and accessID to the user details page. I am submitting the form using javascript to submit from a link. My problem is that regardless of the username I click I can only post the values for the first displayed record. What am I doing wrong?

代码:

<?php
while($row = $result->fetch_array()) { ?>
    <form method="post" action="edit_user.php" id="userForm">
    <tr>
        <td>
            <a href="#" onclick="return(submitForm())"><?php echo $row['firstname'].' '.$row['surname']; ?></a> 
            <input type="hidden" name="userID" value="<?php echo $row['userID']; ?>" />
            <input type="hidden" name="accessID" value="<?php echo $row['accessID']; ?>" />
        </td>
    </tr>
    </form>
<?php } ?>

用于提交表单的javascript:

The javascript used for submitting the form:

function submitForm() {
    var form = document.getElementById("userForm");
    form.submit();
}

谢谢.编辑 - 我不想在 url 中传递值.

Thank you. EDIT - I don't want to pass the values in the url.

推荐答案

因为这条线,你遇到了麻烦

You're running into trouble because of this line

var form = document.getElementById("userForm");

在 Javascript 和 HTML 中,ID 应该是特定 DOM 元素的唯一标识.在这种情况下,您有大量具有相同 ID 的表单标签.您需要为每个表单指定不同的 ID,然后将该 ID 传递给 submitForm 函数.

In Javascript and HTML, an ID is supposed to be unique to a certain DOM element. In this case, you've got a whole load of form tags that have the same ID. You need to give each form a different ID, and then pass that ID to the submitForm function.

例如:

<?php
$id = 0;
while($row = $result->fetch_array()) { ?>
    $id++;
    <form method="post" action="edit_user.php" id="<?php echo "userForm".$id ?>">
    <tr>
        <td>
            <a href="#" onclick="return(submitForm("<?php echo "userForm".$id ?>"))"><?php echo $row['firstname'].' '.$row['surname']; ?></a> 
            <input type="hidden" name="userID" value="<?php echo $row['userID']; ?>" />
            <input type="hidden" name="accessID" value="<?php echo $row['accessID']; ?>" />
        </td>
    </tr>
    </form>
<?php } ?>

然后

function submitForm(id) {
    var form = document.getElementById(id);
    form.submit();
}

edit:我如何 php?:D

edit: how do I php? :D

这篇关于只能发布 while 循环的第一个结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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