i cant seem to make the checkbox appear as checked if this statement turns out to be true. the array passed is working fine and everything is in palce but i dont seem to find a way where i can make the checkbox checked if the value of BSS is there
<?php if(isset($_GET['cng']) && array_search("BSS", $scharr))
{
echo "checked=''";
};
?>/>
BSS
</label>
try below code:
<label for="one">
<input type="checkbox" name="school[]" value="BSS" <?php echo isset( $_GET['cng'] ) && in_array("BSS", $scharr) ? 'checked' : ''; }; ?> />
BSS </label>
<input type="checkbox" <?php if(isset($_GET['cng']) && isset($scharr['BSS'])) echo "checked"?> />
Try this code
<input type="checkbox" <?php if(isset($_GET['cng']) && array_search("BSS", $scharr)) { echo "checked";}>
Related
I have a checkbox that is always checked no matter if I checked the box or not.
Here is how the checkbox is currently setup:
<input class="classname" id="check" value="1" <?php echo (($temp['test']) ? 'checked' : ''); ?> name="check" type="checkbox" />
Here is how the check box value is being captured:
$check = $_POST['check'] ?? 0;
I also tried the following code as well:
$check = (isset($_POST['check']) == '1' ? '1' : '0');
I tried the hidden field approach as well and it didn't work. Any ideas on why the checkbox is showing marked even though I removed the check.
I think Drussy might be onto something, where does $_POST['test'] come from?
Try this code:
$checked = isset($_POST['check']) ? 'checked' : '';
<input class="classname" id="check" value="1" <?php echo $checked; ?> name="check" type="checkbox" />
I know there's "PHP keep checkbox checked after submitting form" on here, but that thread does not solve my problem, because I have multiple checkbox, what I need is when you check a checkbox, this stay checked after submit.
At the moment with this code nothing happens, I tried another way but when I check "id7" checkbox, all the checkbox get checked.
I have to know which checkbox was checked by the id that I give it, but I do not know how.
while ($fila = mysql_fetch_array($rs)) {
echo utf8_encode("
<tr>
<td>
".$fila['title']."
</td>
");?>
<td>
<input type="checkbox" name="checklist[]" value="<?php echo htmlspecialchars($fila['id']); ?>" <?php if(isset($_POST['checklist[]']) && is_array($_POST['checklist[]']) && in_array('$fila', $_POST['checklist[]'])) echo 'checked="checked"'; ?> />
</td>
<?php
}
}
?>
First, the value of checkbox is $fila['id'] so when you are checking, use $fila['id'] instead of $fila. Also, when PHP receive array input fields with [] in their names the [] will be removed so that the correct POST variable is $_POST['checklist'].
Try changing this line:
<input type="checkbox" name="checklist[]" value="<?php echo htmlspecialchars($fila['id']); ?>" <?php if(isset($_POST['checklist[]']) && is_array($_POST['checklist[]']) && in_array('$fila', $_POST['checklist[]'])) echo 'checked="checked"'; ?> />
to
<input type="checkbox" name="checklist[]" value="<?php echo htmlspecialchars($fila['id']); ?>" <?php if(isset($_POST['checklist']) && is_array($_POST['checklist']) && in_array($fila['id'], $_POST['checklist'])) echo 'checked="checked"'; ?> />
"name" is like a variable name - by using checklist[] you're defining an array as the variable.
Why not just name each checkbox properly? When the form is submitted, use the contents of $_POST to set each variable into the user's $_SESSION.
If the page is refreshed, use the values from $_SESSION to determine if the checkbox should be ticked. Something like (untested):
<input type="checkbox" name="vehicle" value="Bike"
<?php if (isset($_SESSION['bike_checked']) echo 'checked'; ?>> I have a bike<br>
<input type="checkbox" name="vehicle" value="Car"
<?php if (isset($_SESSION['car_checked']) echo 'checked'; ?>> I have a car<br>
<input type="submit" value="Submit">
I am having a problem in retrieving checkboxes from the database.
Actually I am making edit form by using the input form which insert the values in database.
I am facing problem with checkboxes and other inputs works fine.
My code looks like
<input type="checkbox" name="proizvodi[]" value="0" <?if ($row['PROIZVODI'] == '0') $checked = 'checked="checked"'; ?> checked="checked"><b>product1<b/> <input type="checkbox" name="proizvodi[]" value="1" <?if ($row['PROIZVODI'] == '1') $checked = 'checked="checked"'; ?> checked="checked"><b>product2<b/> <br/>
<input type="checkbox" name="proizvodi[]" value="2" <?if ($row['PROIZVODI'] == '2') $checked = 'checked="checked"'; ?> checked="checked"><b>product3</b> <br/>
To make it more clear, I want only checkboes that are stored in db to be checked. for ex if value 1 is stored in db, product1 checkbox will be checked, if value 1 is no stored then check box will be not checked.
I appreciate any help or advice.
Thanks in advance!
How about this? You need to echo the 'checked="checked"' and do not assign it to a variable
<input type="checkbox" name="proizvodi[]" value="0" <?php if (strpos($row['PROIZVODI'],'0') !== false) echo 'checked="checked"'; ?>><b>product1<b/>
<input type="checkbox" name="proizvodi[]" value="1" <?php if (strpos($row['PROIZVODI'],'1') !== false) echo 'checked="checked"'; ?>><b>product2<b/> <br/>
<input type="checkbox" name="proizvodi[]" value="2" <?php if (strpos($row['PROIZVODI'],'2') !== false) echo 'checked="checked"'; ?>><b>product3</b> <br/>
Write your rows like this don't make it any harder than it is:
<input type="checkbox" name="proizvodi[]" value="0" <?php if ($row['PROIZVODI'] == '0') { ?> checked="checked" <?php } ?> /><b>product1<b/>
I am trying to get a checkbox checked by default, but everything I have tried doesn't seem to work. I don't know if it has to do with the PHP that is in the code.
function show_subscription_checkbox ($id='0') {
global $sg_subscribe;
sg_subscribe_start();
if ( $sg_subscribe->checkbox_shown ) return $id;
if ( !$email = $sg_subscribe->current_viewer_subscription_status() ) :
$checked_status = ( !empty($_COOKIE['subscribe_checkbox_'.COOKIEHASH]) && 'checked' == $_COOKIE['subscribe_checkbox_'.COOKIEHASH] ) ? true : false;
?>
<p <?php if ($sg_subscribe->clear_both) echo 'style="clear: both;" '; ?>class="subscribe-to-comments">
<input type="checkbox" name="subscribe" id="subscribe" value="subscribe" style="width: auto;" <?php if ( $checked_status ) echo 'checked="checked" '; ?>/>
<label for="subscribe"><?php echo $sg_subscribe->not_subscribed_text; ?></label>
</p>
This is a wordpress plugin that allows you to subscribe to blog comments.
I have tried
echo 'checked=\"checked\" ';
echo 'checked="checked" ' ;
echo 'checked> ';
The plugin author states that you used to be able to default check the checkbox but not anymore.
Since this is showing up in google for "default checked checkbox", I figured I'd answer it. Alpay was right: The correct way to ensure that a checkbox is checked by default is like so (followed by an example of one that is not checked):
<input type="checkbox" name="vehicle" value="Car" checked> I have a car
<input type="checkbox" name="vehicle" value="Bike"> I have a bike
Answer was found on w3schools. The author of the original question was having trouble with his PHP code, which is not at all related to the question title.
In HTML, if you want a checkbox to be checked by default, see the following;
<input type="checkbox" name="name1" value="uc"> This checkbox is unchecked <br>
<input type="checkbox" name="name2" value="c" checked> This checkbox is checked<br>
So, you might consider changing
<?php if ( $checked_status ) echo 'checked="checked" '; ?>
to
<?php if ( $checked_status ) echo 'checked'; ?>
The problem is not in the HTML markup being generated; echo 'checked="checked" ', as in the question, works well, and so would the simpler echo 'checked'.
The problem is with the condition $checked_status. You are testing for a variable that is undefined, as far as the code posted is considered.
I had the same problem. I figured out that I was trying to put the checkbox into a table but left out the and.
didn't check:
if(!$stump){echo '<input type="checkbox" name="stump" value="stump" checked="checked"><b> Stump Job!</b>';}
checked:
if(!$stump){echo '<td><input type="checkbox" name="stump" value="stump" checked="checked"><b> Stump Job!</b></td>';}
In regular PHP you can use this to "save" the checked state after its been submitted.
<form name="checkbox" method="post" action="#" >
<input type="checkbox" name="checkbox1" value="Bike" <?php if ($_POST['checkbox1']=="Bike") echo "checked";?>>I have a bike
<input type="checkbox" name="checkbox2" value="Car" <?php if ($_POST['checkbox2']=="Car") echo "checked";?>>I have a car
<input type="submit" name="submit" value="Display this Data" />
if you want to use this data for something else after the submit, just add:
<?php
if(isset($_POST['checkbox1']) OR isset($_POST['checkbox2']) )
{
echo "for 1 : ".$_POST['checkbox1']."for 2: ".$_POST['checkbox2'];
}
?>
if you want to clear the form (basically clear all the post data) you can add:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="submit" name="Clear all form data" value= "Clear this form">
</form>
HI
i'm using a php page and i need to keep the value of and check box and radio button (checked or not checked) after post page.
how could i make it?
thanks
First get the radio button value.
$radiobuttonvalue = $_POST['radiobuttoname']
Then for each radio button with the same name, do this
<input type="radio" name="radiobuttonname" value="value" id="radiobuttonname" <?php if($radiobuttonvalue == "value") { echo 'checked="checked"';} ?>
You need something like:-
<?php
$postCheckboxName = '';
if (isset($_POST['checkbox_name']) || 'any_value' == $_POST['checkbox_name']) {
$postCheckboxName = ' checked="checked"';
}
?>
<input type="checkbox" name="checkbox_name" value="any_value"<?php echo $postCheckboxName;?> />
<?php
$postRadioName = '';
if (isset($_POST['radio_name']) || 'any_other_value' == $_POST['radio_name']) {
$postRadioName = ' checked="checked"';
}
?>
<input type="checkbox" name="radio_name" value="any_other_value"<?php echo $postRadioName;?> />
This code should get you going. I'm basically checking whether the POST value of either the checkbox / radio element is set or not & whether the corresponding element's value matches with my respective element's value or not.
Hope it helps.
Something like this:
<?php if (isset($_POST['checkbox_name']))?>
<input type="checkbox" checked="checked" value="<?php echo $_POST['checkbox_name'];?>" />
<?php} ?>
<?php if (isset($_POST['radio_name']))?>
<input type="radio" checked="checked" value="<?php echo $_POST['radio_name'];?>" />
<?php} ?>
What happens is that you check if the input variables are in the $_POST and if so you add checked="checked" to the input fields to make them checked.
This worked for me, and is self explanatory
sample code usage:
<div class="form-group">
<label class="radio-inline">
<input type="radio" name="time" value="lunch" <?php if (isset($_POST[ 'time']) && $_POST[ 'time']=='lunch' ){echo ' checked="checked"';}?>>Lunch</label>
<label class="radio-inline">
<input type="radio" name="time" value="dinner" <?php if (isset($_POST[ 'time']) && $_POST[ 'time']=='dinner' ){echo ' checked="checked"';}?>>Dinner</label>
</div>