Get the solution ↓↓↓
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!
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!
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>
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
try this php code in your index page:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
echo $name .'Welcome '. $lastname .'your email adress is: '. $email;
?>
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.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.