简体   繁体   中英

Form not using the file in action attribute

I have a very simple HTML form that is supposed to send information to the file written in action attribute via GET but somehow it's transfering the information back to 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>

The bizarre aspect is that when I submit the form, the URL shows that it is not even using process_form.php:

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

If I manually change the URL to include process_form.php it seems to be working fine and I get the results I am looking for:

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

On my development computer, I'm running EasyPHP 14.1 local WAMP server and thought it might be the root of the problem so I uploaded the files to my website that is running newest PHP on Apache, but the problem still exists there.

What am I doing wrong?

you have a typo error in action ; you have given acton . Should be like this:

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

First thing - you have a typo:

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

The second thing - in my opinion the best method of handling forms is using POST method, not GET , so I would change it to:

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

and in process_form.php I would use $_POST instead of $_GET

After digging around your question,

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>

Note: If you will not specify form method, By default it will take GET method. So please make sure action should be perfect.

Above code just copy and paste, it should work perfect.

Ask me for further clarification.

Thanks, Gauttam

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM