Fetch a checkbox list and get them in mysql - php

I'm fetching values from a checkbox list and would like to get them in mysql, just like the following sql query shows:
<form action="index2.php" method="get">
<input type="checkbox" name="check_list[]" alt="Checkbox" value="one">One
<input type="checkbox" name="check_list[]" alt="Checkbox" value="two">Two
<input type="checkbox" name="check_list[]" alt="Checkbox" value="three">Three
<input type="submit" value="Submit">
<?php
if(!empty($_GET['check_list']))
{
foreach($_GET['check_list'] as $check)
{
$ids = implode(',', $check);
echo $ids;
}
}
$sql="SELECT * FROM table WHERE activity IN($ids)";
I've tried with array(), implode(), explode()...
Nothing works.
this is a simplified code: I did not put the code against injection.
Can anybody be of help. Thanks :)
?>
Can anybody be of help? Thanks very much in advance :)

Related

Combine form values under one name using GET method

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']);
}
?>

How to update table based on the list from an array in php and mysql?

I am developing an application where I need to create challan for customers. But before I ask the question I beg your excuse as I did not search if anyone had already posted similar query. My problem, precisely, is that I am grabbing form data and storing them in an array and finally going to update the same into mysql table with only those tuples guided by the array list. Below is my scheme.
success.php :
<form action="payment.php" method="post">
<input type="checkbox" name="check_list[]" value="100">
<input type="checkbox" name="check_list[]" value="100">
<input type="checkbox" name="check_list[]" value="100">
<input type="checkbox" name="check_list[]" value="100">
<input type="checkbox" name="check_list[]" value="100">
<input type="submit" />
</form>
payment.php
<?php
if(!empty($_POST['check_list'])) {
$form_nos = array();
foreach($_POST['check_list'] as $check) {
$form_nos[] = $check;
}
// comma-separated list
$list_form_nos = "";
for($i = 0; $i<count($form_nos); $i++) {
if($i == count($form_nos)-1)
$list_form_nos .= $form_nos[$i];
else
$list_form_nos .= $form_nos[$i].",";
}
// And finally
$challan_no = uniqid();
$challan_date = date("Y/m/d");
$db->exec("update customers set challan_no=$challan_no, challan_date=$challan_date where cust_id in ($list_form_nos)");
?>
My question is that, whether this would work? Though I have not executed the sql part but I would be highly obliged if you provide some elegant other than this.

Combine arrays depending on checked boxes

I have a form with check boxes.
I want it so that when a check box is checked, it includes an array.
<input type="checkbox" name="main" value="main" checked> Main/unsorted<br />
<input type="checkbox" name="art" value="art" checked> Art/literature/music<br />
<input type="checkbox" name="games" value="games" checked> Games/gaming<br />
If main is checked include the array 'main', if art is checked include the array 'art', etc.
I've tried, but I can't find a function that would work for this scenario.
Edit: I'm cheating a bit and am now doing it like so.
foreach($_GET as $get) {
$end = array_merge($end, $$get);
}
From your information it sounds like you want to merge an array depending on which checkboxes have been ticked? Am I correct in assuming this?
Is something like this what you are looking for?
<?php
$combinationArray = array();
$mainArray = array('item1','item2','item3');
$artArray = array('item4','item5','item6');
$gamesArray = array('item7','item8','item9');
if(isset($_POST['main']) && $_POST['main']=='main'){
$combinationArray = array_merge($combinationArray,$mainArray);
}
if(isset($_POST['art']) && $_POST['art']=='art'){
$combinationArray = array_merge($combinationArray,$artArray);
}
if(isset($_POST['games']) && $_POST['games']=='games'){
$combinationArray = array_merge($combinationArray,$gamesArray);
}
?>
HTML:
<form action="yourpage.php" method="post">
<input type="checkbox" name="main" value="main" checked> Main/unsorted<br />
<input type="checkbox" name="art" value="art" checked> Art/literature/music<br />
<input type="checkbox" name="games" value="games" checked> Games/gaming<br />
<button>
Submit
</button>
</form>

Getting ID's from checkboxes

I'm trying to get the ID's from a load of checkboxes that are in rows that are printed out on a page using a while statement. Each row from the database has a checkbox next to it with the ID in the checkbox value.
Basically I want to do a update query on the checkbox-selected rows, using the ID.
The code for the checkboxes that I have used is:
<input type="checkbox" name="check_list[]" value="<? echo $rows['id']; ?>">
Then when the code for the submit is:
<?
if(!empty($_POST['check_list'])){
foreach($_POST['check_list'] as $id){
echo "$id was checked! ";
}
}
?>
Just wanted to echo out the results to test that it works before putting it into a query. Trouble is...nothing happens. I just get a blank screen. No error or anything. Surely it should work, it looks right but I don't understand why it doesnt work.
Any help is most appreciated! :)
Tested below code with one test.php file
<?php
if(!empty($_POST['check_list']))
{
foreach($_POST['check_list'] as $id){
echo "<br>$id was checked! ";
}
}
?>
<form method="post" name="frm">
<input type="checkbox" name="check_list[]" value="1"> 1
<input type="checkbox" name="check_list[]" value="2"> 2
<input type="checkbox" name="check_list[]" value="3"> 3
<input type="checkbox" name="check_list[]" value="4"> 4
<input type="submit" name="submit" />
</form>
please check if you are getting $rows['id'] properly. Things should work fine otherwise.
Thanks.

Searching and retrieving value from database through checkbox selection

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

Categories