Jump to content


Photo

Entering to a MySQL DB using checkboxes?


  • Please log in to reply
2 replies to this topic

#1 triphis

triphis
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts

Posted 07 November 2003 - 04:00 PM

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.
Xac Attack

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 07 November 2003 - 08:41 PM

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
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 triphis

triphis
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts

Posted 07 November 2003 - 10:36 PM

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!
Xac Attack




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users