greatdipanshu Posted August 1, 2009 Share Posted August 1, 2009 Hey.. i m a super-newbie, just started on php. I m using Mysql and Apache 2.2, and everything seems to be working okay. I tried implementing a basic guest-book on my local machine. For that i used three pages 1.) Form.php - Consists of the basic for 2.) process.php - Does the processing 3.) view.php - Views the guestbook The codes to Form.php is <html> <body> <h2>Sign my guest book!!!</h2> <form method=post action="process.php"> <b>Name:</b> <input type=text size=40 name=name> <br> <b>Location:</b> <input type=text size=40 name=location> <br> <b>Email:</b> <input type=text size=40 name=email> <br> <b>Home Page Url:</b> <input type=text size=40 name=url> <br> <b>Comments:</b> <textarea cols=40 rows=4 wrap=virtual name=comments></textarea> <br> <input type=submit name=submit value="Sign!"> <input type=reset name=reset value="Start Over"> </form> </body> </html> And the code to process.php is : <html> <body> <?php mysql_connect("localhost","root","pwd") or die("Could not connect to database"); mysql_select_db("guestbook") or die("Could not select database"); echo $name; if($submit=='Sign!') { $query = "insert into guestbook(name,location,email,url,comments) values('$name','$location','$email','$url','$comments')"; mysql_query($query) or die(mysql_error()); } ?> <h2>Thanks!!</h2> <h2><a href="view.php">View My Guest Book!!!</a></h2> </body> </html> I tested it.. i ran the form.php.. Once i submit, the second page (process.php) shows but when i view the contents of the database, there is no change. And the 'echo $name' in process.php does nothing. When I remove the if clause, i.e., insert the records without any check, a totally blank record gets appended in the database. So the problem is that the form elements are not getting posted to the process.php script. Help me!!! Link to comment https://forums.phpfreaks.com/topic/168374-cannot-get-form-input/ Share on other sites More sharing options...
bundyxc Posted August 1, 2009 Share Posted August 1, 2009 Try this: <html> <body> <?php mysql_connect("localhost","root","pwd") or die("Could not connect to database"); mysql_select_db("guestbook") or die("Could not select database"); echo $name; if($submit=='Sign!') { $query = "insert into guestbook(name,location,email,url,comments) values($_POST['name'], $_POST['location'], $_POST['email'], $_POST['url'], $_POST['comments'])"; mysql_query($query) or die(mysql_error()); } ?> <h2>Thanks!!</h2> <h2><a href="view.php">View My Guest Book!!!</a></h2> </body> </html> You're trying to call variables that you haven't declared. Remember, once posted, it's still declared as $_POST['var']. Link to comment https://forums.phpfreaks.com/topic/168374-cannot-get-form-input/#findComment-888188 Share on other sites More sharing options...
greatdipanshu Posted August 1, 2009 Author Share Posted August 1, 2009 Thanks for the reply.. but it still behaves the same... Then, I tried this... <html> <body> <?php echo $_POST['name']; ?> </body> </html> Still, this doesn't do anything.. i.e. it doesnt display the first field 'name' as i intended it to be.. i get a blank page Link to comment https://forums.phpfreaks.com/topic/168374-cannot-get-form-input/#findComment-888198 Share on other sites More sharing options...
bundyxc Posted August 1, 2009 Share Posted August 1, 2009 Let me know what happens when you try this: <?php error_reporting(E_ALL); ini_set('display_errors','1'); echo 'Script started.'; echo '<br />'; echo $_POST['name']; echo '<br />'; echo 'Script finished.'; ?> Link to comment https://forums.phpfreaks.com/topic/168374-cannot-get-form-input/#findComment-888206 Share on other sites More sharing options...
greatdipanshu Posted August 1, 2009 Author Share Posted August 1, 2009 Script started. Notice: Undefined index: name in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\process.php on line 8 Script finished. So its not getting the fields.. cuz line 8 is where the code tries to access the field.. Edit : I thought maybe 'name' is a reserved sortof, but even the other fields give the same error... Link to comment https://forums.phpfreaks.com/topic/168374-cannot-get-form-input/#findComment-888212 Share on other sites More sharing options...
bundyxc Posted August 1, 2009 Share Posted August 1, 2009 Try changing the form (on form.php) to method="get". Also, your HTML is very sloppy. Remember to use quotes and such. Also, try this as process.php <?php error_reporting(E_ALL); ini_set('display_errors','1'); echo 'Script started.'; echo '<br />'; echo $_REQUEST['name']; echo '<br />'; echo 'Script finished.'; ?> If that STILL somehow doesn't work, try going to process.php?name=John Link to comment https://forums.phpfreaks.com/topic/168374-cannot-get-form-input/#findComment-888229 Share on other sites More sharing options...
greatdipanshu Posted August 1, 2009 Author Share Posted August 1, 2009 Get somehow works..... Link to comment https://forums.phpfreaks.com/topic/168374-cannot-get-form-input/#findComment-888236 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.