Retaining Radio Button Value after submitting button using GET method - php

I am using method= "GET", I have 2 radio buttons.I want to retain the selected radio button value even After clicking my submit button.
This is something I have tried, but yet does not work. Can any1 plz help me . Thank u in advance.
VEG<input type="radio" name="choice" <?php if (isset($_GET['choice']) && $_GET['choice']=="veg") echo 'selected="seleceted"';?>value="veg" >
Nonveg<input type="radio" name="choice" <?php if (isset($_GET['choice']) && $_GET['choice']=="nonveg") echo 'selected="seleceted"';?>value="nonveg" ></td>
Plz help me solve this.

Right Practice
<?php
$vegChecked = NULL;
$nonvegChecked = 'checked="checked"';
if(isset($_GET['choice'])) {
if($_GET['choice'] == 'veg') {
$vegChecked = 'checked="checked"';
$nonvegChecked = NULL;
} else if($_GET['choice'] == 'nonveg') {
$vegChecked = NULL;
}
}
?>
VEG <input type="radio" name="choice" <?php echo $vegChecked;?> value="veg" />
Nonveg <input type="radio" name="choice" <?php echo $nonvegChecked;?> value="nonveg" />

Instead of selected="selected" try using checked.
For example :-
<input type="radio" name="choice" <?php if (isset($_GET['choice']) && $_GET['choice']=="nonveg") echo 'checked'; ?>value="nonveg" >

Try this,
VEG<input type="radio" name="choice" <?php if (isset($_GET['choice']) && $_GET['choice']=="veg") echo 'checked="checked"';?> value="veg" >
Nonveg<input type="radio" name="choice" <?php if (isset($_GET['choice']) && $_GET['choice']=="nonveg") echo 'checked="checked"';?> value="nonveg" >

Use Ajax instead of form submission to avoid refreshing of the page Or Set the value of radio button in session while executing form submission PHP. like session_start();
$_SESSION['choice'] = $_GET['choice'];
then check the value using if condition to set selected button.

Related

Input checkbox when i edit user no change

When i want to edit my user on inactive is it no working, but when i editing my users to inactive to active, it is working.
This is my PHP code: https://pastebin.com/iBaDxH2u
<input class="form-control" type="checkbox" name="actif" id="actif" value="<?php echo $userinfo['actif']; ?>" <?php if ($userinfo['actif'] == "1") { echo "checked"; } ?>>
<input type="hidden" name="actif" value="1" />
I dont solve the problem...
Thx
This sounds like it has something to so with how checkboxes work. When you submit the form, if the checkbox is ticked then the page you submit the form to will receive [actif] => on. If you submit with the checkbox unticked then the page will not receive [actif] => off, it will receive an empty array []. actif will not be set. Something like this might make it more obvious what is going on.
<?php
if (isset($_GET['actif']) && $_GET['actif']=="on")
{
echo ("The box was ticked");
$ticked = 'checked';
}
else
{
echo ("The box was not ticked");
$ticked = '';
}
echo ("<pre>");
print_r($_GET);
?>
<form>
Actif <input type='checkbox' name='actif' id='actif' <?=$ticked?>>
<button type='submit'>Click me</button>
</form>

mpdf showing a radio button and checkbox selected

I have a quick question.
I creating an pdf output when the user clicks on the button. However the problem I'm having is the radio and checkbox. I tried to use the standard
($answer == 'What the value is' ) ? 'checked':''
however when it is outputted to a pdf, it doesn't show the radio button or checkbox selected. Is there way I can have the radio buttons or checkbox's selected in an pdf output?
Thank you
Ok, I have found a solution. What I did was I put the the checkbox and radio button in if statement and I did put the code in the following format:
<input type="radio" checked="checked"> Yes
That did the trick..
try this:
if ($answer = "1"){
$output .= 'Yes <input type="checkbox" checked /> ';
}
else{
$output .= 'Yes <input type="checkbox" /> ';
}
Where is 1 true. I assume that, you get these values from db. In this cycle check these values if it checked.
<input type="radio" value="1" <?php $data->input_11 == 1 ? 'checked="checked"' : null ?> > Yes
<input type="checkbox" value="1" <?php $data->input_11 == 1 ? 'checked="checked"' : null ?> > Yes
Its Work for me

Storing checkbox values in sessions and then re-selecting check boxes checked when they came back to add some more

I am trying to pass my checkbox values through a session variable if the user goes back by clicking add more books link. I want to show them previously selected check boxes are checked. I tried in chklist.php page
if(isset($_SESSION['pro'])){
echo $_SESSION['pro'];}
shows values like 1_2_5 which are present in session array.
Here is my html code of checkboxes. I have about 24 checkboxes with the same name as product[].
<name="product[]" type="checkbox" value="1" alt="1607.00" />
<name="product[]" type="checkbox" value="2" alt="1607.00" />
and so on
Here I am setting my session after POST of checkboxes .
$_SESSION['pro'] = implode('_', $_POST['product']);
in nextpage chkout.php.
How to make previously selected checkboxes are checked when user came back to firstpage(chklist.php) by clicking ADD MORE BOOKS link present in chkout.php, can any body write the code what i need add to my html.
You can put products array into session then access it as an array.
When setting:
$_SESSION["products"] = $_POST["product"];
When listing products, you should check the values from session:
for($i = 0; $i < 24; $i++) {
echo '<name="product[]" type="checkbox" value="'.$i.'" alt="1607.00"';
if(in_array($i, $_SESSION["products"])) echo ' checked="checked" ';
echo ' />';
}
This is the basic idea and example code.
--UPDATE--
According to your comments:
Inside the form, we will print products as follow:
<?php
session_start();
$session_products = array();
if(array_key_exists("products", $_SESSION))
{
if($_SESSION["products"] != null)
{
$session_products = $_SESSION["products"];
}
}
<form method="post" action="newtest.php">
<input name="product[]" type="checkbox" value="1" <?php if(in_array("1", $session_products)) echo "checked='checked'"; ?> alt="1607.00" />
<input name="product[]" type="checkbox" value="2" <?php if(in_array("2", $session_products)) echo "checked='checked'"; ?> alt="1848.00" />
<input name="product[]" type="checkbox" value="3" <?php if(in_array("3", $session_products])) echo "checked='checked'"; ?> alt="180.00" />
<input name="product[]" type="checkbox" value="4" <?php if(in_array("4", $session_products)) echo "checked='checked'"; ?> alt="650.00" />
and so on upto 24 ...
</form>
Inside the code where the form values are posted, we will put these values into session:
<?php
include("config.php");
session_start();
if(isset($_POST))
{
$_SESSION["products"] = $_POST["product"];
}
?>

PHP check boxes and URL rewriting

Hope someone can help me with my code. i am using rewritten URL's and have this piece of code on a form. When the page first loads mypage.htm, the type_24 checkbox get's checked by default. If they check the type_12 box, i want the type_24 box to uncheck.
My problem is, if i check the type_12 box, the page refreshes and both the type_12 and type_24 boxes are checked.. which is not what i want. I think it's because i'm reloading my rewritten URL in my action because it works fine if i just have php file as the action.
Any ideas how i can fix my code so that it only has the type_12 checked when i check the type_24 box?
<form name="frmrefresh" id="frmrefresh" method="post" action="mypage.htm">
<input type="checkbox" name="type_12" id="type_12" <?php if(isset($_POST['type_12']) && $_POST['type_12']=="12"){?> checked="checked"<?php }?> value="12" onClick="uncheck24(this);" /> <label>12</label>
<input type="checkbox" name="type_24" id="type_24" <?php if(isset($_GET['id']) && $_GET['id']!=''){?>checked="checked"<?php }?><?php if(isset($_POST['month_24']) && $_POST['type_24']=="24"){?> checked="checked"<?php }?> value="24" onClick="uncheck12(this);"/> <label>24 Months</label>
<input type="hidden" name="id" value="<?php echo $_REQUEST['id'];?>" />
</form>
In the header i have the functions:
function uncheck12(obj)
{
if (obj.checked == true)
{
document.getElementById("type_12").checked = false;
document.frmrefresh.submit();
}
}
function uncheck24(obj)
{
if (obj.checked == true)
{
document.getElementById("type_24").checked = false;
document.frmrefresh.submit();
}
}
A number of remarks:
Consider using radio buttons instead of checkboxes
I think you mean isset($_POST['type_24']) instead of isset($_POST['month_24'])
As to your problem, I bet your rewrite rule is something like:
RewriteRule ^mypage.htm$ mypage.php?id=5
This means that when the form gets submitted via POST, PHP will still set the $_GET['id'] variable for you since it's in the query string. And since you check the '24' option whenever $_GET['id'] is set, the second checkbox will always get checked.
To fix this you could consider adding the check $_SERVER['REQUEST_METHOD'] == 'GET':
<form name="frmrefresh" id="frmrefresh" method="post" action="mypage.htm">
<input type="checkbox" name="type_12" id="type_12" <?php if(isset($_POST['type_12']) && $_POST['type_12']=="12"){?> checked="checked"<?php }?> value="12" onClick="uncheck24(this);" /> <label>12</label>
<input type="checkbox" name="type_24" id="type_24" <?php if(isset($_GET['id']) && $_GET['id']!='' && $_SERVER['REQUEST_METHOD']=='GET'){?>checked="checked"<?php }?><?php if(isset($_POST['type_24']) && $_POST['type_24']=="24"){?> checked="checked"<?php }?> value="24" onClick="uncheck12(this);"/> <label>24 Months</label>
<input type="hidden" name="id" value="<?php echo $_REQUEST['id'];?>" />
</form>

How to remember checkbox input in PHP Forms

For usability purposes I like to set up my form fields this way:
<?php
$username = $_POST['username'];
$message = $_POST['message'];
?>
<input type="text" name="username" value="<?php echo $username; ?>" />
<textarea name="message"><?php echo $message; ?></textarea>
This way if the user fails validation, the form input he entered previously will still be there and there would be no need to start from scratch.
My problem is I can't seem to keep check boxes selected with the option that the user had chosen before (when the page refreshes after validation fails). How to do this?
My first suggestion would be to use some client-side validation first. Maybe an AJAX call that performs the validation checks before continuing.
If that is not an option, then try this:
<input type="checkbox" name="subscribe" <?php echo (isset($_POST['subscribe'])?'checked="checked"':'') ?> />
So if subscribe is = 1, then it should select the box for you.
For Example, consider the following code for checkbox :-
<label for="course">Course:</label>
PHP<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("PHP", $_POST["course"]))) {
echo "checked";
} ?> value="PHP" />
Then, this would remember the checkbox of "PHP" if it is checked, even if the validation for the page fails and so on for "n" number of checkboxes as shown below:-
<label for="course">Course:</label>
PHP<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("PHP", $_POST["course"]))) {
echo "checked";
} ?> value="PHP" />
HTML<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("HTML", $_POST["course"]))) {
echo "checked";
} ?> value="HTML" />
CSS<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("CSS", $_POST["course"]))) {
echo "checked";
} ?> value="CSS" />
Javascript<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("Javascript", $_POST["course"]))) {
echo "checked";
} ?> value="Javascript" />
And most importantly, do not forget to declare the "course" variable as an array at the start of the code as shown below :-
$course = array();
I have been battling how to create sticky check box (that is able to remember checked items any time you visit the page). Originally, I get my values from a database table. This means that my check box value is entered to a column on my db table.
I created the following code and it works just fine. I did not want to go through that whole css and deep coding, so...
CODE IN PHP
$arrival = ""; //focus here.. down
if($row['new_arrival']==1) /*new_arrival is the name of a column on my table that keeps the value of check box*/
{$arrival="checked";}// $arrival is a variable
else
{$arrival="";};
echo $arrival;
<b><label for ="checkbox">New Arrival</label></b>
<input type="checkbox" name ="$new_arrival" value="on" '.$arrival.' /> (Tick box if product is new) <BR><BR>
<input type="checkbox" name="somevar" value="1" <?php echo $somevar ? 'checked="checked"' : ''; ?>/>
Also, please consider sanitising your inputs, so instead of:
$somevar = $_POST['somevar'];
...it is better to use:
$somevar = htmlspecialchars($_POST['somevar']);
When the browser submits a form with a checked checkbox, it sends a variable with the name from the name attribute and a value from the value attribute. If the checkbox is not checked, the browser submits nothing for the checkbox. On the server side, you can handle this situation with array_key_exists(). For example:
<?php
$checkedText = array_key_exists('myCheckbox', $_POST) ? ' checked="checked"' : '';
?>
<input type="checkbox" name="myCheckbox" value="1"<?php echo $checkedText; ?> />
Using array_key_exist() avoids a potential array index undefined warning that would be issued if one tried to access $_POST['myCheckbox'] and it didn't exist.
You may add this to your form:
<input type="checkbox" name="mycheckbox" <?php echo isset($_POST['mycheckbox']) ? "checked='checked'" : "" ?> />
isset checks if a variable is set and is not null. So in this code, checked will be added to your checkbox only if the corresponding $_POST variable has a value..
My array has name="radioselection" and value="1", value="2", and value="3" respectively and is a radio button array... how to I check if the radio value is selected using this code
I tried:
<?php echo (isset($_POST['radioselection']) == '1'?'checked="checked"':'') ?> />

Categories