homepage Welcome to WebmasterWorld Guest from 54.163.72.86
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
posting a value in a while loop is in a form
Orangutang




msg:4269479
 3:40 pm on Feb 19, 2011 (gmt 0)

Hi,

I've had this problem for the last few months and I thought I'd found a solution but unfortunately not. I've stripped it down to its bare bones to have a look.

The code below displays the 3 records in the db table with a standard select and while loop. I need to be able to update each row independently so I added a submit button and enclosed it all in a form.

<?php
// connect to db
$req_comm = $_POST['reqcomm'];

if ($_POST['authbox']) {
$auth_array = isset($_POST['authbox']) && is_array($_POST['authbox'])? $_POST['authbox']: array();
foreach($auth_array as $req_id => $key) {
$query = "UPDATE reqs SET status = 'authorised', reqcomm = '$req_comm' WHERE reqid = '$req_id'";
$result = mysql_query ($query) or die (mysql_error());
}
}
$firstquery = "SELECT * FROM `reqs` WHERE `username` = 'test' AND `status` = 'pending' ";
$first = mysql_query($firstquery) or die(mysql_error());
?>
<hr>

<form method="POST" action="">
<?php
while ($row = mysql_fetch_array($first)) { // START WHILE
$req_id = $row[0]; $req_comm = $row[17];
?>

<table>
<tr align='left'>
<td><?php echo $req_id;?></td>
<td><input type="submit" name="authbox[<?php echo $req_id; ?>]" value="Send"></td>
<td><input type ="text" size="20" name="reqcomm" value="<?php echo $req_comm; ?>"></td>
</tr>
</table>

<?php }; ?> // END WHILE
<hr>
</form> <!-- end form tag here - but only sends $req_comm on the bottom row of whats displayed? -->

//////////////////////////////////////////////////////

The above displays the 3 records:

Reqid 55 | Send | input box
Reqid 60 | Send | input box
Reqid 65 | Send | input box

And heres the problem:

If I fill in the reqcomm input field on Reqid 65 and click Send, it all works, reqcomm is picked up by the post superglobal and used in the update statement but if I try to do it on Reqid 55 or 60 it doesn't, the reqcomm field in the db is not updated and I need it to work irrespective of which req is sent first?

What I'm trying to do seems simple but I've tried swapping and moving everything around and tried posting an array instead of just name="reqcomm" but I can't get it working.

I'm wondering if it's something more serious, something to do with the whole structure lay out?

Any help or pointers would be great as I really haven't got a clue whats happening here or what else to try?

 

rainborick




msg:4269514
 6:15 pm on Feb 19, 2011 (gmt 0)

You're creating a <input type="submit"> button for each pass of your 'while' loop. If you only want to submit one value for each instance of <input name='reqcomm'>, you should move the <form> and </form> tags within that same loop.

If you want to be able to submit multiple values within a single form, you need to change the "name='reqcomm'" attribute to a unique value for each pass through the loop. I find it's easiest to add a counter to the loop and append the counter value to the attribute name in a way that I can easily parse it in the script that processes the form data. Good luck!

Orangutang




msg:4269591
 10:01 pm on Feb 19, 2011 (gmt 0)

Hi rainborick,

Thanks for the post, your first paragraph answers my question and your second paragraph outlines what my end goal is. I think I was having trouble working out what I needed to do :-)

I've added the counter in this test script and in my application and it all works as it should. I've posted it below in case it helps anyone else.

$rc_num = 1;
while ($row = mysql_fetch_array($first) and ($rc_num > 0)) {
$req_id = $row[0];
?>
<form method="POST" action="postupdate.php">
<table>
<tr align='left'>
<td><?php echo $req_id;?></td>
<td><input type="submit" name="authbox[<?php echo $req_id; ?>]" value="Send"></td>
<td><input type ="text" size="20" name="reqcomm[<?php echo $rc_num; ?>]" value=""></td>
</tr>
</table>
</form>
<?php
$rc_num++;
}

And pick up the reqcomm array and process it like the authbox array in the first post.

///////////////////////////////////

Just out of curiosity what would you do if you had more than one value to process. Would you create a counter for each and just keep adding them on like below?

$rc_num = 1;
$rd_num = 1;
$re_num = 1;
while ($row = mysql_fetch_array($first) and ($rc_num > 0) and ($rd_num > 0) and ($re_num > 0)) {
code...
<td><input type ="text" size="20" name="reqcomm[<?php echo $rc_num; ?>]" value=""></td>
<td><input type ="text" size="20" name="reqrd[<?php echo $rd_num; ?>]" value=""></t
<td><input type ="text" size="20" name="reqre[<?php echo $re_num; ?>]" value=""></td>
code...
$rc_num++;
$rd_num++;
$re_num++;
}

rainborick




msg:4269661
 2:42 am on Feb 20, 2011 (gmt 0)

Yes, I just structure the name attribute so that I can easily detect and group them as needed.

Orangutang




msg:4269735
 10:20 am on Feb 20, 2011 (gmt 0)

That's great, thanks for your help. :-)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved