Simple Talking Form
Today i will show you how to use forms and php to create a simple response system, this can be extended and used to make other things like a quiz for your users.
Example
Result:
>.< you haven't made up your mind yet?
How to do this?
First off you'll need to write a standard html document and link to our style sheet to make it look nice.
<link href="http://dev.ameoto.com/themes/dev/style.css" rel="stylesheet" type="text/css">
Next up we need to write a simple form to send the requests. This time were going to use a List form and send off the data using $_GET.
<div class="important"> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get"> What do you like most? <select name="foo" id="select"> <option value="1" selected>I like cookies</option> <option value="2">I like cookies and cream!</option> <option value="3">I like TheTooth</option> </select> <input type="submit" value="Go"/> </form> </div>
Now all we need is something to handle the request so today were going to be using php switch.
If you want to select one of many blocks of code to be executed, use the Switch statement. The switch statement is used to avoid long blocks of if..elseif..else code.
Below is the main code to handle a request. As you can see all were doing with this one is echoing different text for different choices submitted by the form and *if none selected it will ask you to.
<?php $x = $_GET[foo]; switch ($x) { case 1: echo "Yeah cookies are really nice."; break; case 2: echo "YAY! cookies with cream thats the best!!!"; break; case 3: echo "^_^ your my friend."; break; default: echo ">.< you haven't made up your mind yet?"; } ?>
And now were done! just check your syntax and enjoy!
