multiple dynamic input boxes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
br5dy
Forum Newbie
Posts: 22
Joined: Sat Jul 23, 2005 2:52 pm

multiple dynamic input boxes

Post by br5dy »

i'm stuck on this one.

i have a page with a bunch of users, and service hours. the table looks like:

-----------------------------------------------
Name | 1 | 2 | 3 | 4 | 5 | 6 |
-----------------------------------------------

and all the information is under it. each hour is given an inputbox with it's name of "sh1_user1", "sh2_user1", "sh3_user1"

however, when i go to submit it i can't figure out what to do. how can i save each inputbox to each user, while getting the variable that was generated at the last page? ah!

what i mean is...

the name of the inputbox depends on the users in the database. but how i can access that variable for a specific inputbox if i can't get the actual variable name?

thanks :D

any suggestions?
smo
Forum Newbie
Posts: 20
Joined: Tue May 31, 2005 11:02 pm

Post by smo »

Your user must be authenticating itself before using the page( by logging in I mean ) then you can use the session userid and define the input fields accordingly. Example
name=sh3_$session[userid]
This way your input boxes can be named.

I am not sure what is your requirement but this is not a practice.

Your input boxs should have common names and should go to the field of the table along with the user id of the man entering ( another field). So the combination of user id and the input field will be unique for any further processing.

HTH
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post by harrisonad »

I suggest that you add a id column in your table that will be used to identify each user. Make it the primary key.

Code: Select all

id INT(4) NOT NULL AUTO_INCREMENT
Then everytime you retrieve the list of users, get the each id and append it to the inputbox names, such as, 'sh1_803','sh2_803', etc.

When you submit the form, traverse the keyname and value of $_POST variable and test if the keyname starts with 'sh'. If it is, remove the 'sh' and use explode() to to separate the inpubox number and userid, and of course get the value.

You can now update your table by doing a query like ...

Code: Select all

$query = "UPDATE tablename SET sh".$inputbox_number."='".$value."' 
    WHERE id=".$userid;
Tell me if it didn't work out or it is better if you explain ore further.
br5dy
Forum Newbie
Posts: 22
Joined: Sat Jul 23, 2005 2:52 pm

Post by br5dy »

Thanks for your help guys. I ended up using $_POST to help out. It's a little cheat design but I guess it'll work.

Code: Select all

$finduser = mysql_query("SELECT username FROM main WHERE auth <> 'Advisor' ORDER BY `firstname` ASC",$n);
while ($theuser = mysql_fetch_row($finduser))
{
for ($i=1; $i<=6; $i++)
{
	$real_sh = "s" . $i . "_" . $theuser[0];
	mysql_query("UPDATE main SET s" . $i . " ='" . $_POST[$real_sh] . "' WHERE username='$theuser[0]'",$n);
}
}
and it works good too!
Post Reply