PHP Cannot POST error running XAMPP

一个人想着一个人 提交于 2019-12-07 03:30:32

Here is a technique that I used in some code today that answered my question.

<!DOCTYPE HTML>
<html>
    <head>
        <title> Temperature Conversion </title>
    </head>
    <body>

        <form method="post" action= "<?php echo $_SERVER["PHP_SELF"]; ?>">
            Fahrenheit: <input type="radio" name="temp_metric" value="Fahrenheit" />
            Celcius: <input type="radio" name="temp_metric" value="Celcius" />
            <input type="submit" name="submit" value="submit" />
        </form>

    </body>
</html>


<?php

//VARIABLE DECLARATIONS
$degrees = NULL;
$temp_degrees = NULL;
$curr_metric = NULL;
$converted_metric = NULL;
$converted_temp = NULL;

//LOGIC
if ('POST' === $_SERVER['REQUEST_METHOD'] and isset($_POST['temp_metric'])) {
    $curr_metric = $_POST['temp_metric'];
    for ($degrees = 0; $degrees <= 100; $degrees++) {
        if($curr_metric == "Fahrenheit") {
            $converted_metric = "Celcius";
            $temp_degrees = ($degrees - 32) * (5/9);
            $converted_temp = round( $temp_degrees, 1);
        } 

        else {
            $curr_metric = "Celcius";
            $converted_metric = "Fahrenheit";
            $temp_degrees = $degrees * 9/5 + 32;
            $converted_temp = round($temp_degrees , 1);
        }

        //output the result of the users selection
        echo "<div> $degrees $curr_metric is $converted_temp     $converted_metric. </div>";

    }//end of for loop
}//end of if current metric is not null


?>

The pieces that I was missing were located in my initial HTML for action to ""> enabled me to post the form data back to the PHP file combined with the IF ('POST' === $_SERVER['REQUEST_METHOD']) allowed me to exclude the PHP until I clicked on the submit button.

This may not be the proper way to do this but since I am using XAMPP it works. However I do know that if one is using the traditional web server using seperate PHP and HTML files works well. As of yet I have not completely set up the development environment at my hosting company. For the time being this will have to do.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!