Jump to content

[SOLVED] putting form data into an array.


overlordofevil

Recommended Posts

hello all,

 

I need some help. I don't understand arrays and have been looking for tutorials on them but its not really helping me figure out what i am trying to do .

 

Basically I have a mysql table that holds customer info. What I am doing is openign the table and pulling out the user id's to create a list.

 

$query = "SELECT * FROM users ORDER BY users.lastname"; 
$result = mysql_query($query) or die (mysql_error());
while ($row = mysql_fetch_array($result))
{
extract($row);
$uid = $row[id];
echo "<tr><td><input type='checkbox' name='myarray[]' value='$uid' /></td>\n";

 

so from the selection you see that I am setting the user id to a check box so when the form is processed it processes the specific ids.

 

Now I got that part to work what I am trying to do is set up the form to allow for user input on multiple items at once. So the user would get the form and check the item(s) they want to affect.

 

so for example the form info they user would get is something like this

 

echo "<tr><td><input type='checkbox' name='myarray[]' value='$uid' /></td>\n";
echo "<td><input type='int' name='amt' size='5'></td>
<td><input type='text' name='reason' size='20'></td></tr>\n";
//this code is not exact just showing an example

 

ok so I hope I explained what I am trying to do so far and haven't made it too confusing. :)

 

now what I need to do is take the separate inputs (amt and reason) and get them go into an array that ties in to the check box (this is the id's) so when the form is processed the loop script will look for the check box and process the input appropriately.

 

I think this should be a multidimensional array but I just don't know how to get the checkbox, and the other variables into a single array that can be posted and then processed.

 

any suggestions would be very appreciated.

Link to comment
https://forums.phpfreaks.com/topic/137567-solved-putting-form-data-into-an-array/
Share on other sites

how about:

 

echo "<tr><td><input type='checkbox' name='myarray[]' value='$uid' /></td>\n";
echo "<td><input type='int' name='amt_<? echo $uid;?>' size='5'></td>
<td><input type='text' name='reason_<? echo $uid;?>' size='20'></td></tr>\n";

 

then you can do:

 

<?php
if ($_REQUEST['amt_' . $uid] != '')
{

}

if ($_REQUEST['reason_' . $uid] != '')
{

}
?>

 

or something like that. i guess it would depend on what you're doing after the form has been submitted. infact, personally, i wouldn't use an array at all.

ok fair enough... I appreciate the suggestion.

 

What i am doing is taking the input from the form and updating the users file based. such as amt is used to add or subtract from a value in the users file.  the reason is inserted into a log file with other info after the process is done.

 

If there is another way to do multiple files at once and use the check boxes to select which ones i want to update what way would you suggest.

you were right in what you were doing, it's just not exactly how i would do it, but it is close. Not to say my way is better, it's just what i'm used to.

 

i would do something like this:

 

<?php
$query = "SELECT * FROM users ORDER BY users.lastname";
$result = mysql_query($query) or die (mysql_error());
while ($row = mysql_fetch_array($result))
{
  extract($row);
  $uid = $row[id];
  echo "<tr><td><input type='checkbox' name='uid_$uid'/></td>\n";
  echo "<tr><td><input type='text' name='amt_$uid'/></td>\n";
  echo "<tr><td><input type='text' name='uid_$uid'/></td>\n";
}
?>

then the action page would go something like this:

 

<?php
$query = "SELECT * FROM users ORDER BY users.lastname";
$result = mysql_query($query) or die (mysql_error());
while ($row = mysql_fetch_array($result))
{
  extract($row);
  $uid = $row[id];
  if ($_REQUEST['uid_' . $uid] != '')
  {
    //it was checked
    //change this to do what you need to do
    echo "amt entered was " . $_REQUEST['amt_' . $uid];
    echo "reason entered was " . $_REQUEST['reason_' . $uid];
  }
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.