I'm trying to have an email send on submit, but I can't figure out how to echo the value of a radio box, and the text in the value. Can anyone help me?
Here's my radio box:
<label>
<input type="radio" name="question8" value="0;Not at all" id="question8_1" />
Not at all</label>
<br />
<label>
<input type="radio" name="question8" value="25;Slightly" id="question8_2" />
Slightly</label>
<br />
<label>
Assuming your form was submitted via post,
echo $_POST['question8'];
If for some reason it was submitted via $_GET
echo $_GET['question8'];
To separate the value and text, use explode(";", $value):
$radio = explode(";", $_POST['question8']);
$radioval = $radio[0];
$radiotext = $radio[1];
echo "Total $radioval: Answer: $radiotext";
// Or the same thing tidier, via list() multi-assignment
list($radioval, $radiotext) = explode(";", $_POST['question8']);
And both of these assume your <input> tags actually occur inside a <form> tag as in:
<form action='somepage.php' method='post'>
<label>
<input type="radio" name="question8" value="0;Not at all" id="question8_1" />
Not at all</label>
<br />
<label>
<input type="radio" name="question8" value="25;Slightly" id="question8_2" />
Slightly</label>
<br />
<label>
</form>
You can get the value of the data from the $_GET, $_POST, or $_REQUEST variables ($_REQUEST will do either post or get requests) just use the name that your radio box has:
$value = $_REQUEST['question8'];
echo $value;
Also, of course make sure that the form that the radio boxes are part of is pointed at the correct php script for this to work. (they are part of a form right?)
Related
I have multiple checkboxes with names of adminMeta[], such as:
<input type="checkbox" name="adminMeta[name1]" value="1" />
<input type="checkbox" name="adminMeta[name2]" value="1" />
and so on and I also have text inputs like this too with the same names.
When the data is posted, I am looping through using a foreach loop:
foreach($_POST["adminMeta"] as $a => $b) {
}
inside the loop, I add/update the record in my database depending on whether it exists already or not.
But I am having some issues with checkboxes and knowing whether they are checked or not.
I have tried using if(isset($b)) but that hasn't worked.
How can I tell inside my loop, whether a checkbox is checked or not?
If a checkbox is not checked, then it is not a successful control.
If it is not a successful control, then it won't be included in the form data at all.
If it isn't in the form data, then it won't appear when you loop over the form data.
So
If it is in the form data, then it is checked
Otherwise it is not checked
Normally I'd approach this problem with something along the lines of:
$list_of_checkboxes = [ "name1", "name2" ];
Then generate the form with:
foreach ($list_of_checkboxes as $name) {
?>
<label>
<input type="checkbox"
name="adminMeta[]"
value="<?php echo htmlspecialchars($name); ?>">
<?php echo htmlspecialchars($name); ?>
</label>
<?php
}
Then test the data with:
foreach ($list_of_checkboxes as $name) {
if (in_array($name, $_POST['adminMeta'])) {
# Checked
} else {
# Not checked
}
}
Another approach would be to set hidden inputs before each check with default value of 0:
<input type="hidden" name="adminMeta[name1]" value="0" />
<input type="checkbox" name="adminMeta[name1]" value="1" />
<input type="hidden" name="adminMeta[name2]" value="0" />
<input type="checkbox" name="adminMeta[name2]" value="1" />
Now you will receive the data even if you don't check the checkboxes.
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.
If I make a long story very short, I have a short form I've made (an input, a select, and three checkboxes). I've made a function on a button that can dynamically add multiple instances of this form on my page. It saves it as an array (i.e. the input name is name="checkbox[]") which will save fine in my database. The problem I run into is I may have 6 instances of this form, but only some of the boxes are checked. So, I may have 6 text inputs, 6 select inputs, but maybe only 3 checkbox inputs. Since it only has 3 inputs, the array is only 3 pieces of data and when I run a for() statement it doesn't accurately save this information and tie it to the correct record.
I thought that maybe I could have a hidden input that will get its value assigned through javascript, but I don't know how to reference the checkboxes appropriately (you can't do id="blahblah[]" right?)
Sad and Confused,
ImmortalFirefly
I am not sure I have caught your drift on this one, but consider this:
<?php
var_dump( $_POST )
?>
<form name=form0 method= post action = "">
<input type=checkbox name=checkbox[0][0] />
<input type=checkbox name=checkbox[0][1] />
<input type=checkbox name=checkbox[0][2] />
<input type = submit>
</form>
Then another form is added
<form name=form1 method= post action = "">
<input type=checkbox name=checkbox[1][0] />
<input type=checkbox name=checkbox[1][1] />
<input type=checkbox name=checkbox[1][2] />
<input type = submit>
</form>
Mock that up in html and POST it back to a webpage and see how it works, you can iterate through th post value to see which form was sent and which box checked, or put it all in one single form.
<?php
var_dump( $_POST )
?>
<form name=form0 method= post action = "">
<input type=checkbox name=checkbox[0][0] />
<input type=checkbox name=checkbox[0][1] />
<input type=checkbox name=checkbox[0][2] />
<input type = submit>
Then another series of checkboxes is added :
<input type=checkbox name=checkbox[1][0] />
<input type=checkbox name=checkbox[1][1] />
<input type=checkbox name=checkbox[1][2] />
close off the form
<input type = submit>
</form>
I'm building a form with the possibility to add more group of fields, to process them i read out the array in a for loop
the script:
<?php
foreach ($_POST as $key => $value) {
$$key = $value;
}
$count = count($name);
for ($i=0; $i<$count; $i++){
?>
<strong><?php echo $name[$i]; ?></strong> (<?php echo $check[$i]; ?>)<br /><?php echo $select[$i]; ?><br /><br />
<?php
}
?>
<form method="post">
<div class="group">
<input type="text" name="name[]" /><br />
<input type="checkbox" name="check[]" value="true" /><br />
<select name="select[]"><option>1</option><option>2</option><option>3</option></select>
</div>
<div class="group">
<input type="text" name="name[]" /><br />
<input type="checkbox" name="check[]" value="true" /><br />
<select name="select[]"><option>1</option><option>2</option><option>3</option></select>
</div>
<div class="group">
<input type="text" name="name[]" /><br />
<input type="checkbox" name="check[]" value="true" /><br />
<select name="select[]"><option>1</option><option>2</option><option>3</option></select>
</div>
<button>Add another group</button>
<input type="submit" />
</form>
If all checkboxes are checked there is no problem but if only the last one is checked it counts only one checkbox in the array, name[0] is then combined with check[0] but check[0] is really check[2]. English is not my native language so i don't know the right words.
Actually, there is a decent workaround for this as proposed by Sam in this answer on Stack Overflow:
Post the checkboxes that are unchecked
It worked for me, and I suspect you and I had a similar problem (mine being that I had/have upwards of 300 input fields in similar(ish) groups and didn't want to write validation rules for every one of those individual fields, just rules targetted at each family of input types e.g. the email addresses, or the postcodes. In brief, the technique is that you place a hidden input field, with the same name, before your checkbox field. Setting the value of the hidden field (type='hidden') to '0' will ensure that at least one key/value appears in your POST array, with the '0' being superceded by a later '1' only if the box is checked. I needed the '0' value to allow people to 'unset' an option they had previously 'set', for example that they were willing to show their contact data. This technique allows me to present the user with much the same form for an update as they would get at at first registration. Thanks to Sam!
That's normal PHP behaviour, when a checkbox is not checked it does not includes it in $_POST variable ...
Yes. That's how it is. There is no workaround for this. Using field[] identifiers is only applicable for unstructured input fields. If you depend on the ordering and relation, then unset fields will prevent this from working.
You have no other option but to set explicit indexes. You should bite into the sour apple and do so for name[0], check[1] and select[2]. Use a PHP loop to simplify it:
foreach (range(0,2) as $i)
echo <<< END
<div class="group">
<input type="text" name="name[$i]" /><br />
<input type="checkbox" name="check[$i]" value="true" /><br />
<select name="select[$i]"><option>1</option><option>2</option><option>3</option></select>
</div>
END
I was having the same exact problem with some parts of a form that can be add on user's wish. I came to this really simple workaround using basic javascript:
Add a hidden type input just after your checkbox, assign its value to the state of the checkbox and it's this hidden input that will be reported in your $_POST, will be true or false.
<input type="checkbox" onchange="this.nextSibling.value = this.checked">
<input type="hidden" name="state[]" value="false">
Just change value to true if your checkbox is checked="checked" by default.
I just want to have php determines whether a checkbox is checked, but I am running into a problem of getting the right return. Help please.
My html code
<label>
<input name="Language" type="checkbox" id="aa" checked="checked" />
One</label>
<label>
<input name="Language" type="checkbox" id="bb" />Two</label>
<label>
<input type="checkbox" name="Language" id="cc" />Three</label>
I pass the values to php by the $_GET method
my php code:
$aa=$_GET["aa"];
$bb=$_GET["bb"];
$cc=$_GET["cc"];
echo $aa."<br>";
echo $bb."<br>";
echo $cc."<br>";
the output is
true
false
false
I next want to just determine if each box is checked and if so, do something.
if ($aa == true) { $abc="checked";}
else { $abc="not checked"; }
if ($bb == true) { $cde="checked";}
else { $cde="not checked"; }
if ($fgh == true) { $fgh="checked";}
else { $fgh="not checked"; }
But the if statements always return true, even if the box is not checked. I tried variations of "===" and "!=", but it does not seem to work.
TIA
if (isset($_GET['checkbox_name'])) { ... }
Form controls (with the exception of file inputs, and with some special rules for image inputs) always submit strings. There is no concept of a boolean in a query string or a form encoded POST body.
The id is irrelevant — only the name and value matter (at least as far as PHP is concerned).
Since you haven't given them values they will, IIRC, default to on so if you are doing a comparison you should look for that. Looking with isset is simpler though. This is somewhat beside the point though, since your example gives them all the same name and value, so you can't differentiate between them.
Additionally, due to an oddity of the PHP form data parser, you have to end the with [] if you want multiple elements with the same name.
You probably want to do something like this:
<label>
<input name="Language[]" type="checkbox" id="aa" checked="checked" value="One" />
One</label>
<label>
<input name="Language[]" type="checkbox" id="bb" value="Two" />Two</label>
<label>
<input type="checkbox" name="Language[]" id="cc" value="Three" />Three</label>
Important: Note the addition of values and the change in name.
Then in PHP $_GET['Language'] will be an Array of the values of the checked checkboxes, which you can loop over.
Try isset()
I think your HTML code should be like
<label>
<input name="Language[]" type="checkbox" id="aa" checked="checked" value ="1" />One
</label>
<label>
<input name="Language[]" type="checkbox" id="bb" value ="2" />Two
</label>
<label>
<input name="Language[]" type="checkbox" id="cc" value ="3" />Three
</label>
and then by using something like
$count = count($_GET["Language"]); you can count the number of checkboxes checked.
Or do
$arr = $_GET["Language"]; //$arr is an array that contains the values of the checked checkboxes
Then you can foreach over the array
foreach( $arr as $item)
{
echo $item . "</br>"; /* Will print 1,2 and 3 (mind newlines)*/
}
In my PHP I got it from $_POST['checkbox_name'], and I found that the variable had the value on when the box was checked (i.e. it existed even if the checkbox was clear).