benjahnee Posted January 28, 2013 Share Posted January 28, 2013 hello guys i am new to the forum and new to php i am attempting to write a code that converts miles to km i think i have the code for the calculation correct. could somebody please tell me what else it is i must add to this code so that the forum posts to itself, it must also have a submit button that i have not added yet so please advise me on this too, the code i have so far is - <?php function miles2kms($miles) { $ratio = 1.609344; $kms = $miles * $ratio; return $kms; } ?> thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/273757-basic-php-form-that-posts-to-itself/ Share on other sites More sharing options...
.josh Posted January 28, 2013 Share Posted January 28, 2013 You need to have a condition to check if the form was submitted. You can check with $_POST['textFieldNameHere']. If it exists, call your function, passing the value of the posted variable, and echo the results. Then after that (outside of the condition), you need to echo out the html code for the form with the input field (and a name attribute matching what your condition looks for), and submit button. The form method should be POST and the action should be left blank, or else the URL of the script. Quote Link to comment https://forums.phpfreaks.com/topic/273757-basic-php-form-that-posts-to-itself/#findComment-1408833 Share on other sites More sharing options...
BryanG Posted January 29, 2013 Share Posted January 29, 2013 HTML FORM <form method="post" action="THIS_FILE"> Miles: <input type="text" name="miles" /><br> <input type="submit" name="submit" value="Convert!" /> </form> PHP CODE <?php if(isset($_POST['submit'])){ //Form was submitted $result = miles2kms($_POST['miles']; } ?> Now just print out $result wherever you want it Quote Link to comment https://forums.phpfreaks.com/topic/273757-basic-php-form-that-posts-to-itself/#findComment-1408943 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.