onclick set php session to keep checkbox checked. On uncheck end session - php

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 />

Related

How to remember checkbox preference in user account in html/php site

Good day!
I'm doing a site. There is a checkbox which update the option I choose in phpmyadmin.
But if I press submit, then f5/reload page, the preference I choose disappears on client/html side, (look the image for better understanding)
Image, open me!
So basically I want the site remember my choice in the "user account"
I tried with
<form action="pr.php" method="POST">
<h3 style="color:red;">Numbers?</h3>
<input type="checkbox" name="a1" value="1" <?php if(isset($_POST['a1'])) echo "checked='checked'"; ?> /><label>One</label><br/>
<input type="checkbox" name="a2" value="2" <?php if(isset($_POST['a2'])) echo "checked='checked'"; ?> /><label>Two</label><br/>
<input type="checkbox" name="a3" value="3" <?php if(isset($_POST['a3'])) echo "checked='checked'"; ?> /><label>3</label><br/>
<input type="checkbox" name="a4" value="4" <?php if(!is_null($_POST['a4'])) echo "checked='checked'"; ?> /><label>Four</label><br/>
<input type="submit" value="submit" name="submit">
It doesnt work with isset, empty or is_null.
Thanks for all!
Unless you save the information on a database, even if you clicked on submit, after you refresh the webpage you are going to loose the the values of the variables.
You could use cookies to save the value of the choice that was checked:
setcookie("checkbox1","checked");
First parameter is name of the cookie, second one is the value of the cookie, and you can add a third one with the time you want the cookie to have before it expires.
And in your if condition, you could do this:
<?php if($_COOKIE['a1'] == "checked") echo "checked='checked'"; >
For more info, check this link.

How to store checkbox value as 1 and 0

It doesn't stores value in my database. It always stores "0" value even if i checked the item.
Here is my code:
<input type="checkbox" name="parental" <?php $parental = (isset($_POST['parental'])) ? 0 : 1;?>/>
<input type="hidden" name="parental" />
You forgot the attribute value and also add echo in php code
<input type="checkbox" name="parental" value="<?php echo $parental = (isset($_POST['parental'])) ? 0 : 1;?>" />
also why you have this element?
<input type="hidden" name="parental" />
It will overwrite the checkbox since both have the same name, either remove the hidden element or change the name
To be clear, checkbox value should always be the same, in your case 1, since checkbox passes it's value when it is checked, and does not when it is not. Also, if you want to use hidden field, you should use it before checkbox, so in case checkbox is not checked, it still passes 0.
For example:
<input type="hidden" name="parental" value="0" />
<input type="checkbox" name="parental" value="1" />
If the checkbox is checked $_POST["parental"] will equal 1, else it will equal 0. There are better ways to achieve this in php after the submit, though.
Also, regarding your conditional, I suppose you want to check the checkbox if parental is 1, which you should do like this, for example:
<input type="checkbox" name="parental" value="1" <? echo $_POST["parental"]=="1"?"checked":""; ?> />
Changing input type checkbox value, does not really have much sense in your case, if I understood right.

Error in Radio Button if not checked

Hello I am a beginner in PHP
I have created a form and there are few radio button with YES/NO options... every thing is working fine but if i submit form without clicking radio button options, it shows the following error... I know there is some code to be written but not getting exactly
'Notice: Undefined index: ws_id in D:\xampp\htdocs\mc\db_def.php on line 18'
Solution in your php use isset() to check
Use required in input tag "very basic validation"
<label for="input1">1:</label><input type="radio" name="test" id="input1" required value="1" /><br />
<label for="input2">2:</label><input type="radio" name="test" id="input2" value="2" /><br />
<label for="input3">3:</label><input type="radio" name="test" id="input3" value="3" /><br />
Check this fiddle http://jsfiddle.net/Pw5vQ/
Solution 1:
If you won't check radio button or checkbox the form won't post it so you will not find these inputs on the $_POST superglobal variable.
Try to var_dump($_POST); with checked and unchecked version.
Just check the required form name with isset() like this isset($_POST['ws_id']).
If isset gives you false it means it won't checked.
Solution 2:
You can preset the inputs with html attribute checked="checked".
For example:
You would like to check the NO radio button for default:
<input type="radio" name="ws_id" value="YES"/>Yes
<input type="radio" name="ws_id" value="NO" checked="checked"/>No
I guess you are getting this error when first accessing the page, so there is no sent data, so your $_POST['ws_id'] or $_GET['ws_id'] or $_REQUEST['ws_id'] has no value in it as there is no data sent.
Use
if (isset($_POST['id'])) // do what you were doing on line 18
//or
if (isset($_GET['id'])) // do what you were doing on line 18
//or
if (isset($_REQUEST['id'])) // do what you were doing on line 18
depending on your choice.

Confusion in checkbox in PHP dynamic values

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.

Get POST data from multiple checkboxes?

Im trying to create a form using PHP and I cant seem to find a tutorial on what I need so thought Id ask on here.
I have a multiple checkbox option on my page...
<li>
<label>What service are you enquiring about?</label>
<input type="checkbox" value="Static guarding" name="service">Static guarding<br>
<input type="checkbox" value="Mobile Patrols" name="service">Mobile Patrols<br>
<input type="checkbox" value="Alarm response escorting" name="service">Alarm response escorting<br>
<input type="checkbox" value="Alarm response/ Keyholding" name="service">Alarm response/ Keyholding<br>
<input type="checkbox" value="Other" name="service">Other<input type="hidden" value="Other" name="service"></span>
</li>
I'm not sure however how to collect all checkbox values using POST method?
if i use
$service = $_POST['service'];
I only get 'other' returned
Name the fields like service[] instead of service, then you'll be able to access it as array. After that, you can apply regular functions to arrays:
Check if a certain value was selected:
if (in_array("Other", $_POST['service'])) { /* Other was selected */}
Get a single newline-separated string with all selected options:
echo implode("\n", $_POST['service']);
Loop through all selected checkboxes:
foreach ($_POST['service'] as $service) {
echo "You selected: $service <br>";
}
Currently it's just catching your last hidden input. Why do you have that hidden input there at all? If you want to gather information if the "Other" box is checked, then you have to hide the
<input type="text" name="other" style="display:none;"/>
and you can show it with javascript when the "Other" box is checked. Something like that.
Just make the name attribute service[]
<li>
<label>What service are you enquiring about?</label>
<input type="checkbox" value="Static guarding" name="service[]">Static guarding<br />
<input type="checkbox" value="Mobile Patrols" name="service[]">Mobile Patrols<br />
<input type="checkbox" value="Alarm response escorting" name="service[]">Alarm response escorting<br />
<input type="checkbox" value="Alarm response/ Keyholding" name="service[]">Alarm response/ Keyholding<br />
<input type="checkbox" value="Other" name="service[]">Other</span>
</li>
Then in your PHP you can access it like so
$service = $_POST['service'];
echo $service[0]; // Output will be the value of the first selected checkbox
echo $service[1]; // Output will be the value of the second selected checkbox
print_r($service); //Output will be an array of values of the selected checkboxes
etc...
<input type="checkbox" value="Other" name="service">Other<input type="hidden" value="Other" name="service"></span>
You've got a hidden input field with the same name as the checkbox. "later" fields with the same name as an earlier one will overwrite the previous field's values. This means that your form, as posted above, will ALWAYS submit service=Other.
Given the phrasing of your question in the html, it sounds more like you'd want a radio button, which allows only ONE of a group of same-name fields to be selected. Checkboxes are an "AND" situation, radio buttons correspond to "OR"

Categories