Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Form POST to PHP page [closed]

Tags:

html

forms

php

ok...i created a form.html page. It asks the user to input into 6 text fields. The form POSTs to a separate page called myform.php. The myform.php page simply just returns the values the user entered. However, when I click submit I just get the myform.php source code popping up on the screen.

<div class="content">
        <form action="myform.php" method="post">
            Name: <input name="name" type="text" size="25" />
            Course: <input name="course" type="text" size="25" />
            Book: <input name="book" type="text" size="255" />
            Price: <input name="price" type="text" size="7" />
            Email: <input name="email" type="text" size="255" />
            Phone #: <input name="phone" type="text" size="12" />

            <input name="mySubmit" type="submit" value="submit" />
        </form>
    </div>

<?php
$name = $_POST["name"];
$course = $_POST["course"];
$book = $_POST["book"];
$price = $_POST["price"];
$email = $_POST["email"];
$phone = $_POST["phone"];

?>
</head>

<body>
<?php
    echo $name;
    ?>
</body>
like image 442
kyros Avatar asked Apr 07 '12 19:04

kyros


People also ask

How do I stay on the same page after submitting form in PHP?

In order to stay on the same page on submit you can leave action empty ( action="" ) into the form tag, or leave it out altogether. For the message, create a variable ( $message = "Success! You entered: ". $input;" ) and then echo the variable at the place in the page where you want the message to appear with <?

How do you display submitted data on the same page as the form in PHP?

$_SERVER['PHP_SELF']: The $_SERVER[“PHP_SELF”] is a super global variable that returns the filename of the currently executing script. It sends the submitted form data to the same page, instead of jumping on a different page.

How do you stay on the same page after submit in HTML?

You could include a hidden iframe on your page and set the target attribute of your form to point to that iframe. There are very few scenarios where I would choose this route. Generally handling it with javascript is better because, with javascript you can...

Can PHP handle forms?

PHP - A Simple HTML FormWhen the user fills out the form above and clicks the submit button, the form data is sent for processing to a PHP file named "welcome.php". The form data is sent with the HTTP POST method.


2 Answers

Marc Towler is correct. You need to have the page with the extension of PHP for PHP code to be parsed in the first place. But I'd actually suggest you separate the pages of your form and your form processing. I'd suggest the following:

myform.php:

<!DOCTYPE html>
<html>
<head></head>
<body>   
<div class="content">
    <form action="formprocessor.php" method="POST">
        <label>Name: </label>
        <input name="name" type="text" size="25" />

        <label>Course: </label>
        <input name="course" type="text" size="25" />

        <label>Book: </label>
        <input name="book" type="text" size="255" />

        <label>Price: </label>
        <input name="price" type="text" size="7" />

        <label>Email: </label>
        <input name="email" type="text" size="255" />

        <label>Phone #: </label>
        <input name="phone" type="text" size="12" />

        <input name="mySubmit" type="submit" value="Submit!" />
    </form>
</div>
</body>
</html>

formprocessor.php:

<?php
$name = $_POST["name"];
$course = $_POST["course"];
$book = $_POST["book"];
$price = $_POST["price"];
$email = $_POST["email"];
$phone = $_POST["phone"];

echo $name;
?>
like image 107
cereallarceny Avatar answered Oct 23 '22 14:10

cereallarceny


Your big issue is in your first sentence

ok...i created a form.html page

To run PHP code on your server you need to rename the file form.php unless you have combined both files in your code example....

like image 24
Marc Towler Avatar answered Oct 23 '22 15:10

Marc Towler