July 8, 201610 yr Hiya one & all. Ive been away from php etc for a while due to looking after an ill family member. I never got time to really delve into what i really love (ie coding) as didnt have the time. Hope to do so from now on. Briefly i typed the following & returned the next following. Can u plz advise why? I am looking to learn. Thankyou <!Doctype html> <html> <head> <title>Form Feedback</title> </head> <body> <?php # Script 2.2 - handle_form.php // Create a shorthand for the form data. $name = $_REQUEST['name']; $email = $_REQUEST['email']; $comments = $_REQUEST['comments']; // Not used: $_REQUEST['age'], // $_REQUEST['gender'], and // $_REQUEST['submit']. // Print the submitted information. echo "<p>Thank you, <b>$name</b>, for the following comments:<br> <tt>$comments</tt></p> <p>I Will reply to you at <i>$email</i></p>\n"; ?> </body> </html> ******************************************************************* Notice: Undefined index: name in C:\xampp\htdocs\handle_form.php on line 10 Notice: Undefined index: email in C:\xampp\htdocs\handle_form.php on line 11 Notice: Undefined index: comments in C:\xampp\htdocs\handle_form.php on line 12 Thank you, , for the following comments: I Will reply to you at
July 8, 201610 yr Undefined index simply means PHP can't find a index (such as 'name' or 'email') in a array ($_REQUEST). It looks like no data is being passed to the page. Be sure you all your inputs on the form page have the correct name values. It should look something like this: <form action="handle_form.php" method="post"> <input type="text" name="name"> <input type="email" name="email"> <textarea name="comments"></textarea> <input type="submit"> </form>I would also swap out $_REQUEST with $_POST so the method is explicit, but what you've written is still perfectly valid. Edited July 8, 201610 yr by jacobg830
July 8, 201610 yr Author Hi jacob. Thanks for replying. Ive done what you suggested even before but still get the error?
July 8, 201610 yr Are you visiting handle_form.php without pressing the submit button on the form? Also, make sure caching in XAMPP is disabled. <input type="text" name="email"> You mean type="email" is valid HTML5, the browser checks the user has entered a valid email address.
July 8, 201610 yr Author Ive no idea how to disable caching. I just go to the handle_form.php through the localhost
July 8, 201610 yr I just go to the handle_form.php through the localhost You need to go to the HTML form page, fill it in and click submit. This should then take you to the handle_form.php. Visiting handle_form.php directly through http://localhost/handle_form.php means no data is being sent to it.
July 10, 201610 yr Can I just suggest ... <?php # Script 2.2 - handle_form.php// Create a shorthand for the form data. if($_SERVER['REQUEST_METHOD']=='POST') { // Only works if posted.$name = $_POST['name'];$email = $_POST['email'];$comments = $_POST['comments'];// Print the submitted information.echo "<p>Thank you, <b>$name</b>, for the following comments:<br><tt>$comments</tt></p><p>I Will reply to you at <i>$email</i></p>\n";}?></body></html> You could also do with some validation and common spam traps.
July 10, 201610 yr And some sanitisation! Lol yeah that's a given, I bundle anything to do with user input under validation (e.g. data types, sanitation, email validation etc) whereas I bundle anti-spam bits prior to any of this as if I detect its automated then I generally kill the post before processing any of the fields.
July 11, 201610 yr I'm sure YOU do, but I bet the OP doesn't. Validation and sanitisation are definitely 2 topics worth knowing well before working with any kind of form submission...sanitisation being the more important one for security. As for captcha I avoid them like the plague, honey potting is my preferred method and works surprisingly well. For sure, but since I write procedural with each post field (after spam traps) gets tested for valid type (e.g. number only, no weird cc: in posts etc) and then escaped before storing which combined is validation and sanitation or am I missing something? Edit: Forgot to say I thought the OP was just about why it wasn't working rather than asking for a full tech demonstration of the correct way to write the whole thing in php ... e.g. $_POST over $_REQUEST. Edited July 11, 201610 yr by BrowserBugs
Create an account or sign in to comment