Skip to content Skip to sidebar Skip to footer

Can Only Post First Result Of While Loop

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

Solution 1:

you are generating multiple <form>s inside loop, move your <form> outside while loop, like:

<form method="post" action="edit_user.php" id="userForm">
<?php
while($row = $result->fetch_array()) { ?>       
    <tr>
        <td>
            <a href="#"><?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>        
<?php } ?>
<a href="#" onclick="return submitForm();">Submit</a>
</form>

Solution 2:

You're running into trouble because of this line

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

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.

For example:

<?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 } ?>

and then

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

edit: how do I php? :D


Post a Comment for "Can Only Post First Result Of While Loop"