I'm having problem in getting the value of a checkbox when checking the value first in DB then change to POST value after submitting the form. Here's my sample:
1: I need to check first in my db if it's need to be checked or not.
<form action="" method="POST">
<input type="checkbox" name="chckbox" <?=$value['chckbox'] == 'ok' ? 'checked' : ''?> />
<input type="submit" name="btnsubmit" />
</form>
2: Then if I already check in DB I want to get the POST value of checkbox after submitting the form if it is checked or not.
<form action="" method="POST">
<input type="checkbox" name="chckbox" <?=isset($_POST['chckbox']) ? 'checked' : ''?> />
<input type="submit" name="btnsubmit" />
</form>
My question is... how to combined my two sample when I'm updating the form?
Any help is appreciated!
Thanks.
How about this,
<input type="checkbox" name="chckbox" <?php echo $value['chckbox'] == 'ok' || isset($_POST['chckbox']) ? 'checked' : '' ?> >
After submitting the form,
<?php
$checked = $value['chckbox'] == 'ok'?'checked':'';
if(isset($_POST['chckbox']) && $_POST['chckbox']=='on'){
$checked = "checked";
}elseif(count($_POST)>0 && !isset($_POST['chckbox'])){
$checked = "";
}
?>
<input type="checkbox" name="chckbox" value="on" <?php echo $checked; ?> >
html:
<form action="" method="POST">
<input type="checkbox" name="code" value="'.$row['Code'].'">
<input type="submit" name="btnsubmit" />
</form>
php:
$Code=$_REQUEST['code'];
Related
I have one dynamic checkbox (can be ++) that needs to stay checked after user submit.
I already tried some tricks like using a hidden input before my checkbox code in HTML. Now I stuck in doing an isset(_POST) and the checkbox didn't stay checked.
Here's my HTML:
<input type="hidden" name="hidden_name[]" id="hidden_name0">
<input type="checkbox" name ="name[]" id="name0">
<label for="name">Name</label>
--------UPDATE--------
And in my PHP file, here's the code:
$valueName = array();
if(isset($_POST["hidden_name"]))
{
foreach($_GET['hidden_name'] as $value)
{
array_push($valueName,$value);
}
}
That code doesn't work :/
How to make the checkbox stays checked after user check it and submit the form? What should I write in PHP? Do I really need a hidden input before my checkbox?
isset might be returning false since the submitted value is NULL. I suggest adding a value='1' on the hidden input field -- or are you updating that field with the corresponding checkbox value?
Alternatively, you have some other options:
Change the name for each dynamic checkbox to have an identifier.
<input type="checkbox" name="name-x" <?php echo isset( $_POST['name-x'] ) ? 'checked="checked"' : '' ?> />
Where x could be your dynamic ID. Notice the addition of the PHP code and using a ternary operator, you can check the checkbox if the $_POST['name-x'] is set.
Add a value to the checkbox.
<input type="checkbox" name="name[]" id="name0" value="name0" />
<input type="checkbox" name="name[]" id="name1" value="name1" />
<input type="checkbox" name="name[]" id="name2" value="name2" />
However, you need to match this value in your PHP code.
<?php
if ( isset( $_POST['name'] ) ) {
$values = array();
foreach( $_POST['name'] as $value ) {
array_push( $values, $value );
}
}
?>
Then you have to modify your checkbox again to have inline PHP.
<input type="checkbox" name="name[]" id="name0" value="name0" <?php echo in_array( "name0", $values ) ? 'checked="checked"' : '' ?> />
<input type="checkbox" name="name[]" id="name1" value="name1" <?php echo in_array( "name1", $values ) ? 'checked="checked"' : '' ?> />
<input type="checkbox" name="name[]" id="name2" value="name2" <?php echo in_array( "name2", $values ) ? 'checked="checked"' : '' ?> />
You can also create a function to display these inline codes to make it much cleaner. HTH!
i assume that you have a form that have some checkboxes and try to check which checkbox is checked. here is an example code
<form action="" method="post">
<label for="name">Name 1</label>
<input type="checkbox" name ="name[]" value="name1">
<label for="name">Name 2</label>
<input type="checkbox" name ="name[]" value="name2">
<label for="name">Name 3</label>
<input type="checkbox" name ="name[]" value="name3">
<label for="name">Name 4</label>
<input type="checkbox" name ="name[]" value="name4">
<button type="submit" name="submit">Submit</button>
</form>
and the php code
<?php
if(isset($_POST["submit"])){
$valueName = array();
foreach($_POST['name'] as $value){
array_push($valueName,$value);
print_r($valueName);
}
?>
How can I make my check box is checked using PHP, when i visit the page later i want previously selected check boxes are checked
<input name="product[]" type="checkbox" value="1" />
Start Session as
<?php
session_start();
$session_products = array();
if(array_key_exists("products", $_SESSION))
{
if($_SESSION["products"] != null)
{
$session_products = $_SESSION["products"];
}
}
?>
Change your code as follows
<input name="product[]" type="checkbox" value="1" <?php if(in_array("1", $session_products)) echo "checked='checked'"; ?>/>
You can add a checked.
Here an example:
<input name="product[]" type="checkbox" value="1" checked />
By the way, your value=1 seems to be wrong. Normally you use `value to distinguish items.
Example:
<input name="product[]" type="checkbox" value="dvd" checked />
<input name="product[]" type="checkbox" value="cd" />
to do something like this
with php
<input name="product[]" type="checkbox" value="1" <?php echo ($value =="1") ? "checked" : ""; ?> />
where $value is the value from database
I have 3 checkboxes used for searching
<input type="checkbox" onChange="this.form.submit()" ',$var1 ? ' class="checkon" checked="checked"' : '','name="c1" value="c1">
<input type="checkbox" onChange="this.form.submit()" ',$var2 ? ' class="checkon" checked="checked"' : '',' name="c2" value="c2">';
<input type="checkbox" onChange="this.form.submit()" ',$var3 ? ' class="checkon" checked="checked"' : '',' name="c3" value="c3">';
I want them to all be on by default, but if I set them to on:
$_POST['c1'] = 'on';
Then when I uncheck the box it is still on? How do I get it on by default but off when I uncheck it?
I am not sure why you have an onChange event with these inputs. You should let the user check and uncheck whichever boxes they want and then submit all the data together. Then do if(isset($_POST['c1'])){ //Box was checked }. Also have checked="checked" just as a HTML attribute (so <input type="checkbox" name="c1" value="on" checked="checked" />) rather than doing a shorthand if statement in an onChange event.
Here would be the way I would do it:
<form action="" method="post">
<fieldset>
<input type="checkbox" name="c1" value="on" <?php if (isChecked('c1')) echo 'checked="checked"'; ?> />
<input type="checkbox" name="c2" value="on" <?php if (isChecked('c2')) echo 'checked="checked"'; ?> />
<input type="checkbox" name="c3" value="on" <?php if (isChecked('c3')) echo 'checked="checked"'; ?> />
<input type="submit" name="submit" value="Go" />
</fieldset>
</form>
<?php
function isChecked($name) { //Darkbee's edit
return empty($_POST) || isset($_POST[$name]);
}
if(isset($_POST['submit'])){
if(isset($_POST['c1'])){
//Checkbox1 was checked when the form was submitted, code goes here
}
//Do the same for c2 & c3 as necessary
}
?>
Hope this helps!!
I'm trying to accomplish one simple task (but is not simple for me).
I have a form and I'm having a trouble with this check box.
<input type="checkbox" name="b"
<?php if (isset($_POST[b])) echo "value='y'"; else echo "value='n'"; ?>/>
I'm not sure if I use the right one, but it doesn't work for me.
So basically I want the value of the input of b will be y if the checkbox is checked else it will always be n if the checkbox is unchecked.
That's not how a checkbox works.
It's checked when the checked attribute is there.
<input type="checkbox" name="a" value="a" checked /> Checked
<input type="checkbox" name="a" value="a" /> NOT Checked
So you want to use
<input type="checkbox" name="a" value="a" <?php echo isset($_POST['b']) ? "checked" : ""; ?>/>
Now if $_POST['b'] is set, the checkbox will be checked.
Also, you have $_POST[b]. The b should be in quotes. It should be $_POST['b']
You have to use two conditions one is for showing checked/unchecked and second for showing y/n
<input type="checkbox" name="b" <?php echo (isset($_POST['b'])?"value='y'":"value='n'")?>
<?php echo (isset($_POST['b'])?"checked":"") ?> />
tested code!
<form method="post">
<input type="checkbox" name="b" <?php if (isset($_POST['b'])) echo "value='y'"; else echo "value='n'"; ?>/>
<input type="submit" name="asd" value="asd">
</form>
So go with the following
<?php if (isset($_POST['b'])) echo "value='y'"; else echo "value='n'"; ?>
This worked for me:
first: Giving the checkbox a default value
then: assign the desired value only if the box is checked.
<input type="hidden" name="b" value="n">
<input type="checkbox" name="b" value="y" >
*Approved by Deployment:
<input type="radio" name="dep_approval_status" value="Approved"
<?php
if ($deploy['dep_approval_status'] === "Approved")
{
echo ' checked';
}
?>
/> Yes, approved
<input type="radio" name="dep_approval_status" value="Not Approved"
<?php
if ($deploy['dep_approval_status'] === "Not Approved")
{
echo ' checked';
}
?>
/> Not Approved
I would like the following:
Data = "123";
if checkbox clicked {Data="";}
Submit-button
That was the best way I could explain it.
I have tried, but I can't get the checkbox to overwrite the data with an empty value.
<form action="form.php" method="post">
<input type="text" value="" name="data" />
<input type="checkbox" value="checkbox" name="checkbox" />
<input type="submit" value="Submit" name="submit" />
</form>
That is your form, and this is your PHP
<?php
if(isset($_POST['submit']))
{
if(isset($_POST['checkbox']))
{
$_POST['data'] = "";
}
/* the rest of your form */
}
?>