PHP post checkbox problem - php

i have a form that utilizes checkboxes.
<input type="checkbox" name="check[]" value="notsure"> Not Sure, Please help me determine <br />
<input type="checkbox" name="check[]" value="keyboard"> Keyboard <br />
<input type="checkbox" name="check[]" value="touchscreen"> Touch Screen Monitors <br />
<input type="checkbox" name="check[]" value="scales">Scales <br />
<input type="checkbox" name="check[]" value="wireless">Wireless Devices <br />
And here is the code that process this form in a external php file.
$addequip = implode(', ', $_POST['check']);
I keep getting this error below;
<b>Warning</b>: implode() [<a href='function.implode'>function.implode</a>]: Invalid arguments passed in <b>.../process.php</b> on line <b>53</b><br />
OK

is any of your checkboxes ticked? php’s $_POST array will only have checkboxes which have been ticked
to silence your warning use this:
$addequip = implode(', ', empty($_POST['check']) ? array() : $_POST['check'] );

the following site seems to be what you need:
http://www.ozzu.com/programming-forum/desperate-gettin-checkbox-values-post-php-t28756.html

Hi I m the original user who posted this question i couldnt login to my account so posting from another account. After couple hours of trying i somehow managed to make it work partially. Below is the modified form html and process code for checkboxes
<input type="checkbox" name="check" value="Touchscreen"> Touchscreen<br>
<input type="checkbox" name="check" value="Keyboard"> Keyboard<br>
<input type="checkbox" name="check" value="Scales"> Scales<br>
I had to remove the [] so it would work. Also below is the entire post method for those would like to see. It works perfectly fine with every other field.
<form id="contact_form" action="process.php" method="POST" onSubmit="return processForm()">
And below is the php code to process checkboxes. For some reason i have to tell script that $_POST['check'] is an array without it would only return array. All other methods suggested returns invalid argument passed error.
$chckbox = array($_POST['check']);
if(is_array($chckbox))
{
foreach($chckbox as $addequip) {
$chckbox .="$addequip\n";
}
}
So this code works but returns only 1 checkbox value that is ticked even no matter how many you ticked.

Related

PHP get checkbox value's separated by comma's

I have a contact form with a couple of checkboxes.
Whenever i submit the form I would like the selected value's to be printed separated by comma's.
<form method="POST">
<input type="checkbox" id="product1" name="product1" value="12">
<input type="checkbox" id="product2" name="product1" value="13">
<input type="checkbox" id="product3" name="product1" value="14">
<button type="submit">Subscribe</button>
</form>
I'm using this form. Lets say product 1 and 2 are selected. Then it should print 12,13 <
What is the best way to do this? Is it even possible?
Thanks in advance.
USE CASE:
So I thought it was useful to post why I need this.
Later when I'm able to get the values I will do something like this:
header("Location: http://test.com/$myvalues");
So this link can be test.com/12,13 after the user submits the form.
Not working:
So im using this code
<?php
if(isset($_POST['product'])){
$values = implode(',',$_POST['product']);
// header("Location: https://test.com/?add-to-cart=$values");
}
?>
Whenever i click on the submit button, it takes me to a page that doesnt exist. So i get a 404 page. Even with the header location commented off.
The header location doesnt seem to work at all.
<form method="POST" action="upload.php" >
<input type="checkbox" id="product1" name="product[]" value="12">
<input type="checkbox" id="product2" name="product[]" value="13">
<input type="checkbox" id="product3" name="product[]" value="14">
<button type="submit" name="products" >Subscribe</button>
</form>
in upload.php
$a=$_POST['product'];
$prodt=implode(',',$a);
$prodt has the value
To consolidate Alive to Die's answer and the comments about XSS by hanshenrik;
Use the array-like syntax in the input name attribute:
<form method="POST">
<input type="checkbox" id="product1" name="product[]" value="12">
<input type="checkbox" id="product2" name="product[]" value="13">
<input type="checkbox" id="product3" name="product[]" value="14">
<button type="submit">Subscribe</button>
</form>
If you check the first two checkboxes and submit, you should receive:
$POST['product'] == ["12", "13"];
Of course any input needs to be validated and escaped before outputting in the response. For the specific use-case you mention, you would end up with something like this:
$products = [];
if (isset($POST['product'])) {
foreach ($POST['product'] as $product) {
if (!is_numeric($product)) {
die("invalid product value.");
}
$products[] = intval($product);
}
}
if (empty($products)) {
die("no products selected.");
}
header("Location: https://test.com/" . implode(",", $products);
Clarification on input validation:
If you skip the input validation, one could submit the following data:
product[]="admin.php" and get redirected to https://test.com/admin.php. Of course that wasn't what you wanted this script to do, so by that principle alone you should consider restricting the possible behavior of your code. But it could get worse:
If you choose to echo implode(",", $POST['product']) somewhere in your website, someone might submit:
product[]="<script>alert(\"vuln\");</script>" which will add JavaScript to your website. An alert isn't dangerous, but this script could be anything. From submitting your session cookies to running a cryptominer in your browser. In principle, if you can alert(), you can do anything.
If you construct your original form from a database, someone might insert malicious inputs as values into the database. So these attacks are not necessarily limited to a single user playing around with their element inspector.
These are a lot of 'ifs' and for a simple local website or PHP script, you don't need to concern yourself with most of these issues. However, if you choose to make any of your code available to the internet, you should never trust user input.
Since you've given every checkbox the same name attribute, you can't. Give them separate names and you can easily handle this client and/or server side.

Error in Radio Button if not checked

Hello I am a beginner in PHP
I have created a form and there are few radio button with YES/NO options... every thing is working fine but if i submit form without clicking radio button options, it shows the following error... I know there is some code to be written but not getting exactly
'Notice: Undefined index: ws_id in D:\xampp\htdocs\mc\db_def.php on line 18'
Solution in your php use isset() to check
Use required in input tag "very basic validation"
<label for="input1">1:</label><input type="radio" name="test" id="input1" required value="1" /><br />
<label for="input2">2:</label><input type="radio" name="test" id="input2" value="2" /><br />
<label for="input3">3:</label><input type="radio" name="test" id="input3" value="3" /><br />
Check this fiddle http://jsfiddle.net/Pw5vQ/
Solution 1:
If you won't check radio button or checkbox the form won't post it so you will not find these inputs on the $_POST superglobal variable.
Try to var_dump($_POST); with checked and unchecked version.
Just check the required form name with isset() like this isset($_POST['ws_id']).
If isset gives you false it means it won't checked.
Solution 2:
You can preset the inputs with html attribute checked="checked".
For example:
You would like to check the NO radio button for default:
<input type="radio" name="ws_id" value="YES"/>Yes
<input type="radio" name="ws_id" value="NO" checked="checked"/>No
I guess you are getting this error when first accessing the page, so there is no sent data, so your $_POST['ws_id'] or $_GET['ws_id'] or $_REQUEST['ws_id'] has no value in it as there is no data sent.
Use
if (isset($_POST['id'])) // do what you were doing on line 18
//or
if (isset($_GET['id'])) // do what you were doing on line 18
//or
if (isset($_REQUEST['id'])) // do what you were doing on line 18
depending on your choice.

submitted forms with the same name to a php script?

Is it possible to submit forms with input checkboxes, each containing the same name, to a PHP script?
Is it possible to loop through the names to get all the values?
I am building a message system, and users can add/remove recipients dynamically. When they do, a hidden checkbox is generated in the form containing the value, yet I'm not sure what to do with the name. On the php end, on top of the recipients a subject and a message are submitted, and the script needs to loop through each name and perform various SQL tasks. I know there are much better ways of doing this, and feel free to suggest, but I'd really like to know if it can get done this way. Comment if you need to see code, but I warn you, it's really confusing.
<input type="checkbox" name="samename[]">
// on the post/get:
foreach( $_POST['samename'] as $eachId ){
// do whatever you want. build the where in a query, ' set = '.$eachId
}
Yes you can, use the same name with [] after it, it will cause all of the values to be stored in an array on PHP.
<input type=checkbox value=1 name=check[]>
<input type=checkbox value=2 name=check[]>
<input type=checkbox value=3 name=check[]>
<input type=checkbox value=4 name=check[]>
<input type=checkbox value=5 name=check[]>
Yes you can, array of post, look at this example:
<?php
print_r($_POST);
?>
<form action="form.php" method="POST">
<input type="checkbox" name="vehicle[]" value="Bike" /> I have a bike<br />
<input type="checkbox" name="vehicle[]" value="Car" /> I have a car
<input type="submit" value="Submit" />
</form>
Notice how vehicle has the square brackets?

PHP storing multiple checkbox in database

i did my 'homework' in searching for a solution to this matter through this forum and googling, and ive tried out various ways in solving this, but i can't seem to get the value to be posted into the action file...
i understand the concept of implode / concatenating the array into a string so that it can be stored in the database, i also understand how data are parsed, but i can't seem to get this done..
PHP Form:
<form name="questionnaire_1" action="test_index.php?action=send_questionnaire" method="post">
<table>
<tr class="row-a">
<td>
2. Position<br />
<i>( You may choose more than one option for this question)</i>
</td>
<td colspan="6">
<label> <input type="checkbox" name="position[]" value="1" /> Project Leader (External) </label><br />
<label><input type="checkbox" name="position[]" value="2" /> Project Leader (Internal)
</label><br />
<label><input type="checkbox" name="position[]" value="3" />Project Member (External)
</label> <br />
<label><input type="checkbox" name="position[]" value="4" /> Project Member (Internal)
</label><br />
<label> <input type="checkbox" name="position[]" value="5" />Others (Please Specify)</label>
<input name="position_specify" type="text" size="40" maxlength="40" />
</td>
</tr>
</table>
PHP Action:
if (isset($_POST['Submit']))
{
$position = mysql_prep($_POST['position']);
$allPosition = implode (",", $position);
}
etc ...
the position_specify input has no problem... only having problem with the checkbox... the database takes in varchar(255) for the position.. i really do not know how to solve this..
much help is appreciated, thank you & God bless!
Possible reference to source of mysql_prep() function: http://forums.devnetwork.net/viewtopic.php?f=1&t=81234 . Note that it ends up passing an array to mysql_real_escape_string() which doubtless converts the array to the string "Array", which is then stored in the database. As miki posted in the comments above, imploding before escaping should solve your problem.
You may have a look at bitwise operators. So in otherwords five the first checkbox the value 1 then rhe second value 2 and 3rd value 4. So count in bits. So when submitting the form add all the values together by adding all the values of the checked checkboxes. And then store ome value in the db.
To check if the checkbox was ticked do the following
If($box & 1){ echo 'checkbox 1 is checked';}

get checked boxes value by post in php

I'm getting database from database and each row has a id and im showing it like this in html
<td><input type="checkbox" value"1">test1</td></tr>
<td><input type="checkbox" value"2">test2</td></tr>
and so on...
now lets say that user checked ten check boxes out of 15 and then clicked submit .
how to get values of those boxes in php???
Your checkboxes need to have a name & a value attribute:
<input type='checkbox' name='test1' value='1'> Test1
<input type='checkbox' name='test2' value='1'> Test2
then when that is posted you can access the values in PHP via $_POST:
$test1 = $_POST['test1']
$test2 = $_POST['test2']
Keep in mind that the values will only be returned if the box is checked, so most likely instead of the above PHP, you're more than likely just going to want to check if the value exists.
give the checkboxes names:
<td><input type="checkbox" value"2" name='test'>test2</td></tr>
Than in php just read the request
$test = $_REQUEST['test']
if the OP doesn't have these checkboxes inside of a , any amount of PHP code will make absolutely no difference (FROM Blender)
they will be in either the $_GET or the $_POST array
Try this way..
<form action="#" method="post">
<input type="checkbox" name="check_list[]" value="1"><label>Test 1</label><br/>
<input type="checkbox" name="check_list[]" value="2"><label>Test 1</label><br/>
<input type="checkbox" name="check_list[]" value="3"><label>Test 3</label><br/>
<input type="submit" name="submit" value="Submit"/>
</form>
<?php
if(isset($_POST['submit'])){//to run PHP script on submit
if(!empty($_POST['check_list'])){
// Loop to store and display values of individual checked checkbox.
foreach($_POST['check_list'] as $selected){
echo $selected."</br>";
}
}
}
?>

Categories