表单不使用action in action属性

时间:2014-07-19 07:24:56

标签: php forms easyphp

我有一个非常简单的HTML表单,它应该通过GET将信息发送到以action属性编写的文件,但不知怎的,它将信息传回index.php:

的index.php

<!doctype html>
<html>
<head>
    <title>Sandbox</title>
    <meta charset="utf-8" />
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

<h1>PHP Forms Sandbox</h1>

<form acton="process_form.php" method="get">
    <label for="username">Username:</label>
    <input type="text" name="username" id="username" value="" />
    <label for="email">E-mail:</label>
    <input type="text" name="email" id="email" value="" />
    <input type="submit" name="submit_btn" id="submit_btn" value="Submit" />
</form>

</body>
</html>

process_form.php

<!doctype html>
<html>
<head>
    <title>Sandbox</title>
    <meta charset="utf-8" />
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

<h1>PHP Response Sandbox</h1>

<?php

$username = $_GET["username"];
$email = $_GET["email"];

echo $username . " : " . $email . "<br />";

?>

</body>
</html>

奇怪的是,当我提交表单时,URL显示它甚至没有使用process_form.php:

http://127.0.0.1/Learning/?username=test&email=x%40test.com&submit_btn=Submit

如果我手动更改URL以包含process_form.php,它似乎工作正常,我得到了我正在寻找的结果:

http://127.0.0.1/Learning/process_form.php?username=test&email=x%40test.com&submit_btn=Submit

在我的开发计算机上,我正在运行EasyPHP 14.1本地WAMP服务器并认为它可能是问题的根源所以我将文件上传到我的网站上运行最新PHP的Apache,但问题仍然存在那里。

我做错了什么?

3 个答案:

答案 0 :(得分:2)

action中有拼写错误;你给了 acton 。应该是这样的:

<form action="process_form.php" method="get">

答案 1 :(得分:1)

首先 - 你有一个错字:

<form action="process_form.php" method="get">
         ^

第二件事 - 在我看来,处理表单的最佳方法是使用POST方法,而不是GET,所以我会将其更改为:

<form action="process_form.php" method="post">

并在process_form.php中,我会使用$_POST代替$_GET

答案 2 :(得分:0)

在挖掘你的问题后,

的index.php

<!doctype html>
<html>
<head>
<title>Sandbox</title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>PHP Forms Sandbox</h1>
<form action="process_form.php" method="get">
<label for="username">Username:</label>
<input type="text" name="username" id="username" value="" />
<label for="email">E-mail:</label>
<input type="text" name="email" id="email" value="" />
<input type="submit" name="submit_btn" id="submit_btn" value="Submit" />
</form>
</body>
</html>

process_form.php

<!doctype html>
<html>
<head>
<title>Sandbox</title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body> 
<h1>PHP Response Sandbox</h1>
<?php
$username = $_GET["username"];
$email = $_GET["email"];
echo $username . " : " . $email . "<br />";
?>
</body>
</html>

注意:如果您不指定表单方法,默认情况下它将采用GET方法。所以请确保行动应该是完美的。

上面的代码只是复制和粘贴,它应该工作得很完美。

请我进一步澄清。

谢谢, Gauttam