Thursday, 19 October 2017

xampp - Undefined Index for $_POST (noob question!)













I
am just learning PHP and I keep getting an Undefined Index error. The book I'm learning
from has an HTML form and a PHP page that processes the form, using the following
format:




name="howlong" />

// The PHP starts with one line like this for
each of the form fields in the HTML
$how_long = $_POST
['howlong'];


// And there is one line for each one like
this to output the form data:
echo ' and were gone for ' . $how_long .
'
';


The
example I'm working with has about 12 form
fields.



What's odd is that not all of the
variables throw this error, but I can't see a pattern to
it.



I've checked that all HTML fieldnames match
up with the PHP $_POST variable name I entered, and I've made certain that when I fill
out the form and submit it that all fields are filled in with something. Interestingly,
the completed code that can be downloaded for the book also throws this
error.




I realize this code may not
reflect best practices, it's from the first chapter of the book and obviously I am a
noob :)



In case it makes a difference, I am
using PHP 5.3.5 on XAMPP 1.7.4 with Windows 7 Home Premium.



Answer




Remember to set the method to POST on the
form tag...



heres the code i used to try yours,
and it worked to me:



in a file named
test.php:










type="submit" value="submit"/>







and
in
testProc.php:



if
(isset($_POST)) {
if (isset($_POST["howlong"])){
$howlong =
$_POST['howlong'];
echo ' and were gone for ' . $howlong . ' />';

}
}

?>


Just
as an advise, to make display manipulation with stylesheets i recommend to put forms
within a table, like
this:



























name="howlong" />








Hope
you can use this...


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...