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.
Related
I have a code here...the thing that i want is to calculate the total instantly and show in the textbox without clicking the action button.
code:
<?php
include('include/connect.php');
if(isset($_POST['enter']))
{
$name = $_POST['name'];
$score1 = $_POST['optA'];
$score2 = $_POST['optB'];
$score3 = $_POST['optC'];
$score4 = $_POST['optD'];
$total1 = $_POST['total'];
$total = ($score1 + $score2 + $score3 + $score4);
mysql_query("INSERT INTO score (name,score1,score2,score3,score4,total) VALUE ('$name','$score1','$score2','$score3','$score4','$total')");
echo "succesful";
}
else
{
echo "you fail to execute!";
}
?>
code:
<form method="post" action="index.php">
<input type="text" name="name" />
<input type="radio" name="optA" value="1" />1
<input type="radio" name="optA" value="2" />2
<input type="radio" name="optA" value="3" />3<br>
<input type="radio" name="optB" value="1" />1
<input type="radio" name="optB" value="2" />2
<input type="radio" name="optB" value="3" />3<br>
<input type="radio" name="optC" value="1" />1
<input type="radio" name="optC" value="2" />2
<input type="radio" name="optC" value="3" />3<br>
<input type="radio" name="optD" value="1" />1
<input type="radio" name="optD" value="2" />2
<input type="radio" name="optD" value="3" />3
<input type="text" name="total" value="<?php echo $total ?>" />
<input type="submit" value="enter" name="enter" />
</form>
PHP is executed before the content reaches the browser, so no elements will be loaded, so you cannot access them. It is a server-side language, not client-side; therefore you cannot do this. An example of something client-side is Javascript. However, you can create a HTML element and set its attributes, then echo it onto the page.
You'll have to use JavaScript in some form or another to not have to post the answer back to the server and wait for a response.
Here's how you can do it in JQuery.
function calculateTotal() {
var total = 0;
$('form input:radio:checked').each(function () {
total = total + parseInt($(this).val());
});
$('form [name=total]').val(total);
return false;
}
In your HTML you'll need to change your submit button to something else like this:
<span onclick="calculateTotal()">Enter</span>
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" />
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" ';?>/>
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"'; ?> />
I'm looking for a way to have a simple form (yes and no radio button questions) where if the user answers yes to all of the questions and hits submit, a hidden link to a file is made visible. I'm no good at creating my own PHP yet...any suggestions?
<form name="myform" action="http://www.mydomain.com/myformhandler.php" method="POST">
<div align="center"><br>
<p>Question nubmer 1...</p><input type="radio" name="group1" value="Yes"> Yes<br>
<input type="radio" name="group1" value="No" checked> No<br>
<hr>
<p>Question nubmer 2...</p><input type="radio" name="group2" value="Yes"> Yes<br>
<input type="radio" name="group2" value="No"> No<br>
</div>
</form>
This would be visible if the answer to both questions is yes...
<div>
grab the file here
</div>
<?php
if (isset($_POST['group1']) && isset($_POST['group2'])) {
if ($_POST['group1']=='Yes' && $_POST['group2']=='Water') print '<div>grab the file here</div>';
}
?>
You basically just need to validate the values of the form that is being submitted.
$group1 = $_POST['group1'];
$group2 = $_POST['group2'];
if ($group1 == 'Yes' AND $group2 == 'Yes') echo 'My hidden data'[
You're interested in the GET/POST global arrays, in this case POST:
<form name="myform" action="" method="POST">
<div align="center"><br>
<p>Question nubmer 1...</p>
<input type="radio" name="group1" value="Yes"> Yes<br>
<input type="radio" name="group1" value="No" checked> No<br>
<hr>
<p>Question nubmer 2...</p>
<input type="radio" name="group2" value="Water"> Yes<br>
<input type="radio" name="group2" value="Beer"> No<br>
<input type="submit"/>
</div>
</form>
<?php
if ($_POST['group1'] == 'Yes' && $_POST['group2'] == 'Water') {
echo '<div>grab the file here</div>';
}
?>
Try it: http://jfcoder.com/test/grabfile.php
$group1 = $_POST('group1);
$group2 = $_POST('group2);
Then just use '==' (Equal or Set as) to check if they are the same:
if($group1 == 'Yes' && $group2 == 'Yes') print grab the file here';
The code above will print the file. The reason is that the values of the operands are equal.
I'm way late to this party but I thought the OP would like to know you could also do this in JavaScript without posting back to the page:
$('form[name="myform"]').submit(function(event){
event.preventDefault();
if($('input[name="group1"]').val() == "Yes" && $('input[name="group2"]').val() == "Water"){
$('#linkToPDF').show();
}
});
You can see it working here:
http://jsfiddle.net/ajp4r/