Boolean to integer in php - 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

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 ternary always outputs as if conditional is true

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

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;

reflecting a mySQL boolean in a checkbox

I have a field in mySQL featured, TINYINT, which will be either 0 or 1
I also have a form:
<input type="checkbox" name="featured" value="<?php echo $row_rs_dealItem['featured']; ?>"/><br /><br />
When I select the checkbox I am able to set the value to '1' in mySQL, however not the other way round because when I view the update record page the checkbox is always blank (I understand the checkbox status is independent of the value and should be set so).
I therefore need the checkbox to be checked if value '1', and also if I uncheck, then I need the value in mySQL to change back to '0'
I know there are lots of posts on this, but I can't seem to make head nor tail, and nothing I try works.
Can we keep it in PHP please?
thanks
use it in your html checkbox:
<?php if ($row_rs_dealItem['featured'] == 1) echo "checked='checked'"; ?>
and write it in your file where you save the data:
if(isset($_POST['featured'])) $featured = 1; else $featured = 0;
$featured = (isset($_POST['featured'])) ? 1 : 0;
Just use PHP's intval function,
after you submit the form, in the file where you save the data:
$_POST['featured'] = intval($_POST['featured']);
If checkbox isn't checked $_POST['featured'] is NULL, and intval(NULL)=0.
try
<?PHP
if(isset($_POST['featured']))
{
$_POST['featured'] =1;
}
else
{
$_POST['featured'] =0;
}

Check if any variables are passed in a GET

I've done a few searches and not come up with anything, I'm sure it's obvious.
Basically I'm trying to work out if anything has been passed via GET from a form.
I know how to check for individual elements, but I just want to do a quick check if anything at all is passed
Cheers
Be careful when using count($_GET). If you submit the form with empty values it will still create keys for the fields, and your count() will be greater than 0 and empty($_GET) will be false.
<?php
print_r($_GET);
?>
<form action="" method="get">
<input type="text" name="name">
<textarea name="mytext"></textarea>
<input type="submit">
</form>
Make sure the fields are actually not empty:
function ne($v) {
return $v != '';
}
echo count($_GET); // prints 2
echo count(array_filter($_GET, 'ne')); // prints 0
This should do the job:
if (!empty($_GET))
{
}
if ( count($_GET) > 0 ) echo("I hear you!");
if(empty($_GET)) { /* no parameters passed*/}
just check the length of the $_GET array via count($_GET).
if none ha passed it should be 0
Simply 'just': if($_GET){ /* parameters passed*/} (for the current request) works to check if any query-string was passed in the GET or POST request.
This is because an empty array is false in a boolean if($x) context.
See: http://php.net/manual/en/types.comparisons.php
So indeed no need for count() or empty().
Actually, I think it is better to check using isset which checks if it is defined and if it is different than null
if(isset($_GET['value_you_looking_for']) {//code logic here}
Check for documentation

Categories