Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing checkbox variables that are displayed from a db 2

Status
Not open for further replies.
May 13, 2005
56
US

I have some code that reads from a db and displays all contents of one of its fields..
As you can see it reads from the managers table and is sorting it by the name field.

here is the code..

<?
mysql_connect('localhost',$username,$password);
mysql_select_db($database) or die("Unable to select database");
$query=("SELECT * FROM managers ORDER BY name ASC");
$result=mysql_query($query);

if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
$num=mysql_numrows($result);
mysql_close();

echo "<table border=0 width=800 cellpadding=2 cellspacing=2 align=center>";
echo "<tr>";
$i=0;
while ($i < $num) {

$name=mysql_result($result,$i,"name");

echo "<td align=right><input type=checkbox value=$name name=$name />$name </td>";
$i++;
}
?>

So I am creating a table that reads each item in the field and displays it as a checkbox, giving each checkbox a name and value of the name it pulls from the DB

lets say the names in the db are Alan, Brett, and Cathy

How would I assign these names as variables so they are sent to my next from??
for exapmle if just Alan is checked, on my next form I dont want to put $_POST['Alan'], I would like it to be a little more flexible then that if possible... Plus I want to require the users to check 2 of the boxes to continue.

I was thinking I could read from the same db and do something like this on the next form:
if $_POST[$name] {
do whatever
}

but that is not the right answer..

I want this to work with whatever name is inserted into the db..

?? Im fairy green at php still and I have no idea how to even look this one up.. Any ideas?

Thanks,

Mike
 
PHP has this undocumented feature (as far as I know, it's undocumented) that if you have a set of form inputs with names that appear to be array references:

<input type="text" name="a[1]">
<input type="text" name="a[2]">
<input type="text" name="a[3]">
<input type="text" name="a[4]">

When the form is submitted, PHP will put all those values into a single array within $_GET or $_POST. If the above had been inside a POST-method form, $_POST['a'] would itself be an array in the script to which the form was submitted.


I have a test script called show_post.php, which consists entirely of:

Code:
<?php
print '<html><body><pre>';

print_r ($_POST);

print '</pre></body></html>';
?>

As a piece of example code to your question, I have created test_multicheck.php, which reads:

Code:
<?php
$a = array ('alan', 'betty', 'charles', 'darla');

print '<html><body><form method="post" action="show_post.php">';

print '<table border="1">';
foreach ($a as $name)
{
   print '
   <tr>
      <td align=right>
         <input type="checkbox" name="managers[' . $name . ']" />
      </td>
      <td>
      ' . $name .'
      </td>
   </tr>';
}

print '</table><input type="submit"></form></body></html>';
?>

When I point my browser to test_multicheck.php, I get a table with for checkboxes and for names. If I were to only check the boxes next to alan and betty then submit the form, show_post.php returns:

[tt]Array
(
[managers] => Array
(
[alan] => on
[betty] => on
)

)[/tt]

Is this what you were asking?


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
You wrote:
Code:
<?
mysql_connect('localhost',$username,$password);
mysql_select_db($database) or die("Unable to select database");
$query=("SELECT * FROM managers ORDER BY name ASC");
$result=mysql_query($query);

if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
$num=mysql_numrows($result);
mysql_close();

echo "<table border=0 width=800 cellpadding=2 cellspacing=2 align=center>";
echo "<tr>";
$i=0;
while ($i < $num) {

$name=mysql_result($result,$i,"name");

echo "<td align=right><input type=checkbox value=$name name=$name />$name </td>";
$i++;
}
?>
Here's how I would modify your code:
Code:
<?
mysql_connect('localhost',$username,$password);
mysql_select_db($database) or die("Unable to select database");
$query="SELECT name FROM managers ORDER BY name ASC"; // note [COLOR=red]1[/color]
$result=@mysql_query($query) or die('Invalid query: ' . mysql_error() . '<br>' .'Whole query: ' . $query ); // note [COLOR=red]2[/color]
 // note [COLOR=red]3[/color]
echo "<table border=0 width=800 cellpadding=2 cellspacing=2 align=center>";
echo "<tr>";
while ($row = @mysql_fetch_assoc($result)) // note [COLOR=red]4[/color]
echo '<td align=right><input type=checkbox value="' . $row['name'] . '" name="managers[]" />' . $row['name'] . "</td>\n"; // note [COLOR=red]5[/color]
?>

A few notes:
[ol]
[li]Why fetch all of the fields when you're only using one?[/li]
[li]Use the die() function to output your mysql errors.[/li]
[li]Removed the get the number of rows (see next note)[/li]
[li]Use a while() statement to loop through the rows and fetch the information into an associative array[/li]
[li]Notes follow:[/li]
[ul]
[li]Always put your values within double quotes in HTML or you will have problems if the value contains a space[/li]
[li]The name passed back to PHP will be an associative array with the names being the indecies. Only those that are checked will be returned to your proccessing script[/li]
[/ul]
[/ol]

Have fun.... :)

Ken
 
Thanks for the answers guys... With what I am tring to accomplish, I think that Ken's solution is what I was looking for..

sleipnir214. yours looks like I will have to put the array members on the page? I want the info to be pulled from a db so when the managers change, i dont have to update the code. Using your show_post.php code correctly outputs the checkboxes from kens code though...

Ken,
Thanks for the code suggestions, I told you I was pretty green :)

So guys, now if I am using this code to populate the checkboxes etc, what variable will I pass to the submitted page?

the print code outputs this:
[managers] => Array
(
[0] => Alan
[1] => Brett
)

so what syntax would I use to assign a variable to Brett?
for example $test=$_POST[I dont know what goes here exactly];

I tried a couple of things that I thought would work, but dont :(

Thanks,

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top