How to set a radio button checked dynamically via php? - 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.

Related

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

Echo input from users only if it exists or is of desired value

I am building a Order form for a Client's website. One of the pages lets users choose Vehicle Spareparts by entering the quantity beside each part. I have achieved this by writing the following code
<input class="qty" type="number" name="oil-pump" min="0" max="100">
The above input attribute is added several times besides each sparepart type while changing just the name value. On clicking Submit, the user is taken to a review page with details on the spareparts ordered.
I presently have the following code in review page:
<?php
echo 'Engine Bearings: ' . $_POST['eng-bearings'];
echo 'Cylinder Heads: ' . $_POST['cyl-heads'];
echo 'Cam and Valve Train: ' . $_POST['cam-valve-train'];
echo 'Oil Pump: ' . $_POST['oil-pump'];
echo 'Oil Caps and Pipes: ' . $_POST['oil-caps-pipes'];
?>
This works well if the user has entered a value in every field. If he doesn't, then the review page has just the echo text without any quantity. Also, I don't want the quantity to apppear if the user has entered 0.
I can't figure out how to use isset() with null and OR statements to not echo text when the value entered is 0 or left blank.
Any help is appreciated. Thanks!!
This is the perfect opportunity for a function:
function print_user_input($title, $queryVar) {
if (isset($_POST[$queryVar]) && !empty($_POST[$queryVar])) {
echo $title . ': ' . $_POST[$queryVar];
}
}
Use it like so:
print_user_input('Engine Bearings', 'eng-bearings');
Echo the value conditionally. If the target data is empty, echo an empty string:
echo empty($_POST['eng-bearings']) ? '': 'Engine Bearings: ' . $_POST['eng-bearings'];
Repeat for the other post values. Information about the ternary operator (<conditional> ? <value1> : <value2>) is here.
You need to check if its set & that its not empty before printing it on a screen.
<?php
if (! empty($_POST['eng-bearings'])) {
echo 'Engine Bearings: ' . $_POST['eng-bearings'];
} else {
// whatever you wanna do here in case eng-bearings is not set or its empty
}
// Do same for other $_POST values ...
?>

Drupal print if single checkbox field is checked

I'd like to print something like "yes, checked" if a cck single checkbox is checked.
The single on/off checkbox has allowed values of no and yes.
checkbox info is -
<input type="checkbox" class="form-checkbox" checked="checked" value="yes"
id="edit-field-billing-terms-value" name="field_billing_terms[value]">
I'm trying, and failing with modifications of this code -
<?php
$node->field_billing_terms[value] . '<br />';
if($node->field_billing_terms[value] == 'yes' ) {
print "yes, checked";
}
?>
Can someone give me some pointers where I'm going wrong? More info can be provided if needed.
Fields are normally in a zero based array when attached to the node object, this should fix your problem:
$node->field_billing_terms[0]['value'] . '<br />';
if($node->field_billing_terms[0]['value'] == 'yes' ) {
print "yes, checked";
}

php checkbox array access

$i=0;
while (db_data) {
$i++;
echo '<input type="checkbox" name="v['.$i.']" value="'.$url.'"';
if ($v[$i]) {
echo ' checked';
$s .= $url;
}
echo '/>';
}
I have the above array of checkboxes. It worked on my pc, but not on the server; it seems like the confusing part is on $v[$i].
$v is not defined, but sure used no where else. the problem is my checkbox selection never restored, and code never get into the if statement.
however, if i add the following, i can see the value. just the checkbox lost after the processing
$v=$_POST['v'];
while (list ($key,$val) = #each ($v)) {
$x .= ' 11*'.$key.'-'.$val.'*22 ';
}
my goal is to preserve the checkbox checked on the form, and i need the $s elsewhere. any solution to replace $v[$i]?
Can anybody help me fix this? Thank you.
The issue seems to be $v = $_POST. If you are just doing that then your conditional statement would need to be
if ($v['v'][$i]) {
///Checkbox
}
or just do $v = $_POST['v'].
Sorry, ignore above as you did mention you did that part. See below.
Here is working code.
<form action="" method="post">
<?php
$v = $_POST['v'];
$i=0;
while ($i < 4) {
$i++;
$url = "test.com/".$i;
echo '<input type="checkbox" name="v['.$i.']" value="'.$url.'"';
if ($v[$i]) {
echo ' checked="checked"';
$s .= $url;
}
echo '/> '.$url.'<br />';
}
?>
<input type="submit" name="submit" value="submit" />
</form>
I left the code pretty much the same to show you where you went wrong, but you should be checking the $_POST variable for exploits before using. If I were doing this as well, I would use a for count, but it's setup as a placeholder for your database code. Make sure that $url gets populated as well.
You could also do away with the $i variable like:
<?php
$v = $_POST['v'];
while (db_data) {
echo '<input type="checkbox" name="v[]" value="'.$url.'"';
if (is_array($v)) {
if (in_array($url,$v)) {
echo ' checked="checked"';
$s .= $url;
}
}
echo '/> '.$url.'<br />';
}
?>
Try to print_r($_POST) and then print_r($v) and see if anything comes up. If the $_POST works, then you know that it is being posted back to the page correctly. Then if the $v is working, then you know you set $v = $_POST correctly. Due to the fact that you don't actually give us any information on the db_data, I assume this is working correctly and displaying all the checkboxes on first load, so as long as it is posted and you are setting the $v variable, it should be working.
A side note is that you should validate the $_POST variables before using, but do that after you get things working.
change
name="v['.$i.']"
to
name="v[]"
the fact that PHP picks that up as an array is a unintended feature of PHP that wasn't intentionally designed. you don't need to set the indexes, just define it as an array.

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