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
Related
I was trying to display the results of this form where when you checkbox a fruit, it shows that fruit, then on the last one it puts a "." period instead of a comma.
It comes down to something in my foreach loop that is causing this.
<span style="font-family: arial,helvetica,sans-serif; font-size: 12px;"><?php
$fruitArray = array('orange', 'apple', 'grapefruit', 'banana', 'watermelon');
if(isset( $_POST['fruit']))
{ $values = array();
$i = 0;
$length = count($_POST['fruit']);
echo 'Checkbox checked: ';
foreach ($_POST['fruit'] as $selection) {
if ($i == 0) {
//first to show
echo $selection . ' , ';
} else if ($i == $length - 1) {
//last to show
echo $selection . '.';
}else{
echo $selection . ' , ';
$i++;
}
}
}
I also was trying this:
foreach ($_POST['fruit'] as $selection) {
echo reset($selection) . " , ";
echo current($selection) . " , ";
echo end($selection) . " . ";
}
?>
<html>
<head>
<title>Checkbox selection using PHP (using PDO) and MySQL v2</title>
</head>
<body>
<h2>Pick your most favourite fruits:</h2>
<form name="fruitcheckbox" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="checkbox" name="fruit[]" value="orange"> Orange
<input type="checkbox" name="fruit[]" value="apple"> Apple
<input type="checkbox" name="fruit[]" value="grapefruit"> Grapefruit
<input type="checkbox" name="fruit[]" value="banana"> Banana
<input type="checkbox" name="fruit[]" value="watermelon"> Watermelon
<br>
<input type="submit" value="Save" name="btn_save">
</form>
</body>
</html> </span>
I added in here also if you only have 1 row, this is what you put in here:
$i = 0;
$length = count($_POST['fruit']);
//if one row
if ($length == '1'){
foreach ($_POST['fruit'] as $selection) {
echo 'Checkbox checked: '. $selection . '.';
echo 'Rows counted ' . $length;
}
}else{
echo 'Checkbox checked: ';
foreach ($_POST['fruit'] as $selection) {
echo $selection; //current fruit
if (each($_POST['fruit'])){
echo ", "; //current rows put a comma after fruit
}else if (!each($_POST['fruit'])) {
echo ".";//last row put period instead of comma
}
}
echo 'Rows counted ' . $length;
}
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 was trying to make a COMPUTERIZED ORDERING SYSTEM. My problem is how can I compute the 1st value on my checkbox. The second value on the checkbox will be posted on the summary of orders.
Once I've check all those three, it will compute the total amount of the menu and to display the selected menu on the summary of orders. But I can't figure out how can I display the total amount.
Can anybody guide me how to compute the total on my 1st value on the checkbox?
<form method="post">
<input name='ts[]' type='checkbox' value='40 |Tosilog'/> Tosilog
<br>
<input name='cs[]' type='checkbox' value='40 |Chiksilog'/>Chiksilog
<br>
<input name='ps[]' type='checkbox' value='45 |Porksilog'/>Porksilog
<br>
<input type="submit" name="go" value= "Total">
</form>
<?php
//tosilog
$ts = $_POST['ts'];
$value = explode("|",$ts[0]);
echo $value[0];
echo $value[1];
//chiksilog
$cs = $_POST['cs'];
$value = explode("|",$cs[0]);
echo $value[0];
echo $value[1];
//porksilog
$ps = $_POST['ps'];
$value = explode("|",$ps[0]);
echo $value[0];
echo $value[1];
?>
<!-- compute for the 1st value on checkbox -->
<?php
$ts=$_POST['ts[0]'];
$cs=$_POST['cs[0]'];
$ps=$_POST['ps[0]'];
?>
<?php $compute = $ts[0] + $cs[0] + $ps[0];?>
<?php echo "$compute " ; ?>
If you're getting an array of checkbox elements, and they are numeric, you can use array_sum(). Not sure I understand your suggested structure, but I'll give you a code sample here based on the existing form structure. Then I'll post another bit to try to make this simpler for you. Executive summary: You do not need all the variables that are created by this form structure.
<?php // RAY_temp_user193.php
error_reporting(E_ALL);
$total = 0;
$inputs = array();
$errors = array();
if (!empty($_POST))
{
if (!empty($_POST['ts']))
{
foreach ($_POST['ts'] as $ts)
{
$inputs[] = current(explode(' |', $ts));
}
}
else
{
$errors[] = 'Tosilog';
}
if (!empty($_POST['cs']))
{
foreach ($_POST['cs'] as $cs)
{
$inputs[] = current(explode(' |', $cs));
}
}
else
{
$errors[] = 'Chiksilog';
}
if (!empty($_POST['ps']))
{
foreach ($_POST['ps'] as $ps)
{
$inputs[] = current(explode(' |', $ps));
}
}
else
{
$errors[] = 'Porksilog';
}
// IF ERRORS
if (!empty($errors))
{
echo 'UNABLE TO PRINT COMPLETE TOTAL. MISSING: ' . implode(',', $errors);
}
$total = array_sum($inputs);
if ($total) echo "<br/>TOTAL: $total <br/>" . PHP_EOL;
// END OF THE ACTION SCRIPT
}
// CREATE THE FORM
$form = <<<ENDFORM
<form method="post">
<input name='ts[]' type='checkbox' value='40 |Tosilog'/> Tosilog
<br>
<input name='cs[]' type='checkbox' value='40 |Chiksilog'/>Chiksilog
<br>
<input name='ps[]' type='checkbox' value='45 |Porksilog'/>Porksilog
<br>
<input type="submit" value= "Total">
</form>
ENDFORM;
echo $form;
See http://www.laprbass.com/RAY_temp_miggy.php
This is probably more along the lines of the way I would do it. The script knows what goes into the HTML and it knows exactly what to expect in the POST request. It is easy to add or remove different inputs. Often these kinds of inputs come from a data base table.
<?php // RAY_temp_miggy.php
error_reporting(E_ALL);
$total = 0;
// EXPECTED INPUTS
$inputs = array
( 'Tosilog' => 40
, 'Chiksilog' => 40
, 'Porksilog' => 45
)
;
if (!empty($_POST))
{
// ACTIVATE THIS TO SEE WHAT WAS SUBMITTED
// var_dump($_POST);
// SUM OF THE ELEMENTS
$total = array_sum($_POST);
echo "TOTAL: $total";
// SUM OF THE EXPECTED INPUTS
$expect = array_sum($inputs);
if ($total != $expect) echo " BUT THERE MAY BE INCOMPLETE INPUTS!";
// END OF THE ACTION SCRIPT
}
// CREATE THE FORM
$checkboxes = NULL;
foreach ($inputs as $name => $value)
{
$checkboxes
.= '<input name="'
. $name
. '" type="checkbox" value="'
. $value
. '" />'
. $name
. '<br/>'
. PHP_EOL
;
}
$form = <<<ENDFORM
<form method="post">
$checkboxes
<input type="submit" value= "Total">
</form>
ENDFORM;
echo $form;
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.
I want to be able to separate the birthday from the mysql data into day, year and month.
Using the 3 textbox in html. How do I separate it? I'm trying to think of what can I do with the code below to show the result that I want:
Here's the html form with the php code:
$idnum = mysql_real_escape_string($_POST['idnum']);
mysql_select_db("school", $con);
$result = mysql_query("SELECT * FROM student WHERE IDNO='$idnum'");
$month = mysql_real_escape_string($_POST['mm']);
?>
<?php while ( $row = mysql_fetch_array($result) ) { ?>
<tr>
<td width="30" height="35"><font size="2">Month:</td>
<td width="30"><input name="mo" type="text" id="mo" onkeypress="return handleEnter(this, event)" value="<?php echo $month = explode("-",$row['BIRTHDAY']);?>">
As you can see the column is the mysql database is called BIRTHDAY. With this format:
YYYY-MM-DD
How do I do it. So that the data from the single column will be divided into three parts?
Please help thanks,
You can use list and explode to get desired result
list($year,$month,$day)=explode("-", $row['birthday']);
hence $year contains year, $month contains month and $day contains Day, you can use it like this in your text boxes
<input name="mo" type="text" id="mo" value="<?php echo $month;?>">
<input name="dt" type="text" id="dt" value="<?php echo $day;?>">
<input name="yr" type="text" id="yr" value="<?php echo $year;?>">
$parts = explode('-', '1912-03-22');
$parts = split('-', '1912-03-22'); // could also use split()
echo 'Year: ' + $parts[0] . '<br />';
echo 'Month: ' + $parts[1] . '<br />';
echo 'Day: ' + $parts[2] . '<br />';
You could use php's explode function to split the date into its separate compoents:
$birthday ="1981-06-22";
list($year, $month, $day) = explode("-", $birthday);
echo "\nDay $day Month $month Year $year";