How do I wrap the selections in a session that can be carried over multiple pages?
This is what I have:
session_start();
<form action="checkbox-form.php" method="post">
Select your options<br />
<input type="checkbox" name="options[]" value="A" />A<br />
<input type="checkbox" name="options[]" value="B" />B<br />
<input type="checkbox" name="options[]" value="C" />C<br />
<input type="submit" name="formSubmit" value="Submit" />
</form>
var_dump($_POST['options']);
foreach($_POST['options'] as &$option){
mysql_real_escape_string($option);
}
$insert = "INSERT INTO submitted (statuses)
VALUES ('". implode(",", $_POST['options']) ."')";
$query = mysql_query($insert) or die ("Error: ".mysql_error());
So, how do I put $_POST['options']; in a session. I tried doing
$_SESSION['options'] = mysql_real_escape_string($_SESSION['options']);
When i do that i get "array" as the result.
You are storing a array in session variable and complaining that its returning me a array!!!
Of course it will return an array...to access a particular key of the same use..
$_SESSION['options'][0]
$_SESSION['options'][1]
$_SESSION['options'][2]
which consecutively correspond to your A,B,C checkboxes on the form...
simply do it
session_start();
$_SESSION['options'] = $_POST['options'];
and you can access checkbox value by
$_SESSION['options'][$i] where $i <count($_SESSION['options'])
Related
how to save multiple radio button in database using php without save button value.
my code :
$user_id = $_POST['user_id'];
foreach ( $_POST as $key => $val ) {
if ($key <> 'user_id') {
$bidder_interst_insert="INSERT INTO bidder_interest_list(id, bidder_id, bidder_interest_name) VALUES ('','$user_id','$val')";
$bidder_interst_insert_result = mysql_query($bidder_interst_insert);
if (mysql_affected_rows() > 0) {
$interest_list_success = "Thank you Successfull insert your interst list.";
$_SESSION['interest_list_success_msg'] = $interest_list_success;
} else {
$insert_error = "interst list Insert Error.";
$_SESSION['insert_error_msg'] = $insert_error;
header("location:interest_list.php");
}
}
}
This code work but database extra save in save button value how to solved this problem??
foreach ( $_POST as $key => $val ){
You are directly looping the $_POST, so the SAVE button value is also saving in the database. Take the values individually instead of looping the whole $_POST, then the SAVE button value won't be saved in the database.
And moreover you are using mysql functions which are deprecated, use mysqli or PDO.
EDIT::
Just take it the same way as u took user_id ==> $variablename = $_POST['fieldname'];
EDIT:::
Let me suppose i have a form like this
<form name="form1" id="form1" method="post" action="">
<input type="checkbox" name="products[]" value="A" checked="checked" />A <br />
<input type="checkbox" name="products[]" value="B" checked="checked" />B <br />
<input type="checkbox" name="products[]" value="C" checked="checked" />C <br />
<input type="checkbox" name="products[]" value="D" checked="checked" />D <br />
<input type="checkbox" name="products[]" value="E" checked="checked" />E <br />
<input type="checkbox" name="products[]" value="F" checked="checked" />F <br />
<input type="submit" name="save" id="save" value="Save" />
</form>
then i can do it like:
<?php
if(isset($_POST['save']))
{
$products = $_POST['products'];
foreach($products as $key => $value)
{
$qry = mysql_query("INSERT INTO tbl(product) VALUES('$value')");
}
}
?>
Try this
unset($_POST['name-of-save-button']);
$data = $_POST;
foreach ( $data as $key => $val ){//your code here}
I have html checkbox like this:
<form action="" method="post">
<input type="checkbox" name="language[]" value="php" />PHP<br />
<input type="checkbox" name="language[]" value="html" />HTML<br />
<input type="checkbox" name="language[]" value="java" />Java<br />
<input type="checkbox" name="language[]" value="c++" />C++<br />
<input type="submit" value="send" />
</form>
Now I want to detect the checkbox is not checked using this PHP
if($_POST)
{
if(empty($_POST['language']))
{
echo "bla";
}
else
{
foreach($_POST['language'] as $value)
{
echo 'Checked: '.$value.'
';
}
}
}
The output is always show the checbox checked.
My question is, how can I detect the checkbox is not checked?
Example I do not check PHP and Java.
You don't need to validate checkbox by checkbox in order to determine if they are checked or not, you won't get the unchecked checkboxes values at the time you send the form, so, sending the form like this:
<form action="" method="post">
<input type="checkbox" name="language[]" value="php" />PHP<br /> <!-- checked -->
<input type="checkbox" name="language[]" value="html" />HTML<br /><!-- checked -->
<input type="checkbox" name="language[]" value="java" />Java<br /><!-- unchecked -->
<input type="checkbox" name="language[]" value="c++" />C++<br /><!-- unchecked -->
<input type="submit" value="send" />
</form>
In your PHP, you will get an array as follows:
$_POST['languages'] = array("php", "html");
Now, lets say you have an array of all the values in order to check which ones you need to delete, and which ones you need to add, a rough code example would be as follows:
$allValues = array('php', 'html', 'java', 'c++');
$valuesForAdd = $_POST['language'];
$valuesForDeletion = array_diff($allValues, $valuesForAdd);
First you need the selectable items array in the backend:
$items = array('php','html','java','c++');
You have the posted (selected) languages array here:
$_POST['language']
Not selected languages array:
$not_selected_languages = array_diff($items,$_POST['language']);
I hope it helps.
Only 'checked' checkboxes get sent as parameters in a POST request.
If you want to know which aren't checked, you could have the value list stored on PHP side; then once you receive POST data - compare the array on PHP side with POST array.
$all_vals = array('php', 'c++', 'html', 'java');
$post_vals = $_POST['languages'];
foreach ($post_vals as $post_val)
if in_array($post_val, $all_vals)
$checkbox checked
else
$checkbox not checked
I assume this gives you enough liberty to do what you need.
I want to store the multiple checkbox values to store in a single field. I use that link http://www.mindfiresolutions.com/Storing-array-data-to-MySQL-using-PHP-1296.php. But i dont get the result.Give Help to find that problem.
Set your column as 'set' (specify the all possible values.) data type and than run the below query.
$comma_separated = implode(",", $values);
$insert_query = "INSERT INTO TABLE_NAME(col_name) VALUES('$comma_separated')";
$result_insert = mysql_query($insert_query);
I hope this will solve your problem.
I hope this will helpful
<html>
<body>
<form action="" method="post">
<p><input type="checkbox" name="color[]" value="red" />Red</p>
<p><input type="checkbox" name="color[]" value="blue" />Blue</p>
<p><input type="checkbox" name="color[]" value="orange" />orange</p>
<input type="submit" value="submit" name="sub" />
</form>
<?php
if(isset($_POST['sub']))
{
mysql_connect("localhost","root","") or die("Server Could not be connected");
mysql_select_db("gobinath") or die("database connection problam");
$color=implode(',',$_POST['color']);
mysql_query("insert into mcheck values('','$color')") or die("insert problam");
}
?>
</body>
</html>
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>
I am trying to insert the value of this multiple checklist into the db column. This code not working. Can anyone spot the problem?
My database consists of a table called "colors" and one column called "color".
<?php
// connect to database
require "mysql_connect.php";
?>
<?php
// get value from the form
$color = $_POST['color'];
foreach($_POST['color'] as $colors){
$insert = mysql_query("INSERT INTO colors (color) VALUES ('$color')");
}
?>
<form action="add_color.php" method="post" enctype="multipart/form-data" name="colorform" id="colorform">
<input type="checkbox" name="color[]" value="black" /> Black
<input type="checkbox" name="color[]" value="red" /> Red
<input type="checkbox" name="color[]" value="blue" /> Blue
<input type="checkbox" name="color[]" value="white" /> White
<input name="submit" type="submit" value="Add color" />
</form>
Thanks
This is a nice way to add your colors
<?php
require "mysql_connect.php";
// connect to database
$colors=array();
// get value from the form
if (isset($_POST['color'])) $colors = $_POST['color'];
foreach($colors as $color)
{
mysql_query ("INSERT INTO colors ('color') VALUES ('$color')");
}
?>
<form action="add_color.php" method="post" enctype="multipart/form-data" name="colorform" id="colorform">
<input type="checkbox" name="color[]" value="black" /> Black
<input type="checkbox" name="color[]" value="red" /> Red
<input type="checkbox" name="color[]" value="blue" /> Blue
<input type="checkbox" name="color[]" value="white" /> White
<td><input name="submit" type="submit" value="Add color" />
</form>
if (isset($_POST['color']))
This condition is important because it will prevent an indexing error in case the array is empty
$colors=array();
Also, do declare your variables to prevent getting undeclared varibles, previously, in your code, this will happen if the user does not specify any color
Remember PHP is server-side and thus getting errors on PHP create loopholes for attacks. Try to read about PHP Best Practices, Its very impotant
Hopes it helps :-)
I would also suggest that you sanitize your from inputs before inserting into your database. You don't mention what type your color column is, could be a mismatch there as well.
When you say INSERT INTO $colors -- is that what you mean? Your table name is variable? You should probably have a proper table name in place of $colors.
In addition, you have used $color which I don't see defined, you probably meant to use $colors so it should be more like this:
INSERT INTO tblColors (color) VALUES ('$colors')
To check your return value to see what error you're getting:
$query = "INSERT INTO tblColors (color) VALUES ('$colors')";
$insert = mysql_query($query) or die("A MySQL error has occurred.<br />Your Query: " . $query . "<br /> Error: (" . mysql_errno() . ") " . mysql_error());
$insert = mysql_query("INSERT INTO $colors (color) VALUES ($color)");
Change it to:
$insert = mysql_query("INSERT INTO colors_table_name (color) VALUES ($color)");
Also, please check the return value of insert, maybe you are getting errors?
First obvious problem was that the table name was being replaced with the color because of the variable, is this the desired effect?
<?php
// connect to database
require "mysql_connect.php";
?>
<?php
// get value from the form
$colors = $_POST['color'];
foreach($colors as $color){
$insert = mysql_query("INSERT INTO colors (color) VALUES ($color)");
}
<form action="add_color.php" method="post" enctype="multipart/form-data" name="colorform" id="colorform">
<input type="checkbox" name="color[]" value="black" /> Black
<input type="checkbox" name="color[]" value="red" /> Red
<input type="checkbox" name="color[]" value="blue" /> Blue
<input type="checkbox" name="color[]" value="white" /> White
<td><input name="submit" type="submit" value="Add color" />
</form>
You've got your variables backwards, SQL syntax errors, SQL injection vulnerabilities, and a total lack of error handling
$color = $_POST['color']; <---stuff the POST data array into $color
foreach($_POST['color'] as $colors){ <--- loop over the POST data directly
$insert = mysql_query("INSERT INTO colors (color) VALUES ($color)");
^^^^^^---insert the array
^^^^^^---no quotes
You use $colors (with an S) to store the individual colors, but then insert $color, which is an array.
Never assume that a query has suceeded. If you'd have the bare minimum or die(...) error handling, you've have seen why your queries were failing:
foreach($_POST['color'] as $color) {
$safe_color = mysql_real_escape_string($color);
$result = mysql_query("INSERT INTO colors (color) VALUES ('$safe_color');") or die(mysql_error());
}