2

I am new to PHP and HTML and trying to create a HTML form and submitting it to PHP using POST method, but the variable values are not being picked up by the POST :-

<html>
<head>
</head>
<body>

<form action="form_script.php" method="POST">

<p>Name: <input type="text" name="name" size="50"/></p>
<p>Size: <select name="size">
<option value="small">small</option>
<option value="medium">medium</option>
<option value="large">large</option>
</select>
</p>
<p>Gender: <input type="radio" name="gender" value="male"/> Male
<input type="radio" name="gender" value="female"/> Female</p>
</br>
<input type="submit" name="submit" value="Submit Button"/>

</form>
</body>
</html>

PHP:

    <?php
$name=$_POST['name'];
$size=$_POST['size'];
$gender=$_POST['gender'];

print "<p>Name: $name Size: $size Gender: $gender<p>";

?>
10
  • 1
    are you running this on a live server, or just as a local file? Commented Nov 22, 2015 at 17:17
  • Do you have a doctype declaration? See stackoverflow.com/questions/4106544/post-vs-post-get-vs-get and try a var_dump($_POST); Commented Nov 22, 2015 at 17:18
  • 1
    I don't even have to test this. Code's valid. Commented Nov 22, 2015 at 17:20
  • 1
    " i forgot to mention that i am running this code on a WAMP server on my localhost, not sure if that woukd matter? – pranay 56 secs ago" - Yeah kind of. Make sure that PHP's installed and properly configured and that all services are running. Comments here probably won't get answered. Depends on how you're accessing those files also. Commented Nov 22, 2015 at 17:25
  • 1
    You cant run php from file:///C:/, you have to use Localhost. If you have Notice: Undefined index: Name it could be the issue of name!=Name. Is the form and php code in the same file? That would cause an issue on initial page load, which can be fixed by using an isset() Commented Nov 22, 2015 at 17:59

1 Answer 1

2

i am using file:///C:/wamp/www/L/form_script.php to run the script, if u use localhost/L/form_script.php i get an error :- Notice: Undefined index: Name in C:\wamp\www\L\form_script.php on line 2"

There you go. You need to use http://localhost/L/form_script.php and not file:///C:/wamp/www/L/form_script.php just as I had a feeling you were using and I even made a comment about it.

PHP directives do not get parsed when accessing a PHP file in a web browser like a regular HTML document .html/.htm does, etc.

If you're getting undefined notices, then this tells me you're using both your HTML form and PHP inside the same file, or you're entering empty values in the inputs.

"@Sean these are 2 different files the HTML file is named form.php & the php file is form_script.php – pranay 5 mins ago"

You are accessing form_script.php directly before using your HTML form, in turn throwing you those notices.

  • I have no idea why you are trying to access it before your form.

Steps: Access your form file first http://localhost/L/form.php, then submit. You will then be taken to form_script.php afterwards, in turn showing you what was entered in the inputs.

You need to use a conditional isset() or !empty() against your inputs/POST arrays, or use two seperate files; one for your form and the other for your handler, while still checking for empty()'ness also; it's always best.

"Notice: Undefined index: Name"

Funny, your code shows as name="name" and the POST array $_POST['name'] also.

No idea where Name with an uppercase "N" comes from.

Name and name are two different animals altogether.

if(!empty($_POST['name']))

as an example and apply that method to your other POST arrays.

With the possibility of using either/or && (AND) - || (OR) operators.

  • Use isset() for radio/checkmarks/submit buttons, and !empty() for user inputs.

Using a ternary operator can also be an option.

Sign up to request clarification or add additional context in comments.

4 Comments

Dear Fred-ii- appologies for missing ur comment earlier, i added if(!empty($_POST['name'])) to theform_script.php and tried again and this time it picked up 3 out of 3 values you are the man!! Thanks a tonner:-)
Dear Fred-ii- i have now accepted your answer. i am very new and want to improve my PHP, can you please suggest any resources on the net using which i can improve my skills ?
Dear Fred-ii- Thank You!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.