musttryharder Posted November 21, 2012 Share Posted November 21, 2012 Can someone help a php newbie? Making a post to php but no data posted. html file contains this: <head> <title>database connection</title> </head> <body> <form name= "search" action="welcome.php" method="post"> Name: <input type="text" id= "fname" name="fname"> <input type="submit" name="submit"> </form> </body> php file contains this: <html> <head> <title> Handler </title> </head> <body> <?php echo "beginning "; if(isset($_POST['search'])) {echo "Welcome" . $_POST["fname"];} else {echo "post not set for fname ";} if($_SERVER['REQUEST_METHOD'] == "POST") { echo "form submitted";}{echo "request method post not set ";} echo "end"; ?> </body> </html> output gives me this: beginning post not set for fname request method post not set end Im using php 5.2. Im running a windows 7 pc with web server configured (iis). I have validated that php is running. The above text is the entire text for each file. Quote Link to comment https://forums.phpfreaks.com/topic/270980-_post-not-working/ Share on other sites More sharing options...
deoiub Posted November 21, 2012 Share Posted November 21, 2012 The form name isn't passed as a $_POST variable. So isset($_POST['search'] is returning false. A simple fix for your code would be to check if $_POST['fname']) is set, or the name of your submit button. - D Quote Link to comment https://forums.phpfreaks.com/topic/270980-_post-not-working/#findComment-1394062 Share on other sites More sharing options...
play_ Posted November 21, 2012 Share Posted November 21, 2012 (edited) You are using the form name in your $_POST check. Use the name of your button. So instead of if(isset($_POST['search'])) {echo "Welcome" . $_POST["fname"];} else {echo "post not set for fname ";} do: if(isset($_POST['submit'])) {echo "Welcome" . $_POST["fname"];} else {echo "post not set for fname ";} --edit-- beaten by someone else. for the second part of your script, you forgot an 'else', so you get "request method post not set" as well. if($_SERVER['REQUEST_METHOD'] == "POST") { echo "form submitted";}ELSE{echo "request method post not set ";} Edited November 21, 2012 by play_ Quote Link to comment https://forums.phpfreaks.com/topic/270980-_post-not-working/#findComment-1394063 Share on other sites More sharing options...
AyKay47 Posted November 21, 2012 Share Posted November 21, 2012 Also, relying on a submit button's value being passed on form submisison is unreliable. Instead use either a hidden input value or $_SERVER['REQUEST_METHOD'] to check for form submission. This code needs to be tweaked a little. You need to verify that the text input actually has a value being outputting its value. Quote Link to comment https://forums.phpfreaks.com/topic/270980-_post-not-working/#findComment-1394080 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.