1

I am trying to make a form on my website where users can submit their name and their score will get saved to a list of highscores. (its a quiz game.)

I tried learning to use forms using w3schools. I used this example: https://www.w3schools.com/php/php_forms.asp

<html>
<body>

<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

</body>
</html>

with welcome.php looking like this:

<html>
<body>

Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html> 

I literally copy and pasted this example and tried running it and i get this error message:

WelcomeWarning: Undefined array key "name" in [filelocation] on line 4

Your email address is:Warning: Undefined array key "email" in [filelocation] on line 5

However when I replaced all the "post" with "get" it worked. Why? What do I need to do to get it to work with post?

EDIT: Also I left the "post" in the html but i replaced the POST in the welcome.php with REQUEST. It now works, however I think its somehow using GET instead of POSTs because i can see the input in the URL. I definitely need to avoid this. Maybe this helps

Thank you!

cactus
  • 69
  • 1
  • 1
  • 8
  • Warning! You are open to XSS attacks. Use proper escaping and `` in the form fields will not do any harm. – no ai please Oct 09 '21 at 05:35
  • `$_REQUEST` combines `$_GET` and `$_POST` but might get unpredictable if the sames keys appear in both. If you did not edit your question than `$_POST` would have been populated. To test this put a ` – theking2 Feb 19 '22 at 09:48

5 Answers5

1

The W3Schools page you linked has two examples using both POST and GET on the same page.

Considering you said you didn't edit the code, i think it's very likely you copied the HTML code of the second example that uses GET whiile using the PHP code from the first example that uses POST. The code block is right below is.

Frankly, this kind of thing happens to everyone sometimes, make sure you're properly hydrated and carry on!

x13
  • 2,179
  • 1
  • 11
  • 27
  • 1
    Hi. Thank you for your concern. I just double checked and I used the correct code snippets. When I use the GET snippets it works but when I copy the POST code it does not. Have you tried running it? – cactus Jan 02 '21 at 16:38
  • @cactus i just tried it on the W3Schools link you included, works fine on there. I can't tell you much except that there's nothing wrong with the code you posted. It would give you the warnings if you open that PHP page **without** submitting the form first, but it sounds like that's not the case.. – x13 Jan 03 '21 at 01:04
  • When I try it on W3Schools it works to but when i try it on my php server it does not. I figured out in the meantime, that the welcome.php is receiving a GET method, even though i specified post in the html. Maybe something is wrong with my server. – cactus Jan 04 '21 at 10:25
  • 1
    I finally figured it out. Once I realized I was gettin a GET instead of a POST I googled more specifically and found this solution: https://stackoverflow.com/questions/37908024/form-sends-get-instead-of-post Thanks for trying to help! – cactus Jan 04 '21 at 10:32
1

you might running directly welcome.php page.

Replace your welcome.php with

<html>
<body>
    <?php if ($_SERVER["REQUEST_METHOD"] == "POST") { ?>
    Welcome <?php echo $_POST["name"]; ?><br>
    Your email address is: <?php echo $_POST["email"]; ?>
    <?php }
          else{ ?>
     <script> location.replace("yourHtmlFileName.html") </script>
     <?php     } ?>
</body>
</html> 
  • Hi. I tried doing this and it simply reloads my HTML website when i click submit – cactus Jan 02 '21 at 17:32
  • `Form Method: = $_SERVER["REQUEST_METHOD"]?> ` add this in welcome.php page, after body start to determine your form method is post or get. If you writing post and there you see get, it might problem with your browser. – Dnyaneshwar Taur Jan 03 '21 at 07:52
  • I tried it and it says get! which is so weird! It happened in both my browsers though. – cactus Jan 04 '21 at 10:24
  • Thank you so much! Knowing this was my problem i finally figured it out. I started googling more specifically and found this question with the solution: https://stackoverflow.com/questions/37908024/form-sends-get-instead-of-post – cactus Jan 04 '21 at 10:31
0

Another issue which I faced for similar problem was that I have named my html file as "index.html" and also php file as "index.php" because of which php was working on php files first In my case changing the name of file worked out for me

VAIBHAV JAIN
  • 29
  • 1
  • 5
0

try this php code in your index page:

<?php 

$name = $_POST['name'];
$email = $_POST['email'];

echo $name .'Welcome '. $lastname .'your email adress is: '. $email;

?>
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Yehya
  • 9
  • 3
0

As you said, if you literally copied and pasted a example from w3schools and got those errors, check if you're using an VSCode with live server plugin. I just stop using the live server plugin (Five server) and my problem with PHP was gone. For some reason, that plugin was the origin of ERROR: Undefined array key.

  • If there is an PHP array error it means that PHP was executed and therefor a full webserver was used and not the live server. But it is worthwhile to lookout for any ` – theking2 Feb 19 '22 at 09:43
  • I think the answer is unclear and not related to the question. – xitas Feb 19 '22 at 11:43
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 19 '22 at 11:43