I am storing a radio button value which can be true, false or NULL in a database.
In case of null or false the answer is 0 that is false.
Is there any solution for checking the null value? I have tried using isset and Empty but none of these help.
Database is MSSQL and datatype is bit to store the value of radio button
<input name="radioVal" type="radio" id="radioVal" value="false"
<?PHP
if(row['radioVal'] == false )
echo "checked='checked' ";
}
?> />
<input name="radioVal" type="radio" id="radioVal" value="true"
<?PHP
if(row['radioVal'] == true )
echo "checked='checked' ";
}
?> />
TRY
if(false === row['radioVal'] ) //or
if(NULL === row['radioVal'] )
and to check for NULL you can check with is_null
is_null(row['radioVal'])
The bit column is probably going to be represented as a 1 or a 0. Use the triple equals to check for value and type. Check if row['radioVal'] === 1 for true, row['radioVal'] === 0 for false, and row['radioVal'] === null for null.
Maybe you should use another data type in your SQL Database.
I suggest Enum or something like that:
ENUM('false', 'true', 'NULL')
In your PHP file you
<?PHP
if(row['radioVal'] == false )
echo "checked='checked' ";
}
?>
Related
I have a php var ($testx) set to 0.
Html code looks like this:
<input type='checkbox' name='test_cb' id='test_cb' checked=<?= $testx == true ? 'checked' : '0' ?>></input>
The checkbox is always checked even though the php var being checked is false. I must be missing something obvious.
You're close :)...
<input type='checkbox' name='test_cb' id='test_cb' <?= $testx == true ? 'checked' : '' ?>></input>
First: Don't include the checked attribute unless you want it checked. (Your original code includes checked= always, even if it should not be checked)
Second: Personally, I find that style of code to be less readable.
Here is how I would propose you do it:
<?php $checked = ( $testx ) ? ' checked' : ''; ?>
<input type='checkbox' name='test_cb' id='test_cb'<?php echo $checked; ?>>
NOTE: This assumes that $textx is either truthy or falsey - since you wrote $textx == true, I'm guessing it's either NULL or FALSE if it's not set. Clearly you could check more strictly, if required:
<?php $checked = ( TRUE === $testx ) ? ' checked' : ''; ?>
FINALLY:
Better yet - since this would be something I presume you do often - would be to create an is_checked function, like so:
function is_checked( $var ) {
if ( $var ) {
echo ' checked';
}
}
Then you could use it repeatedly, like so:
<input type='checkbox' name='test_cb' id='test_cb'<?php is_checked( $testx ); ?>>
<input type='checkbox' name='test_again' id='test_again'<?php is_checked( $testy ); ?>>
// etc...
I have this checkbox in my html
<input name="cb" class="cmn-toggle cmn-toggle-round" type="checkbox">
What I understood about how checkbox worked, was that when "checked" if you isset the input then it would "exist", and if wasn't "checked" wouldn't, so I did this:
if(isset($_REQUEST['cb'])){
//do something
}else{
//do something else
}
The problem is that when sending the form, it always exist, doesn't matter if checked or not, I don't know how to really see if really checked, so what am I doing wrong?
isset() determine if a variable is set and is not NULL. So in your case, the $_REQUEST['cb'] always exist, so isset() will be true. So if you have isset() in your condition, you need to add a check, if the value is true or false.
You need edit your condition to:
if($_REQUEST['cb']) { ...
or
if($_REQUEST['cb'] == true){ ...
and the best way is use isset with check above:
if( isset($_REQUEST['cb']) && $_REQUEST['cb'] == true ) { ...
You can check like this
if(isset($_REQUEST['cb']) && $_REQUEST['cb']){
I tried to make a form with a post method, and i want to check if the fields are filled with isset(). But even if the fields are empty, the isset returns true.
Here is a simplified code: (name of the page: test.php, the post method is directed to itself)
if (isset($_POST['test'])) {
echo 'field set';
}
?>
<form method="post" action="test.php">
<input type="text" name="test"/>
<input type="submit" value="Submit"/>
</form>
When you submit, it always echoes 'field set', even if you didn't enter anything in the "test" input.
Thanks for your answers !
So check whether $_POST['test'] is empty or not and try like this
if (isset($_POST['test']) && trim($_POST['test']) != '') {
echo 'field set';
}
You can also use empty,but Note that "0" is also "empty" and along with it its better to trim the inputs to remove spaces. Another way is to check the length of input using strlen
if (isset($_POST['test']) && trim(strlen($_POST['test'])) > 0) {
echo 'field set';
}
try with empty() to check blank cause after submit form $_POST['test'] always found in isset() even it's blank. so need to check with empty()
if (!empty($_POST['test'])) {
echo 'field set';
}
0 would be empty/blank with checking empty()
You can do this with empty() method.try with this code.
ex : echo !empty($_POST['test']) ? "field set" : "" ;
It will echo that you field is set because all you are checking for if is the array key test exists with isset(). As long as you have a field in your form with the name test, it will create an empty key, thus resulting in it being set.
What you would rather like is to see if the key is set, and if the key also is empty or not.
This you can do by:
if( isset( $key['test'] ) && $key['test'] !== "" ){
//Do something
}
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.
I want that my radio buttons return me a Boolean value true or false instade of on/off
So I pass the true/false in the value of the input :
<label>Male
<input type="radio" name="IsMale" value="true" />
</label>
<label>Female
<input type="radio" name="IsMale" value="false" />
</label>
but it returns me a true/false in a text format. Please masters how could I get them in a booleen format ?
More details : In fact I need to store my $_POST array in a file.txt, and for my radio button I need to store for example :
array ( "IsMale" => true );
and not :
array ( "IsMale" => "true" );
You'll have to check the data and modify it.
if(isset($_POST['SubmitButton'])) {
$_POST['IsMale'] = $_POST['IsMale'] == 'true' ? true : false;
}
You cannot make radio buttons or any other form element directly submit a PHP true value, only a string such as "true".
To solve your problem, you would have to change the value of the $_POST item in your PHP file.
//Form has been submitted
if(isset($_POST['submit'])) {
//Radio button has been set to "true"
if(isset($_POST['IsMale']) && $_POST['IsMale'] == 'true') $_POST['IsMale'] = TRUE;
//Radio button has been set to "false" or a value was not selected
else $_POST['IsMale'] = FALSE;
}
Edit: Ben has provided a functional solution using ternary operators which is a shorter alternative. The example above may clarify exactly what is going on in the process (in a more verbose form).