Jump to content

basic php form that posts to itself


benjahnee

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.