Jump to content

Entering to a MySQL DB using checkboxes?


triphis

Recommended Posts

Okay, so I have a database with 2 columns: name, and status. It\'s basically for a collection I have. I want to run that info through a loop, and be able to check off the ones I have.

 

I\'ve managed this before using radio buttons, and having a new form for EACH item in my collection... but is there anyway to loop out one big form using checkboxes and 1 submit button? I\'m quite good with forms and PHP usully, but whatever I try for this, doesn\'t work :[ Thanks :)

 

This is what I used before (the radio buttons, and seperate submit buttons):

 

[php:1:477a06f548]<?php

$db = mysql_connect(\"localhost\", \"******\", \"******\");

mysql_select_db(\"*****_uk_db\",$db);

 

$q=\"SELECT name FROM items WHERE status=0 ORDER BY name ASC\";

$result = mysql_query($q,$db);

 

while ($row = mysql_fetch_array($result)) {

$name = $row[\"name\"];

?>

 

<form action=\"form.php?name=<?=$name?>\" method=\"post\">

<INPUT TYPE=\"radio\" NAME=\"<?=$name?>\" VALUE=\"1\"><br><br><input type=\"submit\" name=\"submit\" value=\"Go!\">

</form>

 

<?php

}

?>

[/php:1:477a06f548]

 

I will post the process page if needed.

Link to comment
https://forums.phpfreaks.com/topic/1315-entering-to-a-mysql-db-using-checkboxes/
Share on other sites

1. Put form header and footer tags outside the loop.

2. use checkboxes, not radio for multiple selections

3. give checkboxes an array name, such as \'chbox[]\'

 

[php:1:e8777ed79c]

<form action=\"form.php>\" method=\"post\">

 

<?php

 

while ($row = mysql_fetch_array($result)) {

$name = $row[\"name\"];

?>

 

<INPUT TYPE=\"checkbox\" NAME=\"chbox[]\" VALUE=\"<?=$name?>\">

<?=$name?><br>

 

<?php

}

<br>

<input type=\"submit\" name=\"submit\" value=\"Go!\"></form>

 

?>[/php:1:e8777ed79c]

 

Only checked checkboxes will be POSTed

 

To find which ones when processing the form

 

[php:1:e8777ed79c]<?php

$chkboxes = $_POST[\'chbox\'];

# $chkboxes is now an array of the posted names

foreach ($chkboxes as $name) {

# process then posted $names

}

?>[/php:1:e8777ed79c]

 

hth

Ohh, okay, i think I get it Barand: So then, on the process page, it will only take the $name variables, and I can do something like this:

 

$q="UPDATE table SET status=1 WHERE name=$name";

 

I think thats right o.O; I\'ll go try it ^^ Thanks for your help!

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.