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"'; ?> />
Related
The unchecking and checking after submit works if i don't use this line of code to load the data everything works. echo ($extraServices == 'Park') ? "checked='checked'" : (($extraServices == 'Park,Electricity') ?
<input type="checkbox" name="services[]" value="Park" <?php
echo ($extraServices == 'Park') ? "checked='checked'" : (($extraServices == 'Park,Electricity') ? "checked='checked'" : "");
if(isset($_POST['services'])) {
if(in_array("Park", $_POST['services'])) {
echo "checked='checked'";
}
}
?>> Park
So now if use echo "checked='checked'"; and echo ($extraServices == 'Park') ? "checked='checked'" : (($extraServices == 'Park,Electricity') ? "checked='checked'" : "");
together. It doesn't work. So with this code it will load the data from the database on which checkbox was checked. That works. But if i uncheck a checkbox it wont be unchecked after submit. How can i solve this? I thought about a way to detect if i load this page for the first time. But then i bumped into the problem that that was for loading the page the first time EVER.
I wanted to come up with something to detect if the page loads and loads the data. After that it doesn't need to load the data from the database. But i don't know how i could do this.
I dont know what to do now, hope that someone got some idea's?
Thanks for your help.
It is easier to code complex if this or that or the other type tests as a simple piece of php rather than trying to bury the tests into some html. That becomes unmaintainable very quickly. It is also not recommended to string ternary tests together.
So create an empty variable and then fill it with the checked=checked if the rules say it should be checked
<?php
$chk = '';
// here are the rules for setting the checked attribute
if ( ($extraServices == 'Park') || ($extraServices == 'Park,Electricity') ) {
$chk = 'checked=checked';
}
?>
<input type="checkbox" name="services[]" value="Park" <?php echo $chk;?> >
So the HTML will add $chk always, but if its not required, $chk will simply be empty :)
If I missed something this at least should set you on the right path. Post a comment below if you need anything explaining in any more detail
I want to mark a checkbox as 'checked' automatically when an if condition is fulfilled. Here is an example of the if condition-
if($wp_session['tdwa_verification_checks'] < 2){
}
And the checkbox is-
<input class="input-text a-save" type="checkbox" id="chkboxclicked" name="tdwa-foreign-citizen" value="1">
I am trying with this but its not working.
if($wp_session['tdwa_verification_checks'] < 2){
echo '<input class="input-text a-save" type="checkbox" id="chkboxclicked" name="tdwa-foreign-citizen" value="1" checked>';
}
I would appreciate if anyone can give me a clue. Thanks :)
A couple issues. One is that you're wrapping you're entire checkbox inside an if statement. Another is that it's very strange to check for a boolean value by comparing it to less than 2 as you'll generally compare it to equal 1. Another is that chkboxclicked is a very vague ID to say that least, you should probably change that to something more similar to the name. You should also add the closing slash since checkbox inputs are void elements.
Now, looking at your code, you're also checking $wp_session['tdwa_verification_checks'] but the input name is tdwa-foreign-citizen, are you sure that the key you're checking in $wp_session is correct?
Lastly, WordPress has a neat function called checked() that will compare and check values for you if applicable. This is how you should probably be using it in your markup:
<input class="input-text a-save" type="checkbox" id="tdwa-foreign-citizen" name="tdwa-foreign-citizen" value="1" <?php checked( $wp_session['tdwa_verification_checks'], 1 ); ?> />
you're printing the checkbox correctly, you have to verify to value for
$wp_session['tdwa_verification_checks']
you can find the value by doing
echo json_encode($wp_session['tdwa_verification_checks']);
once you find the value you can compare correctly
I would like to set checkbox value with some attribute I put with PHP.
It seems weird but it's quite simple look, here is my PHP :
<input type="checkbox" val="'.$key->mes.'" class="mes"/>
My idea : check the checkbox when the val is 1, uncheck when it's 0
I try to figure this out with Jquery like this :
$('input:checkbox [val=1]').prop('checked', true);
But it doesn't work... Any tips?
Since you're working in PHP already, how about this:
"<input type='checkbox' val='". $key->mes . "'" . $key->mes > 0 ? "checked='checked'" : "" . "/>"
Then you don't need to do stuff client side...
you should use value and not val attribute
<input type="checkbox" value="1" />
and
$('input:checkbox[value="1"]').prop('checked', 'checked');
Fiddle: http://jsfiddle.net/kabY3/
As far as I know you should not use .prop('checked',true); only calling .prop("checked") should be enough. source: jquery.com
$(elem).prop("checked") true (Boolean) Will change with checkbox
state
I've read that using .prop("checked",false) for instance is bad and you should go with .removeProp()
Have a look at the .prop documentation:
http://api.jquery.com/prop/
But there should be a way php to solve your problem.
i think is more easy doing it in php:
<?php
$ch="";
if($key->mes==1) $ch=' checked="checked"';
echo '<input type="checkbox" val="'.$key->mes.'" class="mes"'.$ch.'/> '
?>
if you want it in jquery try what #EvilP has suggest.
As long as your DOM element properly reads <input type="checkbox" val="1" class="mes"> using your favorite developer tools, I would just do $('input[val=1]').prop('checked', true);
Here is a jsfiddle: http://jsfiddle.net/remibreton/u96pc/
I want to get the value of a checkbob in a post method form
The html :
<input type="checkbox" name="subscribe" value="1" style="float:left;" checked="checked">
The php i have for it:
<?php $subsribe = $this =>input =>post("subscribe") ; ?>
I sense from my editor that the php code isn't correct.
In may web page it's all blank.
<?php
$subsribe = $this->input->post("subscribe");
?>
EDIT:
<?php
$subsribe = isset($_POST['subscribe']) && $_POST['subscribe'] == '1'; // this will contain a boolean, will be true if the user wants it, and false if they don't.
?>
As for JavaScript, not quite sure how you going to run it, but if its from the same page that contains the checkbox, this should work:
document.getElementById('subscribe').value;
You will need to give the checkbox the attribute: id="subscribe"
Try to get post data result by var_dump($_POST) then realize what value you want.
I am currently trying to design a for loop to iterate through any 'checked' check boxes, and from that, use the value within the 'value' slot to run some queries. I am unfortunately struggling with this as the list of checkboxes are not pre-defined, they are dynamic from the database pending the users previous selection.
The loop actually works to present the items to be checked:
?>
<input type="checkbox" name="option[]" value="$listing_id">
<font size="+1" color="green"><?php echo"$list_name"; ?>:</font><br />
<?php
The listing ID within the value is what I need to work with in a mysql query before I run an update query. The for loop that's meant to work is:
foreach($_POST['option'] as $option) //loop through the checkboxes
{
...
}
The update query will work within this as its simply copied from somewhere else, I just need the 'Listing_ID' from the check boxes that are checked.
I ran this code to hopefully do some debugging:
if(empty($_POST['option'])){
echo "no checkboxes checked.";
} else {
if(!isset($_POST['option'])){
echo "no set.";
}
}
and it returns "no checkboxes checked."
I have now hit a grey area as to why this for loop isn't working (this was taken from another example on the internet).
empty($_POST['option']) will return true, if either $_POST['option'] is not set (same as !isset($_POST['option']) (!)) or an empty array.
If you need to debug what's going on, use var_dump($_POST['option']); to find out what has been submitted for the option checkboxes. I also suggest you do a var_dump($_POST); so you can see what has been submitted overall - e.g. in case the post action is not post you will immediatly notice). For HTML output:
echo '<pre>', htmlspecialchars(print_r($_POST, true)), '</pre>';
That should give you the information you're looking for. For each individual checkbox, you can do:
foreach($_POST['option'] as $option) //loop through the checkboxes
{
var_dump($option);
}
First of all your code seems to be bugged to me. Maybe is just a typo but
<input type="checkbox" name="option[]" value="$listing_id">
should be
<input type="checkbox" name="option[]" value="<?=$listing_id?>"/>
Moreover using empty over an array is not good at all.
Try echoing out the $option in the loop to see what the value is and there you can see if there is something there.
foreach($_POST['option'] as $option) //loop through the checkboxes
{
echo $option . "<br />";
}
Also make sure your form's method is set to POST or that it's action is pointed to the correct place. You also have an error in your input:
<input type="checkbox" name="option[]" value="$listing_id">
I assume you meant:
<input type="checkbox" name="option[]" value="<?php echo $listing_id;?>">
UPDATE:
The error ended up not being in the code posted. Error was discovered in an if statement that always returned false that in-cased the code posted above.