PHP ternary always outputs as if conditional is true - php

In the following code...
echo "|".$express_ship."|".is_bool($express_ship)."|".(int)$express_ship."|".is_true($express_ship)."|";
echo '<input type="checkbox" id="express_ship" name="express_ship"'.($express_ship ? ' checked' : '').'/>';
... the input box is always checked, even when the echo line before it returns...
|false||0|false|
Any ideas?
It's strange that is_bool($express_ship) returns an empty string. Has anyone encountered this before?
PS: if $express_ship is true, the line before displays as...
|true||0|true|
Addendum: For some reason I thought is_true was a PHP function and I was using this to debug $express_ship. Strangely this never caused an error, even though there is no such function in PHP. (And I have coded no such function of my own.)

if variable $express_ship is return string like "true" or "false" it will always show your checkbox checked.
you need to update the code like below:
echo '<input type="checkbox" id="express_ship" name="express_ship"'.($express_ship == 'true' ? ' checked' : '').'/>';
or you need to check by var_dump to find which type of value you get in this variable $express_ship

Related

How to set a radio button checked dynamically via php?

In this code we want to read line 7 of a file with our shell and if the value is "set" we want to change the status of our checkbox to checked.
<input type="radio" name="myName" value="myValue" <?php
if( shell_exec("./read_from_db 7")=="set") echo ' checked="checked"'
?>
It seems this code those not work. Even though line 7 of the file is "set" the radio button is not checked. Why is that? What's wrong with the code?
The result coming from shell_exec is giving you something extra.
Try using trim, so:
if( trim(shell_exec("./read_from_db 7"))=="set") echo ' checked="checked"'
Here is a test:
var_dump(shell_exec('echo set'));
echo '<br>';
var_dump(trim(shell_exec('echo set')));
echo '<br>';
$v1 = shell_exec('echo set') == 'set';
$v2 = trim(shell_exec('echo set')) == 'set';
var_dump($v1);
echo '<br>';
var_dump($v2);
and the output:
string(4) "set "
string(3) "set"
bool(false)
bool(true)
You can see that even with no space in the original code, it added something there. My guess is that it is some sort of line break or anything like that.

PHP Post Always Returns 1

I'm trying to update mySQL database with PHP. I got form on index.php. I'm getting value of id from link using GET method and trying to transfer value to another page with input hidden.
When i try to read this value on another page it always returns 1.
Here is my code Index.php
<?php
$usr = $_GET['id'];
echo $usr ;
?>
<form method="post" action="status.php">
<input type="text" name="number"> <br>
<input type="hidden" name="3" value="<?php echo (isset($usr)) ? $usr : '' ?>" />
<input type="submit">
</form>
And Here is status.php
$hello=isset($_POST['3']);
echo $hello ;
echo "<br>";
It's always 1.
isset() return bool so if variable is available the $hello=1 else $hello=NULL
Wrtie POST as
if(isset($_POST['3']))
{
$hello=$_POST['3'];
echo $hello;
}
You'll using isset() function to assign variables which should not be the case:
$hello = isset($_POST['3']);
This will return true if $_POST['3'] is set.
As from the literal meaning isset() means variable is set. It's a BOOL function, which means that it can only return true when set and false when not set/ empty.
Thus, your code should be:
$hello = $_POST['3'];
More information on isset(): http://php.net/manual/en/function.isset.php.
Tip: It is actually not a good idea to put the value in a hidden textbox and then get it using $_GET, as most people can just edit the value in their browser.
It is always returning '1' because of:
$hello=isset($_POST['3']);
The function isset() returns 1 for if the value is set and 0 if it is not set. In binary 1 means "true" and 0 means "false"
Essentially, your code is saying that $_POST['3'] exists.
However, there may be a bigger underling issue: $_POST is an array so using a number as your input name may be considered bad practice. Try using a name using letters instead like:
<input type="hidden" name="hidden_user"/>
I think you are using isset wrong.
This function, isset, is not to grab the value of something, but just to check if this something is defined.
Try this:
$hello=$_POST['3'];
The proper way to get your post parameter in status.php is:
$hello = isset($_POST[3]) ? $_POST[3] : null;
echo $hello;

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

PHP not retrieving radio input value

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.

Problems to save the value of a checkbox in the database

I have doubts ...
I am not able to save my information checkbox in the database, and so little rescue in a search screen, if that is done manually in the database ...
$ TRAMENTO1 = (#$ _POST ["TRAMENTO1 "]=='true') ? $ _POST ["TRAMENTO1"]: 'false';
<Input name="TRAMENTO1"
type="checkbox"
id="TRAMENTO1"
value="true"
php if ($TRAMENTO1 == true) {echo "checked"}>
/>
Do so and only get from my bank to respond "false" even if my checkbox is checked. and only the first two checkbox yet.
If you can help me I am very grateful.
Cleiton Capristano
I found a few of things wrong.
One, the name of your input in the HTML is "TRAMENTO1 " but the name as called in PHP is "TRAMENTO1".
There are no brackets around your PHP code within the input*.
There are no brackets between the HTML and the PHP*.
$ TRAMENTO1 and $ _POST don't work as a variable name. No spaces allowed.
As a side note, you might think about generally cleaning things up a bit:
<?php
$TRAMENTO1 = isset($_POST['TRAMENTO1']) ? 'true' : 'false';
?>
<input name="TRAMENTO1" type="checkbox" id="TRAMENTO1" value="true"<?php echo ($TRAMENTO1 == 'true' ? ' checked="checked"' : ''); ?> />
*I see in your revision history that you copied over the code without applying the correct Markdown characters, so these concerns might be moot for the original code.
I don't know if this was broken from the beginning or happened when you copied it to SO. Anyway, here's the code you're looking for:
<?php
$TRAMENTO1 = $_POST["TRAMENTO1"] ? 1 : 0; // This you can put into the database
?>
<input name="TRAMENTO1" type="checkbox" id="TRAMENTO1" value="true" <?php if ($_POST['TRAMENTO1']) echo 'checked="checked"'; ?> />

Categories