problem accessing check-boxes values in some installations - php

I have following structure for check-boxes
<input type="checkbox" name="reg_field['first_name']" value="1" /> First Name<br />
<input type="checkbox" name="reg_field['last_name']" value="1" /> Last Name<br />
<input type="checkbox" name="reg_field['paypal_email']" value="1"/> PayPal Email<br />
<input type="checkbox" name="reg_field['website_url']" value="1" /> Website URL<br />
<input type="checkbox" name="reg_field['address']" value="1"/> Address<br />
For some installation, I can access these fields as
$reg_new_values = (isset($_POST['reg_field'])?$_POST['reg_field']:array());
echo $reg_new_values["'first_name'"]
but for some installation, it won't work at all?
Is my structure wrong, or do I have to change some settings in PHP.INI

Change the name to something like this reg_field[first_name] instead of reg_field['first_name']. Remove the single quotes, by doing that you can access each field name as:
$FirstName = $_POST['reg_field']['first_name'];

Related

Get variable on Wordpress to PHP

I'm trying to get a value of a form in Wordpress to PHP. The form is like this and it is displaying fine in the preview:
<form action=".../name.php" method="get">
<input type="checkbox" name="form_ques_4" value=0 />
<input type="checkbox" name="form_ques_4" value=1 />
<input type="checkbox" name="form_ques_4" value=2 />
<input type="submit" name="formSubmit" value="submit" />
</form>
If the user selected option 2, the value is 1 and this will later be used as the input in a MySQL database. As I have read in other posts, I should get value with the php line.
$a = $_GET["form_ques_4"];
I have tested some other simple outputs for the .php and there is no problem with the "form action" of the wordpress. I also tried using single and double quotes for the "GET" with no result.
Try to change the names of your checkboxes, if you want a user multiple choice:
<form action=".../name.php" method="get">
<input type="checkbox" name="form_ques_1" value="0" />
<input type="checkbox" name="form_ques_2" value="1" />
<input type="checkbox" name="form_ques_3" value="2" />
<input type="submit" name="formSubmit" value="submit" />
</form>
otherwise, if you want the user makes only one choice use type="radio"
<form action=".../name.php" method="get">
<input type="radio" name="form_ques_4" value="0" />
<input type="radio" name="form_ques_4" value="1" />
<input type="radio" name="form_ques_4" value="2" />
<input type="submit" name="formSubmit" value="submit" />
</form>
EDIT
yes, as AZinkey says, you can also use
<form action=".../name.php" method="get">
<input type="checkbox" name="form_ques[]" value="0" />
<input type="checkbox" name="form_ques[]" value="1" />
<input type="checkbox" name="form_ques[]" value="2" />
<input type="submit" name="formSubmit" value="submit" />
</form>
then get the results in php
$checked = $_GET['form_ques'];
for($i=0; $i < count($checked); $i++){
echo $checked[$i] . "<br/>";
}
Quote your value attribute like value="0" and update name to "form_ques_4[]"
<input type="checkbox" name="form_ques_4[]" value="0" />

PHP var_dump only outputting value of bottom checkbox

Hi and thanks for reading my question. I am using a simple form to get some input :
<p>Select your favorite two countries below:</p>
<form id="world" name="world" action="/order.php" method="post">
<input type="checkbox" name="countries" value="USA" /> USA<br />
<input type="checkbox" name="countries" value="Canada" /> Canada<br />
<input type="checkbox" name="countries" value="Japan" /> Japan<br />
<input type="checkbox" name="countries" value="China" /> China<br />
<input type="checkbox" name="countries" value="France" /> France<br />
<input type="submit" value="Order">
</form>
I want to make sure order.php is geting all of the choices selected, so order.php only contains the following code :
<pre>
<?php var_dump($_POST);?>
</pre>
Unfortunately, it is only outputting whatevre is the bottom-most checkbox that is checked.
The output is like this :
array(1) {
["countries"]=>
string(6) "Canada"
}
If i try the following code for output :
<?php
foreach($_POST as $key=>$post_data){
echo "You posted:" . $key . " = " . $post_data . "<br>";
}
?>
I get this output :
You posted:countries = Canada
Can anyone tell me where i am going wrong and how i can retrieve all of the data, for every box that is ticked ?
Thank you.
You gave the same name to your checkboxes, and PHP will overwrite previously parsed name submissions with the current value. You need to use the array-notation hack:
<input type="checkbox" name="countries[]" value="Canada" /> Canada<br />
^^
which then makes $_POST['countries'] an array of all the values submitted.
echo "You posted: " . implode(',', $_POST['countries']);
<p>Select your favorite two countries below:</p>
<form id="world" name="world" action="/order.php" method="post">
<input type="checkbox" name="countries[]" value="USA" /> USA<br />
<input type="checkbox" name="countries[]" value="Canada" /> Canada<br />
<input type="checkbox" name="countries[]" value="Japan" /> Japan<br />
<input type="checkbox" name="countries[]" value="China" /> China<br />
<input type="checkbox" name="countries[]" value="France" /> France<br />
<input type="submit" value="Order">
</form>
Change it to above, this will store all your checkboxes results for you!

TCPDF - Set check box Read Only

I want to set the check-box as read-only in tcpdf.
I try this
<input type="checkbox" onclick="return false" />
But this is working for html only i mean in tcpdf its not working.
try this for check-box as read-only in tcpdf ... it works for me
<input type="checkbox" name="box" value="1" readonly="true" />
This is an example which should work in your case:
<input type="checkbox" name="agree" value="1" checked="checked" readonly="true" /> <label for="agree">I agree </label><br /><br />

insert checkbox values in array into database

I have 3 checkboxes :
<input class="shoesChoice" name="trendy" type="checkbox" />
<input class="shoesChoice" name="luxury" type="checkbox" />
<input class="shoesChoice" name="sports" type="checkbox" />
And I want to store this information into (if possible) only one field in my database.
So, if the 1rst and the last input are checked, my shoesChoice field should have : "trendy, sports"
Try to change the name and keep them in value
<input class="shoesChoice" name="shoes[]" value="trendy" type="checkbox" />
<input class="shoesChoice" name="shoes[]" value="luxury" type="checkbox" />
<input class="shoesChoice" name="shoes[]" value="sports" type="checkbox" />
In PHP, try to get values in this way:
<?php
if(isset($_REQUEST['shoes']))
{
$shoes = '';
foreach
}
?>

How to handle multiple checkboxes in a PHP form?

I have multiple checkboxes on my form:
<input type="checkbox" name="animal" value="Cat" />
<input type="checkbox" name="animal" value="Dog" />
<input type="checkbox" name="animal" value="Bear" />
If I check all three and hit submit, with the following code in the PHP script:
if(isset($_POST['submit']) {
echo $_POST['animal'];
}
I get "Bear", i.e. the last chosen checkbox value even though I picked all three. How to get all 3?
See the changes I have made in the name:
<input type="checkbox" name="animal[]" value="Cat" />
<input type="checkbox" name="animal[]" value="Dog" />
<input type="checkbox" name="animal[]" value="Bear" />
you have to set it up as array.
print_r($_POST['animal']);
<input type="checkbox" name="animal[]" value="Cat" />
<input type="checkbox" name="animal[]" value="Dog" />
<input type="checkbox" name="animal[]" value="Bear" />
If I check all three and hit submit, with the following code in the PHP script:
if(isset($_POST['animal'])){
foreach($_POST['animal'] as $animal){
echo $animal;
}
}
use square brackets following the field name
<input type="checkbox" name="animal[]" value="Cat" />
<input type="checkbox" name="animal[]" value="Dog" />
<input type="checkbox" name="animal[]" value="Bear" />
On the PHP side, you can treat it like any other array.

Categories