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>";

?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
pranay
  • 61
  • 10
  • 1
    are you running this on a live server, or just as a local file? – Sean Nov 22 '15 at 17:17
  • Do you have a doctype declaration? See http://stackoverflow.com/questions/4106544/post-vs-post-get-vs-get and try a `var_dump($_POST);` – clemens321 Nov 22 '15 at 17:18
  • 1
    I don't even have to test this. Code's valid. – Funk Forty Niner Nov 22 '15 at 17:20
  • @clemens321 doctype shouldn't matter and won't stop their code from working, if they're actually using a webserver. – Funk Forty Niner Nov 22 '15 at 17:21
  • 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. – Funk Forty Niner Nov 22 '15 at 17:25
  • @Fred-ii- you are spot on, based on [this comment](http://stackoverflow.com/questions/33857597/php-script-doesnt-seem-to-pick-up-the-variable-values#comment55480010_33857730) by the OP on their output – Sean Nov 22 '15 at 17:34
  • @Sean OP might have downloaded the wamp package and maybe even the wrong one; wampserver has 2 different installs for Windows. If they didn't reboot after installation, that could be an issue; it was for me when I installed wamp some time ago. Plus, if all services are not running, bam, yet another issue. On top of, we don't know if they're using `file:///form.html` or `http://localhost/form.html` and will do just that; output as code on submission. – Funk Forty Niner Nov 22 '15 at 17:37
  • @Sean i have been using this WAMP server for Joomla installation and Joomla website works fine, so reboot is not the issue, WAMP server is green so all services are running fine , i am using file:///C:/wamp/www/L/form_script.php to run the script, if u use http://localhost/L/form_script.php i get an error :- Notice: Undefined index: Name in C:\wamp\www\L\form_script.php on line 2 – pranay Nov 22 '15 at 17:50
  • 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()` – Sean Nov 22 '15 at 17:59
  • @Sean these are 2 different files the HTML file is named form.php & the php file is form_script.php – pranay Nov 22 '15 at 18:23

1 Answers1

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.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • 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:-) – pranay Nov 22 '15 at 18:39
  • 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 ? – pranay Nov 22 '15 at 18:47
  • @pranay Sure. Here's a few http://www.phptherightway.com/ and http://php.net/manual/en/tutorial.php and http://www.tutorialspoint.com/php/ and http://www.webmonkey.com/2010/02/php_tutorial_for_beginners/ – Funk Forty Niner Nov 22 '15 at 18:51
  • Dear Fred-ii- Thank You!! – pranay Nov 22 '15 at 18:55