简体   繁体   中英

PHP script doesn't seem to pick up the variable values

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

?>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM