HTML 表单 POST 到 PHP 页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10057752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTML Form POST to PHP page
提问by kyros
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.
好的...我创建了一个 form.html 页面。它要求用户输入 6 个文本字段。表单 POST 到一个名为 myform.php 的单独页面。myform.php 页面只返回用户输入的值。但是,当我单击提交时,我只会在屏幕上弹出 myform.php 源代码。
<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>
回答by cereallarceny
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:
马克·托勒是对的。您首先需要拥有带有 PHP 扩展名的页面,以便解析 PHP 代码。但我实际上建议您将表单页面和表单处理页面分开。我建议如下:
myform.php:
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:
<?php
$name = $_POST["name"];
$course = $_POST["course"];
$book = $_POST["book"];
$price = $_POST["price"];
$email = $_POST["email"];
$phone = $_POST["phone"];
echo $name;
?>
回答by Marc Towler
Your big issue is in your first sentence
你的大问题在你的第一句话
ok...i created a form.html page
好的...我创建了一个 form.html 页面
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....
要在您的服务器上运行 PHP 代码,您需要重命名文件 form.php,除非您在代码示例中合并了这两个文件......

