Forum Moderators: coopster

Message Too Old, No Replies

Search Query + checkbox option

trying to write php for above options

         

omfLondon

9:02 pm on Mar 27, 2007 (gmt 0)

10+ Year Member



Hi folks,

I am a newbie poster to the site and have just started using php and mysql - have been painstakingly getting dummy site set up which sells CDs, I have searched this site to find the answer to my prob; which is:

On the catalogue page - user or guest can search the catalogue using text field, but I also want the option of choosing from 4 checkboxes (they can all be checked) for type of music e.g. C&W, pop, rap, blues. Also, they can input year of release and price (but I haven't got around to how to do that yet! - maybe another text field or ).... basically HELP! I've been going round in circles trying to figure it out as I'm new .... any help gratefully received!

Thanks; OM

omfLondon

9:03 pm on Mar 27, 2007 (gmt 0)

10+ Year Member



Me again, sorry I should add, that I want user to press Submit to query the table that has all details of music, artist etc.

eelixduppy

9:08 pm on Mar 27, 2007 (gmt 0)



Welcome to WebmasterWorld, omfLondon!

When handling checkboxes, it is very similar to handling other post data, with one exception. Here is a sample form:


<form action="page.php" method="post">
one<input type="checkbox" value="1" [b]name="numbers[]"[/b] />
two<input type="checkbox" value="2" [b]name="numbers[]"[/b] />
three<input type="checkbox" value="3" [b]name="numbers[]"[/b] />
<input type="submit" />
</form>

Notice here how I have defined the name attribute with the two brackets at the end. This signifies that these values are going to be arrays.

And now here is

page.php
handling the checkboxes and echoing the selected ones to the browser:

<?php
#
$checkboxes = [url=http://us3.php.net/manual/en/reserved.variables.php#reserved.variables.post]$_POST[/url]['numbers']; #this is the array from the form (only selected values)
echo '<pre>';
[url=http://www.php.net/print-r]print_r[/url]($checkboxes);
echo '</pre>';
#
?>

You'll see here that it will print out only those that have been selected. Now from here, you can use various array functions [us3.php.net] to handle the data accordingly.

Also, if it will help, here's the documentation on the form element [w3.org]. Should provide you with some detailed information ;)

Good luck!

eelixduppy

9:13 pm on Mar 27, 2007 (gmt 0)




Also, they can input year of release and price (but I haven't got around to how to do that yet! - maybe another text field or )

A text field would work, but you could also use a SELECT [w3.org] element, which, I believe, would be better in this case. Handling a select is similar to the other input types.

omfLondon

9:14 pm on Mar 27, 2007 (gmt 0)

10+ Year Member



wow, that was quick! Thank you sooo much - I'm going to give it a go. Ta

dreamcatcher

9:22 pm on Mar 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



omfLondon, welcome to Webmaster World. :)