PHP form that shows extra content based on user's response
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开发者_如何学JAVA>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>
<a href="http://www.mydomain.com/somefile.pdf">grab the file here</a>
</div>
<?php
if (isset($_POST['group1']) && isset($_POST['group2'])) {
if ($_POST['group1']=='Yes' && $_POST['group2']=='Water') print '<div><a href="http://www.mydomain.com/somefile.pdf">grab the file here</a></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><a href="http://www.mydomain.com/somefile.pdf">grab the file here</a></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/
精彩评论