how to get multiple form values as variables in PHP?

SilverNodashi

Expert Member
Joined
Oct 12, 2007
Messages
3,337
Hi,

Can someone please help me with this?

I have a form, which is dynamically build from a database, as follows:

ID ---- Username ------Visit ------ Reason
| | | |
12 John <select box> <text field>
8 Ann <select box> <text field>
97 Peter <select box> <text field>
35 Collin <select box> <text field>



So, basically there's an ID & username that's being pulled from the DB. This list is dynamic, depending on various factors so the length will differ every time.

Now, I need to save this info into a different table, but only the "ID", "Visit" and "Reason" fields' date.
The problem is, when I submit this form to the script, all field are submitted as variables and I need to extract those variable values. But, how?

I have the following (slightly modified so far, but it isn't helpful:



PHP:
<?PHP

$userids = JRequest::getVar( 'userid' );
$userchecks = JRequest::getVar( 'check' );
$reasons = JRequest::getVar( 'reason' );

if (isset($userids)) {
		foreach($userids as $userid ) {
		    echo '<i>UserID:</i> '.$userid.' <i>Visit:</i> '.$usercheck.' <i>Reason: </i>'.$reason.'<BR>';
		}
}


From here, the "userid" field get returned fine, but the "usercheck" & "reason" variables return as empty values. How do I get those values as well, for that row?
 

CaffeineKing

Member
Joined
Jul 30, 2005
Messages
10
By the looks of it the JRequest::getVar calls are returning arrays. The foreach command is only interating through the $userids array and populating the value of $userid. The following might work if I have understood your description of the scripts current behavior correctly.

Code:
<?PHP

$userids = JRequest::getVar( 'userid' );
$userchecks = JRequest::getVar( 'check' );
$reasons = JRequest::getVar( 'reason' );

if (isset($userids)) {
        for ($n = 0; $n < count($userids); $n++) {
            echo '<i>UserID:</i> '.$userids[$n].' <i>Visit:</i> '.$userchecks[$n].' <i>Reason: </i>'.$reasons[$n].'<BR>';
        }
} 
?>

This is not the prettiest solution and it assumes that all the arrays have the same number of values in but it might give you some ideas.

Another option is to work directly with the $_POST array. This might not be the easiest if your framework is working with those values before exposing them to you.

Hope that helps
 

SilverNodashi

Expert Member
Joined
Oct 12, 2007
Messages
3,337
Thanx, CaffeineKing. This did the trick :) I had to fixup the checkboxes to have a "[]" as well, otherwise it broke up the "Yes" into "Y", "e", "s" on every turn. But now that's fixed as well
 
Top