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
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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.