I have a input in a form
<form name="frmAdd" method="POST" action="/index.php?a=save">
Status : <input type="checkbox" id="status" name="chkActive" value="" ><label for="status">Active</label>
</form>
but when I am calling the value of it, via $_POST['chkActive'], it's giving same value on that page. Whether I have checked the value or not.
Please tell me how can I know that this checkbox is checked or not (in PHP).
If you are using just a single checkbox you can do this :
<input type="checkbox" id="status" name="chkActive" value="1" >
within PHP
if (isset($_POST['chkActive'])) {
//its checked
}
but you need to ensure there is a value set within the HTML
<form name="frmAdd" method="POST" action="/index.php?a=save">
Status : <input type="checkbox" id="status" name="chkActive" value="1" ><label for="status">Active</label>
</form>
<?php if($_POST['chkActive']=='1'){
echo "is Active";
}else{
echo "not Active";
}
Checkbox only passes the value if is checked.
to clarify how it works.
<input type="checkbox" id="status" name="chkActive"
if the checkbox not ticked (checked by user) nothing sent to server. "chkActive" will not exist. So we use isset() to check it.
Related
I have this HTML code:
<form action="index.php" method="post">
<input id="firstCheck" type="checkbox" name="test[]" value="first" checked>
<label for="firstCheck">1</label><br>
<input id="secondCheck" type="checkbox" name="test[]" value="second" checked>
<label for="secondCheck">2</label><br>
<input type="submit" name="test_submit" value="Send">
</form>
Also I have this PHP code:
function create_cookies()
{
if (isset($_POST['test_submit'])) {
$checkboxes_array = $_POST['test'];
setcookie("testcookie1234556", json_encode($checkboxes_array), time()+3600);
}
}
create_cookies();
function check_cookies()
{
if (isset($_COOKIE['testcookie1234556'])) {
$my_cookie_array = json_decode($_COOKIE['testcookie1234556']);
return $my_cookie_array;
} else {
return false;
}
}
check_cookies();
As you can see, I have got all checked checkboxes and stored it to the php cookie.
Now, when someone is logging to the system, all checkboxes is checked by default. But I need them to be checked or unchecked based on the info from php cookie!
I mean, for example, if user logged on to the system and unchecked some checkboxes, logged off, then logged in again, previously unchecked checkboxes must remains unchecked.
Please, give me some advices for doing it.
Put your cookie in an array say
$cookie_aray=check_cookies();
and check it every time you load a check box, whether it was selected or not.
<html><form action="abc.php" method="post">
<input id="firstCheck" type="checkbox" name="test[]" value="first" <?php if(in_array('first',$cookie_aray)) echo 'checked';?>>
<label for="firstCheck">1</label><br>
<input id="secondCheck" type="checkbox" name="test[]" value="second" <?php if(in_array('first',$cookie_aray)) echo 'checked';?>>
<label for="secondCheck">2</label><br>
<input type="submit" name="test_submit" value="Send">
two problem i have:
first:
i want users see my form with no prechecked in radio buttons but it does when they see it at first time(as you see)(explain that i use checked to show user which radio he/she selected after pushing the button)
second:
why when i name submit button "select sex" and push it in the form, it doesn't echo "it's done" but when i name it "select" it works?! i want my submit name has two words.
and the codes:
<html>
<body>
<?php
if(isset($_POST['select sex']))
echo "it's done";
?>
<form name="input" action="" method="post">
<input type="radio" name="sex" value="male" checked="
<?php if(isset($_POST['select sex']) and $_POST['sex']=='male') echo 'checked'; else echo '';?>
"> Male<br />
<input type="radio" name="sex" value="female" checked="
<?php if(isset($_POST['select sex']) and $_POST['sex']=='female') echo 'checked'; else echo '';?>
"> Female<br />
<input type="submit" name="select sex" value="Submit" />
</form>
</body>
For checkbox, not mentioned checked attribute for any of the check box option.
For submit button, normally we are not using the space in field name and this is the best practice. Though you have used then you have written the wrong code. Please check below updated code line for if statement.
if(isset($_POST['select sex']))
This is my form :
<input type="checkbox" name="dept" value="sales" <?php if(isset($_POST['sales'])) echo "checked='checked'"; ?> onclick="this.form.submit();" /><br />
When i click the checkbox, the page is refreshed with the ?dept=sales in the URL. Just as i want it to be. But the checkbox is unchecked. I want the checkbox to be checked. And when the checkbox is unchecked i want the ?dept=sales to be removed from teh URL.
Thanks.
Your checkbox's name is dept, not sales. The value of your checkbox is sales. This means that if you want to access the value of your checkbox, you will need to access it via $_POST['dept'], not $_POST['sales']. If your form method isn't declared as method="post", use $_GET['dept'] instead of $_POST['dept'].
At first check your check box name, It's dept but you fetch from sales $_POST, another hint is that if your request is shown on the url then it's get not post, If you want to remove parameter from your url add method="post" to your form, At last your code should be like this:
<form action="your action here" method="post">
<input type="checkbox" name="sales" value="sales" <?php if(isset($_POST['sales'])) echo "checked='checked'"; ?> onclick="this.form.submit();" /><br />
</form>
You're checking for an input with the name "sales" change $_POST['sales'] to $_POST['dept'] that'll work :)
Submit the form only when the checked property true like,
<input type="checkbox" name="dept" value="sales"
<?php if($_REQUEST['dept']=='sales')) echo "checked='checked'"; ?>
onclick="if(this.checked==true) this.form.submit();" /><br />
I am trying to save fields data after submited, Becouse after all the fields are good to go but lets say at the server side the user name is already taken so the form return empty and i dont want that there is the option to do it with PHP like that:
<input value="<?php if(isset($userName)) echo $userName; ?>" />
But the problem is with the radio input, If can some one think about solution about the radio with PHP i will be very thankful, Also i was thinking about Javascript so i will have cleaned code and i was thinking about taking the values from the URL but i am using POST for security reasons.
Summary: If anyone have a solution with PHP or Javascript i will be very thankful, Thank you all and have a nice day.
Try this
<form name="myform" action="" method="post">
<input type="radio" name="language" value="Java" <?php echo(#$_POST['language'] == 'Java'?"checked":""); ?> /> Java
<input type="radio" name="language" value="VB.Net" <?php echo(#$_POST['language'] == 'VB.Net'?"checked":""); ?> /> VB.Net
<input type="radio" name="language" value="PHP" <?php echo(#$_POST['language'] == 'PHP'?"checked":""); ?> /> PHP
<input type="submit" />
I think this may help you.
<input type="radio" value="choice1" name="radio_name" <?php echo(#$_POST['radio_name'] == 'on'?"checked":""); ?> />
If you want to automatically select a radio input you can add the attribute checked to it. What you are going to need will look like this :
<form method="POST">
<?php
// You have some short of list of possible value //
$arrRadioValues = array("value1", "value2", "value3");
// You display them //
for ($i=0; $i<count($arrRadioValues); $i++) {
?>
<input
type="radio"
name="radioInputName"
value="<?php echo $arrRadioValues[$i]; ?>"
<!-- If the value that was posted is the current one we have to add the "checked" so that it gets selected -->
<?php if (isset($_POST['radioInputName']) && $_POST['radioInputName'] == $arrRadioValues[$i]) { echo " checked"; } ?> />
<?php
}
?>
<input type="submit" />
</form>
Adding the checked attribute works a little bit in the same as setting a value to an input. It's just that instead of defining the value attributes, you define the checked attribute when you want that radio to be selected.
How do I check a checkbox?
I've tried 1, On, and Yes. That doesn't work. Putting the worked "checked" alone works, but then how do I check with PHP after a form post of the checkbox is checked?
<input type="checkbox" class="inputcheckbox" id="newmsg" name=chk[newmsg2] value="1" />
A checkbox will only be a successful control if it is checked.
Controls that are not successful are not submitted as data.
Therefore, you can tell if a checkbox is checked by seeing if its value has been submitted.
E.g.
if ($_POST['chk']['newmsg2'] == 1) {
<input type="checkbox" class="inputcheckbox" id="newmsg" name=chk[newmsg2] value="1" <?php if ($_POST['chk']['newmsg2']): ?>checked="checked"<?php endif; ?> />
Here is the code;
<form action="test.php" method="POST">
<input type="checkbox" class="inputcheckbox" id="newmsg" name=chk[newmsg2] value="1" />
<input type="submit">
</form>
<?php
$check = $_POST['chk']['newmsg2'];
echo "***$check****"
?>
If it is checked $check will show 1.