I have a the following form code:
<form method="post" action="nextpage">
<input type="radio" name="p" value="A"/>
<input type="radio" name="p" value="B"/>
<input type="radio" name="p" value="C"/>
<input type="radio" name="p" value="D"/>
</form>
The next page has to return the same form with the button checked.
this code: PHP How to keep radio button state to the next page didnt help.
<form method="post" action="nextpage.php">
<input type="radio" <?php if(isset($_POST['p'])) && $_POST['p'] == 'A') echo 'checked="checked" ';?> name="p" value="A"/>
//and so on for the rest....
You need to use the below code:
<form method="post" action="nextpage">
<input type="radio" name="p" value="A" <?php if(isset($_REQUEST['p']) && $_REQUEST['p'] == "A") echo 'checked="checked"'; ?>/>
<input type="radio" name="p" value="B" <?php if(isset($_REQUEST['p']) && $_REQUEST['p'] == "B") echo 'checked="checked"'; ?>/>
<input type="radio" name="p" value="C" <?php if(isset($_REQUEST['p']) && $_REQUEST['p'] == "C") echo 'checked="checked"'; ?>/>
<input type="radio" name="p" value="D" <?php if(isset($_REQUEST['p']) && $_REQUEST['p'] == "D") echo 'checked="checked"'; ?>/>
</form>
on the next page right above code.
As in your example answer there is radio button with the name q and in your code name is p that is different in both code. Hope this will be your problem.
<input type="radio" <?=(isset($_REQUEST['p']) && $_REQUEST['p'] == 'A') ? 'checked="checked" ' : ''?> name="p" value="A" />
Related
I'm working on a project. I want to pre-populated data from database.
HTML
<div class="question">
<input type="radio" name="q1" value="A" <?php echo $checkedA ?>>
<input type="radio" name="q1" value="B" <?php echo $checkedB ?>>
<input type="radio" name="q1" value="C" <?php echo $checkedC ?>>
</div>
<div class="question">
<input type="radio" name="q2" value="A" <?php echo $checkedA ?>>
<input type="radio" name="q2" value="B" <?php echo $checkedB ?>>
<input type="radio" name="q2" value="C" <?php echo $checkedC ?>>
</div>
<div class="question">
<input type="radio" name="q3" value="A" <?php echo $checkedA ?>>
<input type="radio" name="q3" value="B" <?php echo $checkedB ?>>
<input type="radio" name="q3" value="C" <?php echo $checkedC ?>>
</div>
<!-- etc till let's say 30 question -->
PHP
$query="SELECT * FROM `quiz` WHERE email='$email'";
$result=mysqli_query($link,$query);
$data=mysqli_fetch_assoc($result);
for($i=1; $i<=30; $i++){
switch($data[${"answer".$i}]{
case "A" : $checkedA="checked"; break;
case "B" : $checkedB="checked"; break;
case "C" : $checkedC="checked"; break;
}
}
Then, how to make the q1 which is corresponded with $data['answer1'] checked if it is filled with data from database, etc?
Using the code you've provided, It's viable to get the data from the database and then show the result in your html and not over complicate things. To pre-populate your radio buttons, you need to have the following below:
PHP
$query="SELECT * FROM `quiz` WHERE email='$email'";
$result=mysqli_query($link,$query);
$data=mysqli_fetch_assoc($result);
HTML
<div class="question">
<input type="radio" name="q1" value="A" <?php echo ($data['q1'] == 'A')? 'checked' : ''; ?>>
<input type="radio" name="q1" value="B" <?php echo ($data['q1'] == 'B')? 'checked' : ''; ?>>>
<input type="radio" name="q1" value="C" <?php echo ($data['q1'] == 'C')? 'checked' : ''; ?>>
</div>
<div class="question">
<input type="radio" name="q2" value="A" <?php echo ($data['q2'] == 'A')? 'checked' : ''; ?>>
<input type="radio" name="q2" value="B" <?php echo ($data['q2'] == 'B')? 'checked' : ''; ?>>
<input type="radio" name="q2" value="C" <?php echo ($data['q2'] == 'C')? 'checked' : ''; ?>>
</div>
<div class="question">
<input type="radio" name="q3" value="A" <?php echo ($data['q3'] == 'A')? 'checked' : ''; ?>>
<input type="radio" name="q3" value="B" <?php echo ($data['q3'] == 'B')? 'checked' : ''; ?>>
<input type="radio" name="q3" value="C" <?php echo ($data['q3'] == 'C')? 'checked' : ''; ?>>
</div>
With that piece of code, it sets checked depending on what the value coming from the database is and displays it on the screen.
Example has exactly 3 variables for checked ($checkedA, $checkedB, $checkedC). In order to have 3 varsiables per question, it would need a collection of "checks". If you have n questions, your collection needs n members. And each of those members needs 3 members representing whether the answer is checked, one for each option.
As an example:
If the answer to q1 is A, then
$checked['q1'] = ['A'=> "checked",'B'=>"",'C'=>""].
In the php, initialize a $checked member for each question as described above. Then change the switch to update the corresponding member. [The reason to initialize this way is because 1) everything needs a value in the html and 2) checked is a boolean attribute]
The html would echo $checked[q#]['A'] $checked[q#]['B'] and $checked[q#]['C'] respectively.
I made a form with radio buttons. How can I preserve it's state after a user picked a choice? Then same form will show again in the next page and the radio button that the user picked is enabled.
//page1.html
<form method="post" action="page2.html">
<p>
<input type="radio" name="q1" value="A" />
A. <br />
<input type="radio" name="q1" value="B" />
B. <br />
<input type="radio" name="q1" value="C" />
C. <br />
<input type="radio" name="q1" value="D" />
D.
<p>
<input type="submit" name="action" value="Enter" />
</p>
</form>
To get the value of q1 on the next page, you would use $_POST['q1']. You can verify that the element has been posted, and the value matches the specific radio button by using if(isset($_POST['q1'])) && $_POST['q1'] == VALUE. So your form code would look like -
<input type="radio" name="q1" value="A" <?php if(isset($_POST['q1']) && ($_POST['q1'] == 'A')) echo 'checked="checked" ';?>/>
A. <br />
<input type="radio" name="q1" value="B" <?php if(isset($_POST['q1']) && ($_POST['q1'] == 'B')) echo 'checked="checked" ';?>/>
B. <br />
<input type="radio" name="q1" value="C" <?php if(isset($_POST['q1']) && ($_POST['q1'] == 'C')) echo 'checked="checked" ';?>/>
C. <br />
<input type="radio" name="q1" value="D" <?php if(isset($_POST['q1']) && ($_POST['q1'] == 'D')) echo 'checked="checked" ';?>/>
I'm new to PHP and Javascript, and just starting a website that will have two forms, one form with 5 checkboxes, and one with 5 radiobuttons. When you change the radiobuttons selected radiobutton it refreshes the page and uses the new value. The same with the checkboxes. Unfortunately, if you change one of the forms, and then change another form, the 1st forms value will no longer be there.
Here's the code:
<form name='form2' method='post'>
<input type="radio" name="group1" value="all" onClick="if (this.checked) this.form.submit();" <?php if ($_REQUEST['group1'] == null || $_REQUEST['group1'] == "all"){ echo "checked='checked'";}?>/> All<br>
<input type="radio" name="group1" value="Example" onClick="if (this.checked) this.form.submit();" <?php if ($_REQUEST['group1'] == "Example"){ echo "checked='checked'";}?>/> Example<br>
<input type="radio" name="group1" value="clifton" onClick="if (this.checked) this.form.submit();" <?php if ($_REQUEST['group1'] == "clifton"){ echo "checked='checked'";}?>/> Clifton<br/>
<input type="radio" name="group1" value="fruita" onClick="if (this.checked) this.form.submit();" <?php if ($_REQUEST['group1'] == "fruita"){ echo "checked='checked'";}?>/> Fruita<br/>
<input type="radio" name="group1" value="loma" onClick="if (this.checked) this.form.submit();" <?php if ($_REQUEST['group1'] == "loma"){ echo "checked='checked'";}?>/> Loma<br/>
</form>
<form name='form3' method='post'>
<input type="checkbox" name="option1" value="smoking" onClick="if (this.checked) this.form.submit();" <?php if(isset($_REQUEST['option1'])){ echo "checked='checked'";} ?>/> No Smoking<br>
<input type="checkbox" name="option2" value="kids" onClick="if (this.checked) this.form.submit();" <?php if(isset($_REQUEST['option2'])){ echo "checked='checked'";} ?>/>Good for Kids<br>
<input type="checkbox" name="option3" value="wheelchair" onClick="if (this.checked) this.form.submit();" <?php if(isset($_REQUEST['option3'])){ echo "checked='checked'";} ?>/>Wheelchair Accessible<br>
<input type="checkbox" name="option4" value="alcohol" onClick="if (this.checked) this.form.submit();" <?php if(isset($_REQUEST['option4'])){ echo "checked='checked'";} ?>/>Serves Alcohol<br>
<input type="checkbox" name="option5" value="delivery" onClick="if (this.checked) this.form.submit();" <?php if(isset($_REQUEST['option5'])){ echo "checked='checked'";} ?>/>Delivery Servicee<br>
</form>
I'm trying to get both forms input to be preserved even when the other form changes.
Any help will be GLADLY appreciated. Thanks!
A better solution would be to use AJAX. But you can also add hidden input fields to both forms corresponding to the values in the opposite form:
<form name='form2' method='post'>
<input type="radio" name="group1" value="all" onClick="if (this.checked) this.form.submit();" <?php if ($_REQUEST['group1'] == null || $_REQUEST['group1'] == "all"){ echo "checked='checked'";}?>/> All<br>
<input type="radio" name="group1" value="Example" onClick="if (this.checked) this.form.submit();" <?php if ($_REQUEST['group1'] == "Example"){ echo "checked='checked'";}?>/> Example<br>
<input type="radio" name="group1" value="clifton" onClick="if (this.checked) this.form.submit();" <?php if ($_REQUEST['group1'] == "clifton"){ echo "checked='checked'";}?>/> Clifton<br/>
<input type="radio" name="group1" value="fruita" onClick="if (this.checked) this.form.submit();" <?php if ($_REQUEST['group1'] == "fruita"){ echo "checked='checked'";}?>/> Fruita<br/>
<input type="radio" name="group1" value="loma" onClick="if (this.checked) this.form.submit();" <?php if ($_REQUEST['group1'] == "loma"){ echo "checked='checked'";}?>/> Loma<br/>
<?php foreach( range( 1, 5) as $i): ?>
<?php if(isset($_REQUEST['option' . $i])): ?>
<input type="hidden" name="option<?php echo $i; ?>" value="1" />
<?php endif; ?>
<?php endforeach; ?>
</form>
<form name='form3' method='post'>
<input type="checkbox" name="option1" value="smoking" onClick="if (this.checked) this.form.submit();" <?php if(isset($_REQUEST['option1'])){ echo "checked='checked'";} ?>/> No Smoking<br>
<input type="checkbox" name="option2" value="kids" onClick="if (this.checked) this.form.submit();" <?php if(isset($_REQUEST['option2'])){ echo "checked='checked'";} ?>/>Good for Kids<br>
<input type="checkbox" name="option3" value="wheelchair" onClick="if (this.checked) this.form.submit();" <?php if(isset($_REQUEST['option3'])){ echo "checked='checked'";} ?>/>Wheelchair Accessible<br>
<input type="checkbox" name="option4" value="alcohol" onClick="if (this.checked) this.form.submit();" <?php if(isset($_REQUEST['option4'])){ echo "checked='checked'";} ?>/>Serves Alcohol<br>
<input type="checkbox" name="option5" value="delivery" onClick="if (this.checked) this.form.submit();" <?php if(isset($_REQUEST['option5'])){ echo "checked='checked'";} ?>/>Delivery Servicee<br>
<input type="hidden" name="group1" value="<?php echo (isset($_REQUEST['group1']) ? $_REQUEST['group1'] : 'all'); ?>" />
</form>
Edit: Here is a demo showing it working.
you have to join this inputs in one form element, form elements submit only the information of their input fields and can't send the data of other form element unless you prevent the default form submit action and make your own call via ajax
So, you're missing all the server-side code here, but I can explain the basic problem and hopefully that will allow you to solve it.
You have two forms, form2 and form3. When you submit form2, it doesn't submit form3's data, and vice versa. So, when your server sends back the new page (the one you see post-submit) it doesn't have the data from the other form reflected in it.
What you need to do is send the data from both forms along. The simplest way to do this would be to just use one form, but if you want to get fancy you could combine their values via JS, or even submit them both via AJAX.
Form table is introduced inside an echo and, for the texts fields, I use value=" ' .$_POST['name'] to set default value if form was already sent at least one time. It works properly.
However, how could I save radio buttons status when form was already sent? Thanks.
<tr>
<td colspan="2" align="left" valign="top"> <input type="radio" name="ambiente" value="si" />
Si
<input type="radio" name="ambiente" value="no" />
No</td>
</tr>
Simply:
<input type="radio" name="ambiente" value="si" <?php if ($_POST['ambiente'] == 'si') echo 'checked'; ?> /> Si
<input type="radio" name="ambiente" value="no" <?php if ($_POST['ambiente'] == 'no') echo 'checked'; ?> /> No
<?php
$checked = NULL;
if(isset($_POST['ambiente']) && $_POST['ambiente'] == 'no') {
$checked = 'checked="checked"';
}
?>
<input type="radio" name="ambiente" value="no" <?php echo $checked ?> />
Or this can be shown on one line:
<input type="radio" name="ambiente" value="no" <?php if (isset($_POST['ambiente']) && $_POST['ambiente'] == 'no') echo 'checked="checked"'; ?> />
To allow a user to edit information in a record, this is done:
$case=$_GET['case'];
$query="SELECT * FROM `cases` WHERE `case`= '$case'";
$result=mysql_query($query);
<input type="text" name="firstname" value="<?php echo $firstname; ?>" />
I need to set the value of a radio group based on what its value is in the "cases" table.
<input type="radio" name="flight1_departing" value="AM" />
<input type="radio" name="flight1_departing" value="PM" />
How is this possible?
<input <?php if ($somevalue == 'AM') echo 'checked="checked"'; ?> type="radio" name="flight1_departing" value="AM" />
<input <?php if ($somevalue == 'PM') echo 'checked="checked"'; ?> type="radio" name="flight1_departing" value="PM" />
Given a known value $val, you just need to check it against each radio button value and set the checked attribute, eg
<input type="radio" name="flight1_departing" value="AM"
<?php if ($val == 'AM') : ?>checked="checked"<?php endif ?>
/>
<input type="radio" name="flight1_departing" value="PM"
<?php if ($val == 'PM') : ?>checked="checked"<?php endif ?>
/>
That example is very manual. It would be easier if the radio elements are created in a loop.
Your questions is a little ambiguous but I'm going on the assumption that you mean you need to determine which value is default checked based on the value in the cases table?
Something like,
<input type="radio" name="flight1_departing" value="AM" <?php if ($some_cases_value) { print 'CHECKED'; } ?>/>
<input type="radio" name="flight1_departing" value="PM" <?php if ($some_cases_value) { print 'CHECKED'; } ?> />
Though there is likely a very more elegant way of doing it?