i have the following checkboxes
<input type="checkbox" name="weekday[]" value="Monday" /> Monday
<input type="checkbox" name="weekday[]" value="Tuesday" /> Tuesday
<input type="checkbox" name="weekday[]" value="Wednesday" /> Wednesday </br>
<input type="checkbox" name="weekday[]" value="Thursday" /> Thursday
<input type="checkbox" name="weekday[]" value="Friday" /> Friday
<input type="checkbox" name="weekday[]" value="Saturday" /> Saturday
<input type="checkbox" name="weekday[]" value="Sunday" /> Sunday
I would like to enter all the checked values into "day" field in mysql separated by coma,
please help
How about
implode(',',$_POST['weekday'])
?
if(isset($_POST['submit_btn_name']))
{
$days="";
if(isset($_POST['weekday']))
{
foreach($_POST['weekday'] as $id)
{
$days.=$id.",";
}
$days = substr($days, 0, -1);
}
echo $days;
}
EDIT
this is in response to the comment about the query to post the $days variable as I found it difficult to format code in the comments.
$sql1=mysql_query("INSERT INTO class (class_id, subject_id, student_id, available_days, available_time, status) VALUES ('".$class_id."','".$subject_id."','".$student_id."','".$days."','".$available_time."','pending')")or die('Error: There was error while submitting the schedule, please try again.');
you will get $_POST['weekday'] as an array. you can use it like
$_POST['weekday'][0];
$_POST['weekday'][1];
$_POST['weekday'][2];
$_POST['weekday'][3];
$_POST['weekday'][4];
$_POST['weekday'][5];
$_POST['weekday'][6];
For searching you should implode(',' , $_POST['weekday']) and use
$sql = "select * from table where day in ('" . implode("','" , $_POST['weekday']) . "')";
in sql query
You can do like this:
$arr = array();
// check for CHECKED checkboxes
for(var $i = 0; $i > count($_POST['weekday']); $i++){
// if this checkbox is checked
if (isset($_POST['weekday'][$i])) {
$arr[] = $_POST['weekday'][$i];
}
}
// convert to comma separated
$checkbox_str = implode(',', $arr);
Now you can use $checkbox_str to save in the database.
Related
I want to find the group of checkbox that user tick the most to something else
please help me!
Thanks.
Here my HTML
<form method="post">
<input type="checkbox" name="group_1[]" value="V1"/>V1
<input type="checkbox" name="group_1[]" value="V2"/>V2
<input type="checkbox" name="group_1[]" value="V3"/>V3
<br>
<input type="checkbox" name="group_2[]" value="V4"/>V4
<input type="checkbox" name="group_2[]" value="V5"/>V5
<input type="checkbox" name="group_2[]" value="V6"/>V6
<br>
<input type="checkbox" name="group_3[]" value="V7"/>V7
<input type="checkbox" name="group_3[]" value="V8"/>V8
<input type="checkbox" name="group_3[]" value="V9"/>V9
<br><br>
<input type="submit" name="submit" />
</form>
PHP
<?php
if (isset($_POST['submit'])) {
$group_1 = count($_POST['group_1']);
$group_2 = count($_POST['group_2']);
$group_3 = count($_POST['group_3']);
if ($group_1 > $group_2 and $group_1 > $group_3) {
echo "Group 1 is highest ";
}
elseif ($group_2 > $group_1 and $group_2 > $group_3) {
echo "Group 2 is highest ";
}
elseif ($group_3 > $group_1 and $group_3 > $group_2) {
echo "Group 3 is highest ";
}
}
?>
i'm new in php so I code "If Else" but i don't want to use this. and one more i don't want to code specific like "$group_1 = count($_POST['group_1']);" i just want to define "name of checkbox" to get value.
all i want is get same result but different code.
I think you could use the max() function to get highest value
Please refer to http://php.net/max
Probably like this:
<?php
if (isset($_POST['submit'])) {
$group_1 = count($_POST['group_1']);
$group_2 = count($_POST['group_2']);
$group_3 = count($_POST['group_3']);
$array = array("Group 1"=>$group_1,"Group 2"=>$group_2,"Group 3"=>$group_3);
$maxIndex = array_search(max($array), $array);
echo $maxIndex;
}
?>
i m trying to store checkbox values inside variable $days but when i store this value to database it just store the last selected checkbox inside the database... please help me to sort out the code so i can get all selected values
<?php
if (isset($_POST['days'])) {
foreach ($_POST['days'] as $day) {
$days = " " . $day;
}
} else {
$days = "not available";
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<tr>
<td>Select Days :</td>
<td>
<input type="checkbox" name="days[]" value="Sunday">Sunday
<input type="checkbox" name="days[]" value="Monday">Monday
<input type="checkbox" name="days[]" value="Tuesday">Tuesday
<input type="checkbox" name="days[]" value="Wednesday">Wednesday
<input type="checkbox" name="days[]" value="Thursday">Thursday
<input type="checkbox" name="days[]" value="Friday">Friday
<input type="checkbox" name="days[]" value="Saturday">Saturday
<input type="checkbox" name="days[]" value="All Days">All Days
</td>
<td></td>
You assign a normal string to $days and overwrite it on each iteration. You could append to it, by using the .= operator. ($days .= ' ' . $day), but maybe easier is to use implode:
if (isset($_POST['days'])) {
$days = implode(' ', $_POST['days']);
} else {
$days = "not available";
}
Note there is a small functional difference. implode will add spaces inbetween the days, while the foreach loop will also put a space at the start.
You overwrite $days with each iteration of the forech loop. Instead you want to add to it. Most likely this is what you are looking for:
if (isset($_POST['days'])) {
foreach ($_POST['days'] as $day) {
$days .= " " . $day;
}
} else {
$days = "not available";
}
If so, then you can even simplify the code and remove the loop:
if (isset($_POST['days'])) {
$days = implode(" ", $_POST['days']);
} else {
$days = "not available";
}
Use . to concatate the string
$days .= " " . $day;
You need to make $days as array and then push the values you get from checkbox into array.
$days[] = $day
inside your foreach statement
I have a form with two fieldsets which contains checkboxes:
<fieldset style="width:300px; height:200px; overflow:scroll;">
<input type="checkbox" name="table[]" id="01" value='ado'> Adoption <br />
<input type="checkbox" name="table[]" id="02" value='acc'> Accomodations <br />
<input type="checkbox" name="table[]" id="03" value='ann'> Announcements <br />
<input type="checkbox" name="table[]" id="04" value="bea"> Beauty/Fitness <br />
<input type="checkbox" name="table[]" id="05" value="bus"> Business Oportunities
</fieldset>
and this one
<fieldset style="width:300px; height:200px; overflow:scroll;">
<input type="checkbox" name="State[]" id="01" value='AL'> Alabama <br />
<input type="checkbox" name="State[]" id="02" value='AK'> Alaska<br />
<input type="checkbox" name="State[]" id="03" value='AZ'> Arizona<br />
<input type="checkbox" name="State[]" id="04" value='AR'> Arkansas <br />
<input type="checkbox" name="State[]" id="05" value='CA'> California <br />
</fieldset>
Im using this code to go into their respective tables
$table = $_POST['table'];
$name = $_POST['name'];
$state = $_POST['State'];
if(is_array($table)){
while(list($tables) = each($table)){
$sql2 = "INSERT INTO tableName (name,table) VALUES ('$name','$tables')";
$q2 = mysqli_query($db_conx,$sql2);
}
}
if(is_array($state)){
while(list($key,$value) = each($state)){
$sql3 = "INSERT INTO states (name,State) VALUES ('$name','$value')";
$q3 = mysqli_query($db_conx,$sql3);
}
}
when it gets executed the only data that gets entered is states
I used
echo "table; ".$table."<br /> State; ".$state;
and got
table; Array
State; Array012ALAKAZ
someone help me!
You are vulnerable to sql injection attacks.
And your table query is using a reserved word, so the entire insert query is failing. Since you failed to check for failure, and simply assumed success, you'll never see any error messages.
Never EVER assume success when dealing with an external resource (especially a database). There's exactly ONE way for a query to succeed, and a near infinite number of ways for it to fail. Yet you seem to think that 1:infinity odds are really good.
$sql2 = "INSERT INTO tableName (name,`table`) VALUES ('$name','$tables')";
^-----^---you need these
$q2 = mysqli_query($db_conx,$sql2) or die(mysqli_error($db_conx));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---you also need this
Here you have solution what makes only 2 queries instead of 20 and so queries:
$tables = $_POST['table'];
$name = $_POST['name'];
$states = $_POST['State'];
$states_values = '';
$tables_values = '';
$i = 0;
foreach($states as $state)
{
$i++;
$last = $i == count($states) ? true : false;
$states_values .= '(' . $name . ', ' . $state . ')' . ($last ? '' : ',');
}
$i = 0;
foreach($tables as $table)
{
$i++;
$last = $i == count($tables) ? true : false;
$tables_values .= '(' . $name . ', ' . $table . ')' . ($last ? '' : ',');
}
mysqli_query($db_conx, 'INSERT INTO states (name, State) VALUES ' . $states_values;
mysqli_query($db_conx, 'INSERT INTO tableName (name, table) VALUES ' . $tables_values;
As Marc said, you should escape your inputs.
I have multiple checkboxes in my websites:
checkbox 1
checkbox 2
checkbox 3 etc.
I want to dynamically generate mysql query based on the above checkboxes.
i:e if 1st checkbox is selected then the query should be:
$mysql="Select * from mytable where colname=" . $checkbox1 .;
if 1st and 2nd checkbox is selected then the query should be:
$mysql="Select * from mytable where colname=" . $checkbox1 ."AND colname=" . $checkbox2 ." ;
if all are selected then it should be :
$mysql="Select * from mytable where colname=" . $checkbox1 . "AND colname=" . $checkbox2 ."AND colname=" . $checkbox3 . ;
Can someone pls help:
you have to change your form like follow because its taking multiple value its should be post as an array
<form action="register.php" method="POST">
<input type="checkbox" name="rating[]" value="5">5 Star
<input type="checkbox" name="rating[]" value="4">4 Star
<input type="checkbox" name="rating[]" value="3">3 Star
<input type="checkbox" name="rating[]" value="2">2 Star
<input type="checkbox" name="rating[]" value="1">Less than 2 Star
</form>
Then in php
$where = '';
if(isset($_POST['rating'])){
$data = implode(',',$_POST['rating']); // beacuse your rating is only one column in db i think
$where = "WHERE cloumn_name IN($data)";
}
$query = "SELECT * FROM your_table $where";
You can use string concatenation with a bit of trickery to get the job done. do not rely on isset, instead use !empty.
<form action="example.php" method="post">
<input type="checkbox" name="Col1" value="colname" />Col 1</br>
<input type="checkbox" name="Col2" value="colname" />Col 2</br>
<input type="checkbox" name="Col3" value="colname" />Col 3</br>
</form>
<?php
if(!empty($_POST)) {
$string = '';
if(!empty($_POST['col1'])) {
$string.= "column1 ='".$_POST['col1']."'";
}
if(!empty($_POST['col2'])){
if(!empty($string)) {
$string.=" AND ";
}
$string.= "column2 ='".$_POST['col2']."'";
}
if(!empty($_POST['col3'])){
if(!empty($string)) {
$string.=" AND ";
}
$string .= "column3 ='".$_POST['col3']."'";
}
if(!empty($string))
{
//execute your query here.
}
}
Something about it
<? if ($_POST[option]==1) {
//query
}
?>
<form method="post">
<input type="checkbox" name="option" value="1">1
<input type="checkbox" name="option" value="2">2
<input type="checkbox" name="option" value="3">3
</form>
Just check if your Checkboxes are selcted via (isset($_POST[checkbox1]))
So you could do something like
if (isset($_POST[checkbox1])
{
//statement
}
else if (isset($_POST[checkbox2])
{
//statement2...
}
...
Read more here
Cheers
It should be apparent from the code provided below that I'm certainly no PHP coder. But anyway, here's an idea to think about...
Try it with different values for input between 0 and 127, e.g. ?input=66
<?php
if(isset($_GET['input'])){
$input = $_GET['input'];
}else{
$input=0;
}
$days = array('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun');
for( $i=0; $i<7; $i++ ) {
$daybit = pow(2,$i);
if( $input & $daybit ) {
echo "<input type = checkbox checked/>$days[$i]";
}else{
echo "<input type = checkbox>$days[$i]";
}
}
?>
I have a string $eventdays which holds information regarding which days are selected.
The format of the data is:
Monday = Mo
Tuesday = Tu
Wedneday = We
Thursday = Th
Friday = Fr
Saturday = Sa
Sunday = Su
Mo,Tu,We,Th,Fr,Sa,Su
So if for example Tuesday and Friday were selected, the string would be:
Tu,Fr
If Monday, Wednesday, and Saturday were selected it would be:
Mo,We,Sa
Note: Any combination of days can be selected.
I was wondering how to get this information, and preselect checkboxes. The checkboxes are:
<input type="checkbox" name="days[]" value="Mo" />Monday<br />
<input type="checkbox" name="days[]" value="Tu" />Tuesday<br />
<input type="checkbox" name="days[]" value="We" />Wednesday<br />
<input type="checkbox" name="days[]" value="Th" />Thursday<br />
<input type="checkbox" name="days[]" value="Fr" />Friday<br />
<input type="checkbox" name="days[]" value="Sa" />Saturday<br />
<input type="checkbox" name="days[]" value="Su" />Sunday<br />
I know how to preselect a checkbox (checked = "yes"), but my question is how can I parse the string and then select the correct checkboxes from that information?
You can use strpos and dynamically generate your checkboxes.
$eventdays = "Tu,Fr"; // Selected days
$days = array( "Monday" => "Mo",
"Tuesday" => "Tu",
"Wedneday" => "We",
"Thursday" => "Th",
"Friday" => "Fr",
"Saturday" => "Sa",
"Sunday" => "Su"
);
foreach ($days AS $day => $shortDay) {
// Is there an event on this day?
$checked = strpos($eventdays, $shortDay) !== FALSE ? "checked='checked'" : "";
// Generate checkbox HTML
echo "<input type='checkbox' name='days[]' value='{$shortDay}' {$checked} />{$day}<br />";
}
Output
<input type='checkbox' name='days[]' value='Mo' />Monday<br />
<input type='checkbox' name='days[]' value='Tu' checked='checked'/>Tuesday<br />
<input type='checkbox' name='days[]' value='We' />Wedneday<br />
<input type='checkbox' name='days[]' value='Th' />Thursday<br />
<input type='checkbox' name='days[]' value='Fr' checked='checked'/>Friday<br />
<input type='checkbox' name='days[]' value='Sa' />Saturday<br />
<input type='checkbox' name='days[]' value='Su' />Sunday<br />
Assuming your input is a string with line-breaks... First process your data into an keyed array to make life easier... I've used regexes to make it more robust against formatting changes.
$eventdays="Monday = Mo
Tuesday = Tu
Wedneday = We
Thursday = Th
Friday = Fr
Saturday = Sa
Sunday = Su";
$lines = explode("\n", $eventdays);
$data = array();
foreach ($lines as $line) {
if (preg_match("/(\w+)\s*=\s*(\w+)/", $line, $match)) {
$data[] = array('value'=>$match[2], 'label' => $match[1]);
}
}
Now just iterate over the structure printing out the keys / labels. Use the in_array function to check if the current one should be selected. Also I used checked="checked" which is the standards-compliant way of selecting checkboxes... See this question.
$selected_test="Mo,We,Sa";
$select=explode(",", $selected_test);
foreach ($data as $datum) {
$checked="";
if (in_array($datum['value'], $select)) {
$checked = " checked=\"checked\"";
}
echo <<< EOF
<input type="checkbox" name="days[]" value="{$datum['value']}"$checked/>{$datum['label']}<br />\n
EOF;
}
Output
<input type="checkbox" name="days[]" value="Mo" checked="checked"/>Monday<br />
<input type="checkbox" name="days[]" value="Tu"/>Tuesday<br />
<input type="checkbox" name="days[]" value="We" checked="checked"/>Wedneday<br />
<input type="checkbox" name="days[]" value="Th"/>Thursday<br />
<input type="checkbox" name="days[]" value="Fr"/>Friday<br />
<input type="checkbox" name="days[]" value="Sa" checked="checked"/>Saturday<br />
<input type="checkbox" name="days[]" value="Su"/>Sunday<br />
use explode(), it returns an array
$days = "mon,tu";
$needstobeselected= explode(",", $days);
then you have an array with the days that has to be checked. you can then make a loop on the array and write your logic.
i guess crappy method, but build a $checkeddays array, like:
[0] => 'checked',
[1] => ' ',
and so on, then use it :
echo '<input type="checkbox" name="days[]" value="Mo" '.$checkeddays[0].'/>Monday<br />'
the main part is explode.