DJ Mike's Tutorials: PHP

Forms And PHP Pt.2


< ^ >

On the last page I showed you how to make an interface for someone else's script. Now it is time for you to make your own tool. For this example, we will make a form to convert kilometers to miles.

The form can be basic HTML, a text input, a submit button a text input for the results and a table to hold it all together.

<form><table><tr> <td>Kilometers: <input type="text" name="km" cols="8" /> </td> <td><input type="submit" value="Convert to miles" /> </td> <td>Miles:<input type="text" name="miles" value="" cols="8" /> </td></tr></table></form> </plaintext></code> <p> Notice that the first text input is named "km" so it will become $km when submitted to a PHP script. You don't want your script executing if nothing has been submited so you can test with <a href="operators.php#variable_content">isset()</a> to see if $km has been set. A quick check with Google and you find that one mile is 1.609344006 km's and a little arithmetic gives you the PHP to convert miles to kilometers: </p> <code><plaintext> <? $mi = $km/1.609344006; ?> </plaintext></code> <p>To generate an ouput, simply echo it in the "miles" text box</p> <code><plaintext> <input type="text" name="miles" value="<? echo $mi; ?>" cols="8" /> </plaintext></code> <br /><br /> <center> <a href="examples/km2miles_01.php" target="demo1">Show example</a> | <a href="examples/km2miles_01.phps" target="demo1">Show code</a> </center> <iframe name="demo1" src="examples/blank.html" width="100%" border="0"> </iframe> <center> <a href="forms_01.php"><</a> <a href="index.html">^</a> <a href="forms_03.php">></a> </center> <br /><br /> <center> <script type="text/javascript" language="javascript"> /*<![CDATA[*/ document.write(' <img alt="counter" src=http://eclecticdjs.com/logs/wtv_bug.php?referer='+document.referrer+' />'); /*]]>*/ </script> <noscript> <img src="http://eclecticdjs.com/logs/wtv_bug.php" alt="counter" /> </noscript> </center> <div align="center" style="text-align: center"> <center> Created by <a href="http://eclecticdjs.com/mike/">DJ Mike from Santa Barbara</a> <br /><br /> <table bgcolor="black" border="0" summary="footer"> <tr> <td align=center valign=middle> <img src="http://eclecticdjs.com/mike/images/partyball.gif" width="77" height="88" alt="" /> </td><td> <a href="http://eclecticdjs.com/mike/"> <img src="http://eclecticdjs.com/mike/images/rainbow_mike.gif" width="343" height="100" border="0" alt="DJ Mike" /> </a> </td><td> <img src="http://eclecticdjs.com/mike/images/mikeshead4.jpg" width="108" height="100" border="0" alt="" /> </td></tr> </table> </center> </div> <br /><br /> <div align="center"> <a href="http://danceawaysb.com"> <img src="http://danceawaysb.com/images/dabanner.gif" width="468" height="120" alt="Dance Away Santa Barbara's Home Page" /></a> </div> <!-- WebTV users --> <!-- Start of StatCounter Code --> <script type="text/javascript" language="javascript"> /*<![CDATA[*/ var sc_invisible=1; var sc_project=1321035; var sc_partition=9; var sc_security="da2193dc"; /*]]>*/ </script> <script type="text/javascript" language="javascript" src="http://www.statcounter.com/counter/counter.js"> </script> <noframes> <a href="http://www.statcounter.com/" target="_blank"> <img src="http://c5.statcounter.com/counter.php?sc_project=1321035&java=0&security=da2193dc" alt="counter free hit invisible" border="0" /></a> </noframes> <!-- End of StatCounter Code --> <br /> </body> </html>