When I checked my checkbox and click submit, it doesn't work and is still unchecked, mo matter if is checked or not!
<label for="checkbox-4" tabindex="4">food</label>
<input type="checkbox" <?php $ch1=strpos($f_type,"1"); if($ch1 >= 0 && $ch1 != ""){echo('checked="checked"');} ?> name="foodtype1" id="checkbox-4" value="1" />
Can anyone help me?
<input type="checkbox" <?php $ch1=strpos($f_type,"1"); if($ch1 >= 0 && $ch1 != ""){echo('checked="checked"');} ?> name="foodtype1" id="checkbox-4" value="1" />
You should put a space between <?php ... ?> and name="foodtype1".
EDIT:
Easier test below
<input type="checkbox" <?php echo (strpos($f_type,"1") !== FALSE) ? 'checked="checked" : ''; ?> name= "foodtype1" id="checkbox-4" value="1" />
<?php
$_POST['foodtype1'] = 1;
//$_POST['foodtype1'] = 'a';
?>
<label for="checkbox-4" tabindex="4">food</label><input type="checkbox"
<?php if(isset($_POST['foodtype1']) AND $_POST['foodtype1'] == 1) { echo('checked="checked"');} ?>
name="foodtype1" id="checkbox-4" value="1" />
Related
I am trying to store the input value of a radio button and storing that intoa session so that if the user roams around the site the radio remains checked unless they switch it themselves and then the new selection remains checked.
<form action="" method="POST" id="reportSwitch">
<input checked type="radio" name="reportType" id="leadership" value="1" <?php if($reportType == 1){
echo 'checked';} ?>>
<label for="leadership">Leadership</label>
<input type="radio" name="reportType" id="fundementals" value="2" <?php if($reportType == 2){
echo 'checked';} ?>>
<label for="fundementals">Fundementals</label>
</form>
<?php
$_SESSION['reportType'] = $_POST['reportType'];
$reportType = $_SESSION['reportType'];
if(isset($reportType)){
} else{
$reportType = 1;
}
?>
I cannot seem to to get it to remain in a checked state...
Put the value in session and use the session variable to populate value in radio button in place of using extra variable.
By populating from session It will help to retain in all pages.
<?php
$_SESSION['reportType'] = $_POST['reportType'];
?>
<form action="" method="POST" id="reportSwitch">
<input type="radio" name="reportType" id="leadership" value="1" <?php if($_SESSION['reportType'] == 1){
echo 'checked';} ?>>
<label for="leadership">Leadership</label>
<input type="radio" name="reportType" id="fundementals" value="2" <?php if($_SESSION['reportType'] == 2){
echo 'checked';} ?>>
<label for="fundementals">Fundementals</label>
</form>
check this code
<?php
session_start();
$_POST['reportType'] = 1; // for testing it is set define value , you can change
if(isset($_POST['reportType'])){
$_SESSION['reportType'] = $_POST['reportType'];
$reportType = $_SESSION['reportType'];
} else {
$reportType = $_SESSION['reportType'];
}
if(!isset($reportType)){
$reportType = 1;
}
?>
<form action="" method="POST" id="reportSwitch">
<input checked type="radio" name="reportType" id="leadership" value="1" <?php if($reportType == 1){
echo 'checked';} ?>>
<label for="leadership">Leadership</label>
<input type="radio" name="reportType" id="fundementals" value="2" <?php if($reportType == 2){
echo 'checked';} ?>>
<label for="fundementals">Fundementals</label>
</form>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this code for dynamic checked radio box:
PHP:
if ($author === 0) {$checked = 'checked';} else {$checked == '';}
if ($author === 1) {$checked = 'checked';} else {$checked == '';}
if ($author === 2) {$checked = 'checked';} else {$checked == '';}
HTML:
<input type="radio" name="test" <?PHP echo $checked; ?> />
<input type="radio" name="test" <?PHP echo $checked; ?> />
<input type="radio" name="test" <?PHP echo $checked; ?> />
This way is true? What’s is a better/optimized Way?
Can I write any PHP function or class for save code and checked any radio box?
It can save you some lines and unnecessary extra variables if you look forward using the shorten if statement form. Example with the first input :
<input type="radio" name="test" <?php echo $author === 0 ? 'checked' : '' ?> />
Ideally you want to check a button if the author value relates to that input, for example:
<input type="radio" name="test" value="0" <?php echo ($author == 0 ? 'checked="checked"' : ''); ?> />
<input type="radio" name="test" value="1" <?php echo ($author == 1 ? 'checked="checked"' : ''); ?> />
<input type="radio" name="test" value="2" <?php echo ($author == 2 ? 'checked="checked"' : ''); ?> />
Your currently checking all if the value is any.
The HTML markup is wrong. With that HTML the user is able to select all of those radios. To make it so that the user can only select one of the radio buttons out of that group of options, change the names to all match, like this:
<input type="radio" name="test1" <?PHP echo $checked; ?> />
<input type="radio" name="test1" <?PHP echo $checked; ?> />
<input type="radio" name="test1" <?PHP echo $checked; ?> />
Something you can do:
<?php
$checked = ($author >= 0 && $author <= 2 ? 'checked' : '');
?>
HTML
<input type="radio" name="test" <?php echo $checked; ?> />
<input type="radio" name="test1" <?php echo $checked; ?> />
<input type="radio" name="test2" <?php echo $checked; ?> />
<?php
switch($author) {
case 0:
case 1:
case 2:
$checked = "checked";
break;
default:
$checked = '';
}
I have radio buttons for Yes and No and want to pre-populate them from a value in a mysql database. I am able to show the radio buttons but they always show the Yes checked even when the returned value is No. I am also not able to change the pre-populated value eg Yes to No.
<input type="radio" id="etag" name="etag" value="Yes" <?php echo ($d_etag == 'Yes') ? 'checked="checked"' : ''; ?> />Yes
<input type="radio" id="etag" name="etag" value="No" <?php echo ($d_etag == 'No') ? 'checked="checked"' : ''; ?> />No
Note : id = should b unique as mentioned by Phil in his comments
This is how you can achieve ur task!
<input type="radio" id="etag_1" name="etag" value="Yes" <?php echo ($d_etag == 'Yes') ? 'checked="checked"' : ''; ?> />Yes
<input type="radio" id="etag_2" name="etag" value="No" <?php echo ($d_etag == 'No') ? 'checked="checked"' : ''; ?> />No
To find out Which Radio button is clicked use
$("input:radio[name=etag]").click(function() {
var value = $(this).val();
});
or try this
$('input:radio[name=etag]:checked').val();
Can you var_dump($d_etag); and check the data type of that variable. it should be a string with Case-sensitive.
Can you try this:
<input type="radio" id="etagYes" name="etag" value="Yes" <?php echo ($d_etag == 'Yes') ? 'checked="checked"' : ''; ?> />Yes
<input type="radio" id="etagNo" name="etag" value="No" <?php echo ($d_etag == 'Yes') ? '' : 'checked="checked"'; ?> />No
I'm trying to check a checkbox based on its database value, i have tried the following but no joy, any suggestions?
I am using Kohana framework.
public static function defaultdistance(){
$result = DB::select('value')->from('mytable')->where('key', '=', 'default-distance')->execute();
$distancestores = $result->as_array();
foreach($distancestores as $distancestore)
{
echo 'Value: '.$distancestore['value'];
}
}
<label class="shortLabel"><input type="radio" name="distance" value="10" <?php if ($distancestore['value'] == '10') echo "checked='checked'"; ?> /> 10 <?php echo $dict->transl('distance_km'); ?></label>
<label> </label><label class="shortLabel"><input type="radio" name="distance" value="15" <?php if ($distancestore['value'] == '15') echo "checked='checked'"; ?> /> 15 <?php echo $dict->transl('distance_km'); ?></label>
May be you need like this
$distancestores = $result->as_array();
foreach($distancestores as $distancestore)
{
?>
<label class="shortLabel">
<input type="radio" name="distance" value="10"
<?php if ($distancestore['value'] == '10') echo "checked='checked'"; ?> />
10 <?php echo $dict->transl('distance_km'); ?>
</label>
<?php
}
try this i think you have to use ternary operator to resolve this issue.
<input type="radio" name="distance" value="10"
<?php $select = $distancestore['value']=='10'?'checked':'';
echo $select; ?>
10 <?php echo $dict->transl('distance_km'); ?>
I have this PHP/HTML Code that is selecting data from a MySQL Database:
<?php
$sql3="SELECT * from property_images where property_seq = '".$property["sequence"]."' ";
$rs3=mysql_query($sql3,$conn);
while($property_img=mysql_fetch_array($rs3))
{
?><tr>
<td colspan="2"><img src="http://domain.co.uk/img/property-images/<?php echo $property_img["image"]; ?>" width="80px" height="80px" /></td>
<td colspan="2"><input type="checkbox" name="image1" value="Y" <?php if($property_img["image1"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image2" value="Y" <?php if($property_img["image2"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image3" value="Y" <?php if($property_img["image3"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image4" value="Y" <?php if($property_img["image4"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image5" value="Y" <?php if($property_img["image5"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image6" value="Y" <?php if($property_img["image6"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image7" value="Y" <?php if($property_img["image7"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image8" value="Y" <?php if($property_img["image8"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image9" value="Y" <?php if($property_img["image9"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image10" value="Y" <?php if($property_img["image10"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image11" value="Y" <?php if($property_img["image11"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image12" value="Y" <?php if($property_img["image12"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image13" value="Y" <?php if($property_img["image13"] == 'Y') { echo 'checked="checked"'; } ?> /></td>
</tr><?php
}
?>
each row, has its own image with the columns image1 - image13
i want to update the table with the checkboxes that are checked and unchecked on the form update
how is this possible?
Thanks
If you mean that you want to update the $property_img["imagexx"] val on db depending on the user click on the checkbox you must use ajax.
Fire an event on triggering each checkbox and send the value to a php page that update the php.
Jquery Ajax function can help you on this task.
L
name all your checkboxes images[]
<input type="checkbox" name="images[]" value="1" />
<input type="checkbox" name="images[]" value="2" />
<input type="checkbox" name="images[]" value="3" />
<input type="checkbox" name="images[]" value="4" />
You can then update with PHP :
<?php
$q = "UPDATE property_images SET";
foreach($_POST["images"] as $image) {
$q .= " image" . $image ." = 'Y', ";
}
$q .= " property_seq = '".$property["sequence"]."' WHERE property_seq = '".$property["sequence"]."'";
mysql_query($q);
?>
First of all, mysql_ functions are deprecated, please google mysqli_ or PDO, mysql_ won't be supported on future versions, and is unsafe, etc
Your html output could be much simpler with a loop, also have an eye on putting the sequence number on a hidden field or something first:
<?php
$sql3="SELECT * from property_images where property_seq = '".$property["sequence"]."' ";
$rs3=mysql_query($sql3,$conn);
while($property_img=mysql_fetch_array($rs3)){
?>
<tr>
<td colspan="2"><img src="http://domain.co.uk/img/property-images/<?php echo $property_img["image"]; ?>" width="80px" height="80px" /></td>
<!-- THIS IS VERY IMPORTANT: send the sequence or ID through a hidden field, to know which row you are gonna update later-->
<input type="hidden" name="sequence" value="<?php echo $property_img['sequence']; ?>"/>
<td colspan="2">
<?php for($i = 1; $i <= 13; $i++): ?>
<input type="checkbox" name="images[]>" value="<?php echo $i; ?>" <?php if($property_img["image$i"] == 'Y') { echo 'checked="checked"'; } ?> />
<?php endfor ?>
</td>
</tr>
<?php
}
?>
Then this is the next page where the update is done, have a good look at the comments:
<?php
//Handle as you want the situation when there are no images selected instead of using an exit(), I used it here just for the quickness;
if(count($_POST['images'] < 1) exit('no images where selected to update');
$images = array();
for($i = 1; $i <= 13; $i++){
$string = "image$i = ";
if(in_array($i, $_POST['images'])){
$string .= "'Y'"; //notice the double quoting here, it's important
} else {
//This updates the table so if it was checked when loaded, but unchecked by the user, this makes the change!
$string .= "'N'";
}
}
//This creates a string like: image1 = 'Y', images2 = 'N', etc...
$images = implode(', ', $images );
//try to sanitize the query first ... I won't cos am just showing you your question xD
$sql = "UPDATE property_images SET $images WHERE property_seq = " . mysql_real_escape_string($_POST[sequence]) . ";
mysql_query($sql,$conn);
?>