I am writing a form, and I may be overcomplicating this, but I want the form to change based on the response to a question. It is a contact form. I want to ask a question, then use a php if else statement to generate the response do if a show this haml, elseif b show this. In the end the form will be emailed to me. Here is my question. If i set the variables in the head section like this:
<?php
$letter = $_POST['letter'];
?>
</html>
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<select name="letter'>
<option value="a">a</option>
<option value="b">b</option>
</form>
<?php
if ($letter == a) {html}
elseif ($letter == b) {html}
?>
Would that work, or does the form need to be submited before the variable can be assigned? If it needs to be submited, can that be done as soon as the selection is made in the select tag and how would I do that without the user clicking a submit button?
yes using javascript(jQuery) you can submit form without refreshing page.!
follow these steps and create files
Example.php
<html>
<head>
<!--include jquery and js code -->
<script type="javascript/text" src="jquery.js"></script>
<script type="javascript/text" src="myjs.js"></script>
</head>
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<select name="letter' id="letter">
<option value="a">a</option>
<option value="b">b</option>
</select>
</form>
<div id="response"></div>
</body>
</html>
myjs.js
$(document).ready(function(){
$("#letter").change(function(){
$.post("process.php", { letter: $(this).val() },function(r){
$("#response").html(r);
});
});
});
Now final process.php
now in process.php you can do process like that
<?php
$letter = $_POST['letter'];
if ($letter == a) {html}
elseif ($letter == b) {html}
?>