Jump to content

How do i proccess a check-boxes with php?


anybody99

Recommended Posts

I asked something on : http://www.phpfreaks.com/forums/index.php/topic,138898.0.html

 

but i guess that question belongs more to php, so how do i process a check box function for example I have 3 check boxes to filter a results page, and when users click 1/2 or 3 boxes and then click 'go' that will filter the results [checks with mysql] so then only the necessary information will be retrieved from the DB.

i know how to make the boxes or what so ever and i also know how to get information from mysql DB with php codes but i don't know how to start write it and how exactly doest it work.

 

here is an example of what i look for to process although it's in ASP which i don't know.

http://w3schools.com/tags/tryit.asp?filename=tryhtml_form_checkbox

 

thanks for any help,

Rob

Link to comment
Share on other sites

I made a sample page, and although it doesn't deal with sql, it should show you how you could dynamically create sql queries.

 

Hopefully the comments are clear enough, but I'm bad at commenting, so....

 

<?php
if($_POST) { //if the POST array has data in it.
//since the method of the form is POST, the values from the form will be stored in $_POST... had it been via GET it would be stored in $_GET
if($_POST['pie'] == "true") { 
	/*since the value of the input with the name pie was true, if it's checked, then $_POST['pie'] will be true.
	Note how it uses true as a string, not boolean since it will be passed into post as a string.  
	You could use if($_POST['pie'] == true) and it would evaluate correctly....  
	if($_POST{'pie'] === true) would evaluate to false because === makes sure datatypes match as well as values.*/
	echo "You like pie!";
}
else { //if it doesn't equal true
	echo "How can you not like pie?";
}
echo "<br />"; //put a line break between the messages ;p
if($_POST['cake'] == true) { //this is an example to show what was mentioned under pie.... if the value of cake was set to 1, then it would also evaluate to true.
	echo "You like cake!";
}
else {
	echo "You don't like cake?  Crazy....";
}
echo "<br />"; //put a line break between the messages ;p

if($_POST['icecream'] === true) {
	/*
	this if statement will never evaluate to be true because post values are always passed as strings (if you do math or what ever on integers, PHP is very good about automatically changing datatypes).  If you manually set $_POST['icecream'] = true; it would evaluate to true because that would set it as a boolean.  Once again, === matches datatype and value.  
	*/
	echo "I like icecream too!";
}
else {
	echo "Even if you checked icecream, this won't be correct ;p";
}
echo "<br />"; //put a line break between the messages ;p

}

echo '<form action="" method="POST">'; //this echos the opening tag of the form... note the method=post... When post is used it is accessed via the $_POST array
echo '<label for="pie">I like pie: </label><input type="checkbox" name="pie" value="true" id="pie" /><br />'; //the name of the input is what php looks for... for example if an input was named 'key' then you would access it via $_POST['key']
echo '<label for="cake">I like cake: </label><input type="checkbox" name="cake" value="true" id="cake" /><br />';
echo '<label for="icecream">I like icecream: </label><input type="checkbox" name="icecream" value="true" id="icecream" /><br />';
echo '<input type="submit" value="Submit">';
echo '</form>';
?>

Link to comment
Share on other sites

Thanks ! just what i needed, but some questions,

 

What the difference between GET and POST?

and also if i will check 2 boxes then it will work right? both echo of the 2 boxes will appear, right?

and also, again about GET VS POST, what would be the best choice to filter a results page, for example data was retrived from the DB and some of the date is in Dollars, and some of it in Euro's and some in GBP's so if i check that i want $ and GBP's then each one of them[of the boxes, 2 seprate 'if'] will do a query from the DB and will post the results, right?

 

Link to comment
Share on other sites

GET is passed in the url making it very easy to modify from the client end, but POST is passed behind the scenes (of course it can be edited though; always check user input ;p).

 

I'm confused at what you're trying to do with your DB stuff....

 

Let's say your table looks like the following and is named 'items':

itemid,

itemname,

usd,

euro,

gbp

 

Would you be wanting to do something like "SELECT itemid, itemname, usd FROM items", or are you wanting to order results by the different currencies?

Link to comment
Share on other sites

I have the following:

Id: it's just a number of offer

title of offer,

price of the offer in us dollar

country for where that offer is available : All/US/Canada/UK

special requirements like: Free offer or not free so it will say Free/ Not_Free

and then again specaial requerments of credit card: Yes/No

 

So... I want to do a search/Filter the offers, i want that the user will have the abilty to take all the offers from the US let's say who needs Credit card or take all FREE[trial] offers who need Credit card [and then again if possible to filter the results that i got by price(higher price on top let's say) automatically]..... so i thought making it with check boxes. what do think would be the best way to do that?

 

thanks,

Rob

 

Link to comment
Share on other sites

In this example I used checkboxes for country allowing all (none checked), one or more countries to be selected.

 

For the offers, radio buttons, allow either free or not-free as one or other must be selected

 

Depends on you how you want to interact with the user.

 

<?php
if (isset($_POST['action'])) { 
    $where = array();
    $whereclause = '';
    /**
    * build the selection criteria array
    */
    if (isset($_POST['country'])) {
        $clist = join ("','", $_POST['country']);
        $where[] = "(country IN ('$clist'))";
    }
    if (isset($_POST['offer'])) {
        $offer = $_POST['offer'];
        $where[] = "(offer = '$offer')";
    }

    if (count($where)) $whereclause = ' WHERE ' . join (' AND ', $where);

    $sql = "SELECT * FROM offers $whereclause ORDER BY price DESC";

    echo "Your query:<p>$sql</p>";
}
?>
<form method='post'>
Select country  
    <input type="checkbox" name="country[]" value="US">US 
    <input type="checkbox" name="country[]" value="CA">Canada 
    <input type="checkbox" name="country[]" value="UK">UK
    <br>
Offers 
    <input type="radio" name="offer" value="free" checked>Free
    <input type="radio" name="offer" value="not_free">Not free
    <br> 
<input type="submit" name="action" value="Search">
</form>

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.