Radio buttons and IF statements - php

I have a form of radio buttons, I want some code to run that depends on which radio button is chosen. Originally I did this by making the "name" different for each choice, but it causes problems as the names of each radio button should be the same.
Here's a form code example
<li><label for="vsat">YES</label><input type="radio" name="camp" id="vsat" value="vsat" ></li>
<li><label for="vsat2">NO</label><input type="radio" name="camp" id="vsat2" value="vsat2" ></li>
So there's two options, yes or no. I want it to echo "You chose YES" if yes is chosen and "You chose NO" if no is chosen.
Here's how I'm trying to do it
if(isset($_POST['vsat'])){
echo "You chose YES";
}elseif(isset($_POST['vsat2'])){
echo "You chose NO";
}
Make sense? I'm not even sure if it is possible the way I'm trying

You should be checking the value of the $_POST['camp'] variable:
if ($_POST['camp']) == 'vsat') {
echo "You chose YES";
} elseif ($_POST['camp'] == 'vsat2') {
echo "You chose NO";
}
The names of the variables are passed as $_POST variables when you submit the form, not the IDs.

The value of the selected radio button ('vsat' or 'vsat2') will be assigned to $_POST['camp'], while 'camp' is the name of the radio-buttons:
if (isset($_POST['camp']) && $_POST['camp']=='vsat') {
echo "You chose YES";
} elseif (isset($_POST['camp']) && $_POST['camp']=='vsat2') {
echo "You chose NO";
}
PS: the isset() is there to prevent a NOTICE popping up if none of the two radio buttons is checked. $_POST['camp'] will be undefined in this case.

I think you need change your code like this:
if ($_POST['camp'] == 'vsat') {
echo "You chose YES";
}
elseif ($_POST['camp'] == 'vsat2') {
echo "You chose NO";
}
Becouse the value you will check in radio-button-group will be sent to your PHP-script within variable $_POST['camp'] ('camp' is the name-parameter of radio-button). It will contain value 'vsat' or 'vsat2'.

this php code
<?php
if(isset($_POST['submit_btn'])){
echo $_POST['camp'];
if($_POST['camp']=='vsat'){
echo "You chose YES";
}else{
echo "You chose NO";
}
}
?>
this is form
<form method="post" action="">
<li><label for="vsat">YES</label><input type="radio" name="camp" id="vsat" value="vsat" ></li>
<li><label for="vsat2">NO</label><input type="radio" name="camp" id="vsat2" value="vsat2" ></li>
<li><input type="submit" name="submit_btn" /></li>
</form>

Related

Input checkbox when i edit user no change

When i want to edit my user on inactive is it no working, but when i editing my users to inactive to active, it is working.
This is my PHP code: https://pastebin.com/iBaDxH2u
<input class="form-control" type="checkbox" name="actif" id="actif" value="<?php echo $userinfo['actif']; ?>" <?php if ($userinfo['actif'] == "1") { echo "checked"; } ?>>
<input type="hidden" name="actif" value="1" />
I dont solve the problem...
Thx
This sounds like it has something to so with how checkboxes work. When you submit the form, if the checkbox is ticked then the page you submit the form to will receive [actif] => on. If you submit with the checkbox unticked then the page will not receive [actif] => off, it will receive an empty array []. actif will not be set. Something like this might make it more obvious what is going on.
<?php
if (isset($_GET['actif']) && $_GET['actif']=="on")
{
echo ("The box was ticked");
$ticked = 'checked';
}
else
{
echo ("The box was not ticked");
$ticked = '';
}
echo ("<pre>");
print_r($_GET);
?>
<form>
Actif <input type='checkbox' name='actif' id='actif' <?=$ticked?>>
<button type='submit'>Click me</button>
</form>

How to validate a checkbox input using PHP?

I have two check boxes in my form:
<div class="label-input-wrapper pickup">
<div class="form-label">I need Airport pick-up</div>
<div class="form-input">
<input type="checkbox" name="pick_up" value="Yes" />Yes
<input type="checkbox" name="pick_up" value="No" />No
<div class="error-msg">
<?php if(isset($errors['pick_up_no'])) { echo '<span style="color: red">'.$errors['pick_up_no'].'</span>'; } ?>
</div>
</div>
</div>
Variable that saves the value of the above check boxes: $pickup = $_POST['pick_up'];
Validation:
//Check the airport pickup and make sure that it isn't a blank/empty string.
if ($pickup == ""){
//Blank string, add error to $errors array.
$errors['pick_up_no'] = "Please let us know your airport pick up requirement!";
}
if (($pick_up = Yes) && ($pick_up = No)) {
//if both selected, add error to $errors array.
$errors['pick_up_no'] = "Can not select Yes and No together!";
}
But the above validation just printing Can not select Yes and No together! if both are NOT selected, or if only one selected or even both are selected. It gives same error message for all the selections. Why?
You're presently assigning with a single = sign instead of comparing == with
if (($pick_up = Yes) && ($pick_up = No))
you need to change it to
if (($pick_up == "Yes") && ($pick_up == "No")){...}
while wrapping Yes and No inside quotes in order to be treated as a string.
Edit:
Your best bet is to use radio buttons, IMHO.
Using checkboxes while giving the user both Yes and No options shouldn't be used; it's confusing.
It should either be Yes OR No and not and.
<input type="radio" name="pick_up" value="Yes" />Yes
<input type="radio" name="pick_up" value="No" />No
That is the most feasible solution.
I don't know why you need a checkbox for this (this is what radio button's are supposed to do), but you can do it something like:
if(!isset($_POST['pick_up']) || count($_POST['pick_up']) > 1) {
echo 'please select at least 1 choice';
} else {
echo $_POST['pick_up'][0];
}
Then make your checkbox names:
name="pick_up[]"
Or if you really need to have separate error messages. Do something like this:
$errors['pick_up_no'] = '';
if(!isset($_POST['pick_up'])) {
$errors['pick_up_no'] = "Please let us know your airport pick up requirement!";
} elseif(count($_POST['pick_up']) > 1) {
$errors['pick_up_no'] = "Can not select Yes and No together!"; // weird UX, should have prevented this with a radio button
}
if($errors['pick_up_no'] != '') {
echo $errors['pick_up_no'];
} else {
echo $_POST['pick_up'][0];
}
It is impossible for $_POST['pick_up'] to be both yes and no at the same time since the name is pick_up and not pick_up[] <- pointing to multiple variables. So it will either be yes or no either way because one parameter will overwrite the other. You would have to use javascript to verify only one is checked before submit in that case.
Use array of checkbox field elements;
<input type="checkbox" name="pick_up[]" />Yes
<input type="checkbox" name="pick_up[]" />No
then in your PHP script:
<?php
$pick_ups=$_POST['pick_up'];
$first=isset($pick_ups[0]); //true/false of first checkbox status
$second=isset($pick_ups[1]); //true/false of second checkbox status
?>
Again, as you've already been told, you should use radio-buttons for this purpose! Take into account that $_POST does not send checkbox if it is not set (check with isset($_POST['name'])

PHP Radio Buttons - Keep The Values in The Form after an Error

I'm building a contact page with email, name, etc. in HTML with PHP. I have radio buttons on my contact page as well. If the user submitted their name and checked a radio button but forgot to put an email in, the form processing page will flipped them back to the contact page with an error. I'm able to have my contact keep the values put in for their name (and email if user inputs it), but it does not keep the value checked in the radio button.
I'm new to PHP, so I bet it's a silly error on my part. Here's what I have for my Name Input:
Your Name:
<input type="text" name="name" <?php
if (isset($form['name'])) {
echo 'value="';
echo htmlentities($form['name']);
echo '"';
}
?>/>
I tried to do something similar to my Radio. Here's what I have for my Radio:
Are you New to our Business?<br>
<input type="radio" name="customer" value ="yes" <?php
if (isset($form['customer'])) {
echo 'value="';
echo htmlentities($form['customer']);
echo '"';
}
?>/>
Yes, I am!<br>
<input type="radio" name="customer" value="no"<?php
if (isset($form['customer'])) {
echo 'value="';
echo htmlentities($form['customer']);
echo '"';
}
?>/>
No, I am a returning customer!
I'm storing the values on the user input in an array called $form - that's why I have ($form['name]). I would like to it to continue doing that. Some other responses I have researched simply have an isset without the array part.
Hopefully I've provided enough information... Thanks for your help!
You need to do it like this:
if (isset($form['customer']) && $form['customer'] == "yes") {
echo 'checked="checked"';
}
and
if (isset($form['customer']) && $form['customer'] == "no") {
echo 'checked="checked"';
}
You need to change the if-conditional to the following:
<?php
if (isset($form['customer']) && $form['customer'] == "yes") {
echo 'checked';
}
?>/>
Replace the "yes" with "no" for the No part.

Validating radio buttons in php

I'm new to PHP and trying to write code to test whether or not a user has clicked a radio button in response to a survey question. There are numerous radio buttons. If they haven't clicked on one then I'd like to issue an error to the user. I've tried a couple of approaches, but haven't found anything that works. Here is my current code and the error message I get. For the PHP script, I've tried all of the three following examples:
....
if ($_POST['degree_type'] == "MS"||"MA"||"MBA"||"JD"||"PhD") {
$degree_type = ($_POST['degree_type']);
} else if ($_POST['degree_type'] == null) {
$errors[] = 'Please select a degree type.';
}
if (isset($_POST['degree_type'])) {
$errors[] = 'Please select a degree type.';
} else {
$degree_type= $_POST['degree_type'];
}
if (array_key_exists('degree_type', $_POST)) {
$degree_type = ($_POST['degree_type']);
} else {
$errors[] = 'Please select a degree type.';
}
....
Here is my html, located in the same page and below the PHP.
<table>
<tr>
<td class="span6">What type of degree?</td>
<td class="span6">
<input type="radio" name="degree_type" value="MA"
<?php if (($_POST['degree_type']) == 'MA') {echo 'checked="checked"';} ?>
>MA
<input type="radio" name="degree_type" value="MS"
<?php if (($_POST['degree_type']) == 'MS') {echo 'checked="checked"';} ?>
>MS
<input type="radio" name="degree_type" value="MBA"
<?php if (($_POST['degree_type']) == 'MBA') {echo 'checked="checked"';} ?>
>MBA
<input type="radio" name="degree_type" value="JD"
<?php if (($_POST['degree_type']) == 'JD') {echo 'checked="checked"';} ?>
>JD
</td>
</tr>
ETC....
I get an "undefined index" error on each of the HTML lines referencing a radio button. I understand this might be easier to do in JavaScript, but I don't know much about JS... A detailed response would be much appreciated!
Thanks!
If you're getting an undefined error on the HTML page, just can add an isset() check to the logic where you're printing out the value. E.g.:
<input type="radio" name="degree_type" value="JD" <?php if (($_POST['degree_type']) == 'JD') {echo 'checked="checked"';} ?> >JD
Becomes
<input type="radio" name="degree_type" value="JD" <?php if (isset($_POST['degree_type']) && $_POST['degree_type'] == 'JD') {echo 'checked="checked"';} ?>>JD
An 'undefined index' error in PHP means that you are using an undefined variable in an expression. So for example, when you did:
<?php if (($_POST['degree_type']) == 'MA') {echo 'checked="checked"';} ?>
$_POST['degree_type'] was undefined. There's a couple of different possible reasons why the variables are undefined. I'd have to see the rest of the PHP file to know the exact cause.
One reason could be that the form was not properly submitted. Another reason could be that the expression was evaluated before the form was submitted.
Either way, the code below should work. Note that I'm checking if each field is set before attempting to validate it or compare it's value.
NOTE:
Obviously you have to have a proper HTML doctype, opening and closing body tags etc. The HTML in this example is only the form portion of the page.
<!-- myform.php -->
<form name="my-form" method="POST" action="/myform.php">
<span>What degree do you have?</span>
<label for="bs">BS</label>
<input type="radio" name="degree" id="bs" value="BS" <?php if(isset($degree) && $degree == 'BS') echo 'checked="checked"';?> />
<label for="ma">MA</label>
<input type="radio" name="degree" id="ma" value="MA" <?php if(isset($degree) && $degree == 'MA') echo 'checked="checked"';?> />
<label for="phd">PHD</label>
<input type="radio" name="degree" id="phd" value="PHD" <?php if(isset($degree) && $degree == 'PHD') echo 'checked="checked"';?> />
<span>Which do you like better?</span>
<label for="steak">steak</label>
<input type="radio" name="food" id="steak" value="steak" <?php if(isset($food) && $food == 'steak') echo 'checked="checked"';?> />
<label for="lobster">lobster</label>
<input type="radio" name="food" id="lobster" value="lobster" <?php if(isset($food) && $food == 'lobster') echo 'checked="checked"';?> />
<input type="hidden" name="submitted" value="submitted" />
<input type="submit" name="submit" value="submit" />
</form>
<?php
if (isset($_POST['submitted'])) {
$errors = array();
if (isset($_POST['degree'])) {
$degree = $_POST['degree'];
} else {
$errors[] = 'Please select your degree type.';
}
if (isset($_POST['food'])) {
$food = $_POST['food'];
} else {
$errors[] = 'Please select your food preference.';
}
if (count($errors) > 0) {
foreach($errors as $error) {
echo $error;
}
} else {
echo 'Thank you for your submission.';
}
}
?>
The reason you see these notices is because $_POST['degree_type'] is simply not set. Either by typo, or it just didn't get submitted (because you didn't select any before submitting the form).
Also note,
if ($_POST['degree_type'] == "MS"||"MA"||"MBA"||"JD"||"PhD") {
It doesn't work that way. This will check that $_POST['degree_type'] == "MS" OR: "MS" is truthy (always true) OR "MA" is truthy (always true)... see where I'm heading?
if (in_array($_POST['degree_type'], array("MS", "MA", "MBA", "JS", "PhD")) {
Is a better alternative.
Unrelated:
You should really use <label> elements to markup your labels. Example:
<label><input type="radio" name="degree_type" value="MA"> MA</label>.
This will have MA clickable.
When a form is submitted with no member of a radio button group (defined as the group of radio buttons whose name attributes are the same) selected, the submitted data doesn't include that name at all.
This is why you're getting the "undefined index" error (actually a notice); when you test the value of $_POST['degree_type'], and no radio button named "degree_type" was selected, $_POST['degree_type'] doesn't exist at all.
Fortunately, this simplifies your validation task. By calling array_key_exists('degree_type', $_POST), you can find out whether or not the key is present, and thus whether or not a radio button was selected, without prompting the PHP "undefined index" notice. If the function call returns true, you know that a radio button was selected; otherwise, you know one wasn't, and that's what your validation is trying to determine. Therefore:
if (array_key_exists('degree_type', $_POST)) {
$degree_type = $_POST['degree_type'];
}
else {
array_push($errors, "Please select a degree type.");
};
will cleanly accomplish your task.
//set a default
$degree_type = "";
if (isset($_POST['degree_type'])) {
$degree_type = $_POST['degree_type'];
} else {
$errors[] = 'Please select a degree type.';
}
Then instead of using
if (($_POST['degree_type']) == 'MA')
for your checks, use:
if($degree_type == 'MA')
undefined index means that the key you are using hasn't been initialized. So $_POST['degree_type'] won't appear until after the first time the form is submitted.

Pass a php variable into another php file

i have a php file which stores a variable, and i need to create a submit form which passes this variable.
So the code of the current file is:
if(isset($_POST['Submit'])) {
echo "<pre>";
$checked = implode(',', $_POST['checkbox']);
echo $checked;
}
I have to insert the submit form and on click it goes to another php file, i need to get there the $checked variable.. how can i store this variable?..
Thanks you!
The principle of POSTing is that you can send all the data to the next .php page.
<?php
if ($checked == 1) {
$checked = 'checked="checked"';
}
else {
$checked = '';
}
?>
<form action="POST" method="target_file.php">
<input type="hidden" name="variableA" value="Something I want target_file.php to know" />
<input type="hidden" name="variableB" value="Something else I want target_file.php to know" />
<input type="checkbox" name="gender" value="male" <?php echo $checked ?>" />
</form>
Your target_file.php:
echo "Here I am :), variableA: ".$_POST['variableA'];
echo "Here I am :), variableB: ".$_POST['variableB'];
echo "Here I am :), My gender is: ".$_POST['gender'];
Also, don't trust on checking your field values that are send through the $_POST. Check what every value is and if it meets certain criteria before sub-statements may be executed.

Categories