2

I am currently completing php a practice assessment and my browser tells me that variable "quan" on line 4 is undefined. My Programming Teacher is stumped as to what is wrong as my code seems to be correct.

Here is the error message I am receiving: Notice: Undefined index: quan in /Applications/XAMPP/xamppfiles/htdocs/phpassesment/shopping list/list.php on line 4

Html Form Page:

    <form action="list.php" method="post">
<fieldset>
<legend>Shopping List</legend>
Name:    <input type="text" name="name" placeholder="McLovin Juskinsky" pattern="[a-Z]{2,30}" autofocus><br>
Item:    <input type="text" name="item" placeholder="olive oil" pattern="[a-Z]{2,30}"><br>
Quantity:<input type="text" name="quan" placeholder="0" pattern="[0-9]|[1-9][0-9]|100]"><br>
<input type="submit">
</fieldset>
</form>

PHP results page:

<?php
$yourName = $_POST["name"];
$yourItem = $_POST["item"];
$yourQuantity = $_POST["quan"];
echo "<h1>Welcome " . $yourName . "<h1>";

everything about this code works except "quan" is undefined for some unknown reason. Any help would be much appreciated!

Phovon
  • 37
  • 2
  • 1
    everything seems to be correct. Try restarting your web server. – Camilo Go Jr. Aug 17 '17 at 03:17
  • 1
    Try closing the web browser down and reloading the page. Also check that you are loading the correct file this has caught me out a few times :-S – Hayden Passmore Aug 17 '17 at 03:20
  • There is no variable named `quan` in the code you presented. Please provide the exact error message you receive. Also check the developer console and verify the form data being sent. – chris85 Aug 17 '17 at 03:25
  • 2
    Also `[1-100]` is allowing `1` or `0`. Try `min` and `max` (https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input), or write out the longer regex version `([0-9]|[1-9][0-9]|100)`. – chris85 Aug 17 '17 at 03:28
  • Possible duplicated : https://stackoverflow.com/questions/15571391/input-value-0-empty – Jorge SB Aug 17 '17 at 03:29
  • @JorgeSB There is no `empty` usage in the code provided here, if there was it wouldn't throw the error described. – chris85 Aug 17 '17 at 03:30
  • Working fine for me – Rey Norbert Besmonte Aug 17 '17 at 03:32
  • try to remove pattern of quan and see the results, and always validate the input using PHP – Eng Cy Aug 17 '17 at 03:33
  • @chris85 the validation of variables is required, because PHP not require a variable declaration and in some cases where a variable is set with a value as zero, empty string or null, is necesary this task. – Jorge SB Aug 17 '17 at 03:58
  • @JorgeSB None of that relates to the usage of `empty()`. Please look at the error and the code the OP provided again. – chris85 Aug 17 '17 at 10:30

0 Answers0