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
https://forums.phpfreaks.com/topic/273757-basic-php-form-that-posts-to-itself/
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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.