PHP not retrieving radio input value - php

I'm having a real problem with retrieving the value of a radio button using PHP . . . I have two radio buttons as such:
<input name="admin" type="radio" value="1" />Yes
<input name="admin" type="radio" value="0" checked />No
And a conditional PHP statement checking to see if PHP can retrieve any data from it:
if(!empty($_POST['admin'])) {
// do stuff
}
else {
echo "Value not set";
}
the problem is that PHP can seem to return a value for the radio buttons if "yes" is selected, but not if "no" is selected, I've tried removing the "checked" portion, to no avail. I just can't get it to retrieve the "0" value whatever I try.
I remember using PHP arrays to name checkboxes, but this shouldn't be needed for radio buttons surely, as only can be selected at any one time?
Or does PHP just have a problem returning radio buttons with a value of 0?
Or am I doing something horribly wrong without realising it?

PHP function empty will return false for 0, NULL, "", and others.
Quoting from php.net:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)
All of these will return (false) therefore nullifying the execution of your if statement.
You will have to modify your condition to reflect a value depending on what you wish to accomplish.
Good luck!

Your problem is that empty() treats zeros as empty values, no matter if it's a number 0, or a string '0'. So, instead of using empty() use isset() and/or direct checks, e.g.
if (isset($_POST['admin'])) {
or
if (isset($_POST['admin']) && $_POST['admin'] == 0) {

I believe the zero is your problem. Try setting them to "Y" and "N" and you should see that the N value comes through. This is because 0 is an empty value in PHP.

Related

Form not posting one field

I ran into a problem in my php/html code here, and I can't seem to locate the bug. The form seems to be posting everything except this one field, which seems to return "empty" every time.
(trying to set the variable "active" from the "select" option in the form, defaults to database value. The problem is, it's always returning to the default [database] value, regardless of the "select" option below...even after posting the form.) *Note that the database value of "active" is a 0/1 bit value. Does this affect the result?
php:
$active = (!empty($_REQUEST["active"]))?$_REQUEST["active"]:$row["active"];
html:
<select class="sel" name="active" id="active">
<option value="0" <?php echo ($active=="0"?"selected":"");?>>Not Active</option>
<option value="1" <?php echo ($active=="1"?"selected":"");?>>Active</option>
</select>
In your PHP code, empty will return true for the string "0", thus setting $active to the pre-existing value in such cases. What you maybe want instead is something like:
$active = (array_key_exists('active', $_REQUEST) ? (!empty($_REQUEST['active']) ? true : false) : $row['active']);
This will set the $active variable to true if the provided string is not considered empty (i.e. anything other than 0 or an empty string), false if it's present but empty, and preserve the existing value if the array key doesn't exist in the data.

PHP Checkbox can not show checked on edit page

There is a problem to show checked in checkbox on edit page.
There is my code below:
SSC<input type="checkbox" name="ssc"class="form-control" value="SSC"
<?php if(isset($ssc)) echo ($data[6] == 'SSC') ? 'checked':''; ?>/>
HSC<input type="checkbox" name="hsc" class="form-control" value="HSC"
<?php if(isset($hsc)) echo ($data[6] == 'HSC') ? 'checked':''; ?>/>
Where did you assign $hsc/$ssc ? Why don't you make it like this :
if(isset($data[6]) && $data[6] == 'SSC') echo 'checked';
or
if(isset($data[6]) && $data[6] == 'HSC') echo 'checked';
for the other checkbox
Okay, lets go throw the issues
mysql functions were deprecated
currently it is possible for an attacker can inject malicious SQL because the way you pass your variables
beacuse your $data variable always contains the education column
the value of a checkbox is sent only if it was checked otherwise it's not even set
the educatio column can contain three value currently and none of them equals with your data
SSC
SSC HSC
HSC
Solutions:
Use mysqli functions instead mysql
Use prepared statements and parameterized queries.
Don't check if a column is set if it is defined by your select query
Always check if a checkbox value was sent with isset or add a hidden input with the same name and empty value above the checkbox so if the box is not checked the value of the hidden input will be sent
check if the string contains those "words" (strpos($a, 'SSC') !== false - it's true when not false)
(And do not use the answer section to give aditional data update your question instead)

Boolean to integer in php

I am currently using checkboxes in a form and want to save the checkbox value into a database column of type int. What would be the most efficient way to achieve this. I have already tried casting the variable to a (int) but this has not done anything for me yet.
HTML: <input name='active' id='active' ng-model='formData.active' ng-init='formData.active=true' type="checkbox"> Active </br>
PHP:
$active = (int) $_POST['active'];
echo $active;
echo $_POST['active'];
Output:
0
true
Note: I am trying to implement this without an awful if statement or switch.
The $_POST variable for the checkbox will only be set if the checkbox is checked, so you can use something like this:
$active = isset($_POST['active']) ? 1 : 0;
I suspect that you're getting the string "true" rather than a boolean "true", in which case you can use a similar construct:
$active = $_POST['active'] === "true" ? 1 : 0;
solution in https://stackoverflow.com/a/29288171/3923450 should work,
but if you are looking for an inbuilt function, try
intval
add an value attribute in input tag
<input name='active' value='1' ...>
Now it should work the way you need.
Fist of all, your input in HTML is "sticky" and in PHP you test "active".
But anyway, try in PHP following test:
if (isset($_POST['sticky']) and ($_POST['sticky']=='on')) $active=1;
else $active=0;
Looking around in the PHP manual the following code can be found:
filter_var('FALSE', FILTER_VALIDATE_BOOLEAN, array('flags' => FILTER_NULL_ON_FAILURE)
Reference: http://php.net/manual/en/function.filter-var.php#118356

MySQL storing boolean - 1, 0 , or NULL

So I've generally stored Boolean values in MyIASM MySQL DBs using TinytInt(1). In a site I'm currently working on, I need to be able to store 1, 0 or NULL. This field is being populated from a radio button in a PHP form.
When I choose 'Yes' in the form (with a value of 1), this gets stored accurately. When I choose 'No' however (with a value of 0), it gets stored as NULL.
I want to reserve NULL for if the user chooses neither 'Yes' or 'No'.
Any idea why the 'No' (0) values aren't storing as expected?
EDIT:
Here's the basic HTML:
Yes <input type='radio' name='video_transfer_dvd_question' value='1' />
No <input type='radio' name='video_transfer_dvd_question' value='0' />
In PHP, both '' and 0 are considered FALSE in a boolean context, so if you try to test '' == 0 you'll find the result is TRUE. And empty() only checks whether the argument is boolean FALSE, so it doesn't truly distinguish between an empty $_POST and a $_POST which contains a value that happens to evaluate as FALSE. My suggestion would be to change the values of your radio buttons so than they never evaluate as boolean FALSE, e.g.:
Yes <input type='radio' name='video_transfer_dvd_question' value='yes' />
No <input type='radio' name='video_transfer_dvd_question' value='no' />
This keeps things more explicit when you test the response and choose a value to send to MySQL:
if (empty($_POST['button'])) {
$value = NULL;
} elseif ($_POST['button'] == 'no') {
$value = 0;
} elseif ($_POST['button'] == 'yes') {
$value = 1;
}
You could also use !$_POST['button'] in place of empty($_POST['button']), but that would generate a warning if $_POST['button'] is not set, which you would probably rather not happen.
You could also change that last elseif block to a simple else, if you have no reason to expect that the possible responses to this radio button will ever change and don't care about being explicit.
As the other people have commented its 99% an issue with your code...what you will need is something like...
if ($_POST["radiobutton"]) { $insertValue = 1; } else { $insertValue = 0; }
or
$insertValue = ($_POST["radiobutton"]) ? 1 : 0;
The reason is an empty radio button is an empty $POST var value which gets interpreted as NULL in mysql.

If HTML form was submtited check

is if ($_POST['submit']) ... same as if (isset($_POST['submit']) ... or it means something else? Which one i should use to check if form was submitted?
Is there some way to pull form name?
Use if ($_SERVER['REQUEST_METHOD'] == 'POST'). All other methods are flawed. What if you rename the submit button?
To get the form name you can either:
1) use a hidden input field whose value is the same as the form name
2) set the name of the submit button to the form name
The actual form name is not sent in the POST or GET variables.
isset
Determines if a variable is set and is not NULL
Boolean cast (same as !empty($var))
empty returns FALSE if var has a non-empty and non-zero value.
The following things are considered to be empty:
* "" (an empty string)
* 0 (0 as an integer)
* 0.0 (0 as a float)
* "0" (0 as a string)
* NULL
* FALSE
* array() (an empty array)
* var $var; (a variable declared, but without a value in a class)
Pages can have more than one form. It's best to give your submit element a value as well:
<input type="hidden" name="submit" value="login" />
Then in your PHP code, you can:
if (isset($_POST['submit']) && $_POST['submit'] == 'login') {
}
if(isset($_POST['submit'])) checks if the variable exists or in this case if the key exists under the array and is not null.
if($_POST['submit']) will check if the value of $_POST['submit'] is equal to true (or some other value that evaluates to true).
the second method there, will throw a notice error if the variable is not set. You should use isset to check if a variable is set, that's what it is for.
they are the same but using if(isset($_POST['submit']) or if(!empty($_POST['submit']) are better

Categories