I need help ... that too from scratch as now am learning php. just variable declaration i am learning.
I have created a form with 5 check boxes. when i select any 1 or any 2, 3.. or any combination, i should get the data which is already stored for that option in MySQL database.
My form is this:
<form method="post" action="search.php" name="search_form" onsubmit="return checkCheckBoxes(this);">
<input type="checkbox" name="search1" value="qwerty_keypad" id="search1">QWERTY Keypad
<input type="checkbox" name="search2" value="touch_screen" id="search2"> Touch Screen
<input type="checkbox" name="search3" value="usb" id="search3"> SUB Drive
<input type="checkbox" name="search4" value="mobile_tracker" id="search4">Mobile Tracker
<input type="checkbox" name="search5" value="Backup" id="search5">Phone backup on MMC
<input type="submit" value="Search" /> </form>
what i should write in search.php.
Please help me ... please
Thanks in advance
Use an array to submit the values
<input type="checkbox" name="search[connectivity]" value="usb" id="search3"> USB
<input type="checkbox" name="search[display]" value="touchscreen" id="search4">Touchscreen
Afterwards you build your query based on those values:
foreach($_POST['search'] as $k=> $search){
$where[]= $k." = '".mysql_real_escape_string($search)."'";
}
$query = "Select * from table where ".implode(' AND ',$where);
Related
I have a form using the GET method consisting of checkboxes.
This form is sending data to another page that is receiving the GET info and using it to pull info from a json api. I need to have it send the name once with all values combined into one string like this: example.com/color=RedGreenBlue
I am able to get all values combined and echoed onto the page but because they are in a foreach loop I am not able to pass them in the form. I tried below using the hidden field to pass them with no luck.
This is the method I've seen suggested but does not work for me:
<form action="" method="get">
Red<input type="checkbox" name="color[]" value="Red">
Green<input type="checkbox" name="color[]" value="Green">
Blue<input type="checkbox" name="color[]" value="blue">
<input type="submit" value="submit">
<?php
$name = $_GET['color'];
if (isset($_GET['color'])) {
foreach ($name as $color){
echo $color;
}
}
?>
<input type="hidden" name="MajorArea" value="<?php echo $color; ?>" />
</form>
Is there a way to assign one name to a group of checkboxes? Is there a way to pull the foreach loop data and use it outside a loop? Am I overlooking a way that is much easier than this?
Thanks for any advice!
I'll go out on a limb here and delete if it isn't what you're after. $_GET['color'] will be an array or it will be empty. You could also use isset:
<form action="" method="get">
Red<input type="checkbox" name="color[]" value="Red">
Green<input type="checkbox" name="color[]" value="Green">
Blue<input type="checkbox" name="color[]" value="blue">
<input type="submit" value="submit">
</form>
<?php
if (!empty($_GET['color'])) {
echo implode($_GET['color']);
}
?>
I have 4 different MySQL query's and I want to be able to choose which one is used based on which radio button is clicked. I was wondering what would be the best way to do this?
<form action="">
<input type="radio" name="query1" value="">Rating<br>
<input type="radio" name="query2" value="">Genre<br>
<input type="radio" name="query3" value="">Year<br>
<input type="radio" name="query4" value="">Rating,Genre,Year
</form>
Do I store the query's on individual php pages and then call them using the radio button or ....?
The query's select movie info from a database and then order them by either rating, year, genre or all three.
Set all your radio buttons to hold the same name attribute but with different values, then upon submit and choosing the desired radio button, query for what is matched.
<?php
if(isset($_POST['submit'])){
if(!isset($_POST['query'])){
echo "You chose nothing";
}
if($_POST['query'] == "Rating"){
// query for Rating
echo "Rating";
}
if($_POST['query'] == "Genre"){
// query for Genre
echo "Genre";
}
if($_POST['query'] == "Year"){
// query for Year
echo "Year";
}
if($_POST['query'] == "Rating,Genre,Year"){
// query for Rating,Genre,Year
echo "Rating,Genre,Year";
}
} // brace for if(isset($_POST['submit']))
?>
<form action="" method="post">
<input type="radio" name="query" value="Rating">Rating<br>
<input type="radio" name="query" value="Genre">Genre<br>
<input type="radio" name="query" value="Year">Year<br>
<input type="radio" name="query" value="Rating,Genre,Year">Rating,Genre,Year
<br>
<input type="submit" name="submit" value="Submit query">
</form>
This is at best, a basic example.
When using database-related code, remember to use mysqli with prepared statements, or PDO with prepared statements, they're much safer.
Radio buttons must have same name.
Without a method="post" the <form> method will be "get" so the PHP would use $_GET not $_POST
<form action="">
<input type="radio" name="query" value="1">Rating<br>
<input type="radio" name="query" value="2">Genre<br>
<input type="radio" name="query" value="3">Year<br>
<input type="radio" name="query" value="4">Rating,Genre,Year
</form>
I am not a big fan of using IF ELSE branching structures and avoid them whenever possible.
I prefer passing integer values to be used in arrays.
PHP
$q = intval($_GET['query']);
The intval() will return a zero if ($_GET['query'] return no value.
$queries = array(
0 => $query4,
1 => $query1,
2 => $query2,
3 => $query3,
4 => $query4);
$sql = $queries[$q];
I am trying to do eCommerce filtering on the category page. So far I can get it to work for one colour, but if the user checks more than one colour, I want it to search for the other colour too.
URL
//current
category.php?colourChoice=White
//more than one
category.php?colourChoice=White&colourChoice=Ruby+Red
I realise I need some sort of form array or an explode?
MYSQL
products
---
id sku price
1 p22 45.00
2 p33 65.00
3 p44 70.00
colours
---
id sku name
1 p22 White
2 p33 Ruby Red
3 p44 Black
HTML
<form action="form.php" method="get">
<input type="checkbox" name="colourChoice" value="White"/>
<input type="checkbox" name="colourChoice" value="Black"/>
<input type="checkbox" name="colourChoice" value="Ruby Red"/>
<input type="checkbox" name="colourChoice" value="Orange"/>
<input type="submit" value="go">
</form>
PHP
// VARS
$colourName=mysql_real_escape_string($_GET["colourChoice"]);
// MYSQL
$checkSQL=mysql_query("
SELECT * FROM `products`
INNER JOIN `colours`
ON `products`.`sku` = `colours`.`sku`
WHERE (
name = '".$colourName."'
OR name LIKE '".$colourName.";%'
OR name LIKE '%;".$colourName.";%'
OR name LIKE '%;".$colourName."'
)
");
// SHOW RESULTS
while($r = mysql_fetch_array($checkSQL)) {
echo '
<div class="product">
Cost £'.$r['price'].'<br />
</div>
';
}
You have to make checkbox array as below
<form action="form.php" method="get">
<input type="checkbox" name="colourChoice[]" value="White"/>
<input type="checkbox" name="colourChoice[]" value="Black"/>
<input type="checkbox" name="colourChoice[]" value="Ruby Red"/>
<input type="checkbox" name="colourChoice[]" value="Orange"/>
<input type="submit" value="go">
</form>
Then, you will get multiple value checked into array in php.
foreach($_GET["colourChoice"] as $value){
/// here you can make concat for where clause.
}
You can get HTML input values in PHP as array:
HTML
<form action="form.php" method="get">
<input type="checkbox" name="colourChoice[]" value="White"/>
<input type="checkbox" name="colourChoice[]" value="Black"/>
<input type="checkbox" name="colourChoice[]" value="Ruby Red"/>
<input type="checkbox" name="colourChoice[]" value="Orange"/>
<input type="submit" value="go">
</form>
Then you query the database using prepare/execute statements to prevent SQL injection.
PHP
$placeholders = substr(str_repeat('?, ', count($_GET['colourChoice']), 0, -1);
$query = "
SELECT * FROM `products`
INNER JOIN `colours`
ON `products`.`sku` = `colours`.`sku`
WHERE name in ($placeholders);
");
$db = new PDO(...);
$stmt = $db->prepare($query);
$stmt->execute($_GET['colourChoice']);
print_r($stmt->fetchAll());
I have a list of emails with a checkbox next to it, the user will be able to select which address he/she wants to email. Now i've added another checkbox that when checked will check all the other checkbox. Below is the code i wrote (with help from stackoverflow of course) :
<SCRIPT LANGUAGE="JavaScript">
function selectFunction (checkall,field)
{
if(checkall.checked==true){
for (i = 0; i < field.length; i++)
field[i].checked = true ;
}else{
for (i = 0; i < field.length; i++)
field[i].checked = false ;
}
}
</script>
<form name="myform" action="profile-invite.html" method="post">
<b>Your Favorite Scripts & Languages</b><br>
<input type="checkbox" name="list" value="1">aaa#xxx.com<br>
<input type="checkbox" name="list" value="2">bbb#xxx.com<br>
<input type="checkbox" name="list" value="3">ccc#xxx.com<br>
<input type="checkbox" name="list" value="4">ddd#xxx.com<br>
<input type="checkbox" name="list" value="5">eee#xxx.com<br>
<input type="checkbox" name="selectallcb" value="Check All"
onClick="selectFunction(document.myform.selectallcb,document.myform.list)">
<input type="submit" name="formSubmit" value="Submit" />
</form>
The "select all" function works fine, but using print_r i saw that when the submit button is clicked, the submited value is the last checkbox that i selected. For example if i click 5-3-1-2, the value in $_POST is "2" and not the rest.
i realized that my code can only register ONE selected checkbox, therefore only the last one is taken into account. So I rewrote the code by adding [] behind the name of the checkbox :
<form name="myform" action="profile-invite.html" method="post">
<b>Your Favorite Scripts & Languages</b><br>
<input type="checkbox" name="list[]" value="1">aaa#xxx.com<br>
<input type="checkbox" name="list[]" value="2">bbb#xxx.com<br>
<input type="checkbox" name="list[]" value="3">ccc#xxx.com<br>
<input type="checkbox" name="list[]" value="4">ddd#xxx.com<br>
<input type="checkbox" name="list[]" value="5">eee#xxx.com<br>
<input type="checkbox" name="selectallcb" value="Check All"
onClick="selectFunction(document.myform.selectallcb,document.myform.list)">
<input type="submit" name="formSubmit" value="Submit" />
</form>
Now, it registers the multiple selections when i checked with print_r.(if i click 5-3-1-2, the value in $_POST is now [0]=>5,[1]=>3,[2]=>1,[3]=>2.)
But the "select all" checkbox doesn't work anymore. I assume is due to the [] which transformed the field into an array. I tried various methods (by replacing "document.myform.list" with "document.myform.list[]" etc. ) None is working so far, i'll continue experimenting but if anybody have a clear idea on how to merge the 2 codes above please help.
Thank You
Change this:
onClick="selectFunction(document.myform.selectallcb,document.myform.list)"
to this:
onClick="selectFunction(document.myform.selectallcb,document.myform['list[]'])"
Here's the fiddle: http://jsfiddle.net/NxfH6/
Am Newbie.I have this coding...How to store into Mysql and How to Retrieve from the Database?
<input type="checkbox" name="like" value="yes"/>Yes
<input type="checkbox" name="like" value="no"/>No
am doubt with if i select both 2 box means what will happen?
Please Explain...Thanks in Advance..
If you have no problem with comma separated values
my answer will help you
simple use array name to your field
<input type="checkbox" name="like[]" value="yes"/>Yes
<input type="checkbox" name="like[]" value="no"/>No
then implode it with comma
implode(","$_POST['like']);
if two box is checked then
result will be yes,no
make your column in varchar
Then do usual stuff to store
yourscript.php:
<?php
include("mysql_connect.php");
if ($_POST['like'])
{
if ($_POST['like'] != "yes" and $_POST['like'] != "no") die("Hacker");
mysql_query("INSERT INTO likes ('id', 'like') VALUES ('', '".$_POST['like']."')");
}
?>
<form method="post" action="yourscript.php">
<input type="checkbox" name="like" value="yes"/>Yes
<input type="checkbox" name="like" value="no"/>No
</form>
retreive.php:
<?php
include("mysql_connect.php");
$likes = mysql_query("SELECT * FROM likes");
while($like = mysql_Fetch_assoc($likes))
{
echo "ID ".$like['id']." => ".$like['like'];
}
If you select both fields, it will save the latest select.
You want radio (probably)
You probably don't want to be doing this in the first place. What you really want to be using is radio buttons for yes/no values. Otherwise, you're going to run into problems.
you can use like this
<input type="checkbox" name="like[]" value="yes"/>Yes
<input type="checkbox" name="like[]" value="no"/>No
to store in db use serialize(); function.
unserialize() return the same array with keys.