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/
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 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
problem
i have two button "YES" and "NO". if user click YES then text input will appear with value set ex: value="$check['sum'];" but when user click No then text input will appear without value ex: value ="".
here is my code
<input id="test<?php echo $row['br_loc'];?>" value="<?php echo $value;?>" name="test" type="text" class="test" style="width:70px;display:none">
and jquery
$(document).ready(function(){
$("#Yes<?php echo $row['br_loc'];?>").click(function(){
$("#test<?php echo $row['br_loc'];?>").show(1000);
});
$("#No<?php echo $row['br_loc'];?>").click(function(){
$("#test<?php echo $row['br_loc'];?>").hide(1000);
});
thanks you
Try using the disabled attribute:
$("#test<?php echo $row['br_loc'];?>").attr('disabled','disabled');
and
$("#test<?php echo $row['br_loc'];?>").removeAttr('disabled');
Try this:
$(document).ready(function(){
$("#YES").click(function(){
$("#test").show(); //value of input as set before. so, just show
}
$("#NO").click(function(){
$("#test").attr('value','').show(); //set value = '' , and show
}
});
Use binding function so that you provide your JS code with exact parameters. Code like yours should never be used.
Object ID or class should contain alphanumerical characters only, should not begin with number(s), and optionally, - or _ might be used. Avoid spaces or, as you've already shown yourself, using embedded PHP code which is highly unlikely to work in any further project of yours. It's also dangerous.
instead of this code $("#Yes<?php echo $row['br_loc'];?>")
try the following
$("#test<?php echo $row['br_loc'];?>").
Because you used id of button as "test<?php echo $row['br_loc'];?>". so only your click event not worked.
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"'; ?> />