Joefuss Posted July 24, 2013 Share Posted July 24, 2013 I'll admit it upfront: I am a total noob to this, so please bare with me. I'm trying to create a simple PHP program with an HTML form to submit an input into PHP which then echos back the user's input but I keep getting an "Object not found!" error when I push the submission button on the page. If you could please tell me what I'm doing wrong, I would really appreciate it. My code is: <form action="index.php" method="post"> <input type="text" name="user_input" size="20"> <input type="submit" name="press" value="Press da button!"> </form> <?php if (isset($_POST['user_input'])) { $post= $_POST['user_input']; echo "You posted $post ."; } ?> Again, thanks a ton. Quote Link to comment Share on other sites More sharing options...
KapaGino Posted July 24, 2013 Share Posted July 24, 2013 Hmm, this seems to work fine for me!, this might sound dumb, but I'm a total noob as well xD is the php file you've saved definitely called index.php? Quote Link to comment Share on other sites More sharing options...
codelinx Posted July 24, 2013 Share Posted July 24, 2013 wheres the rest of your html? Quote Link to comment Share on other sites More sharing options...
codelinx Posted July 24, 2013 Share Posted July 24, 2013 i have the rest of the coding done, but i will give you this... so you can read some and learn on your own with testing. Change if (isset($_POST['user_input'])) { ... } To this: if (isset($_POST['submit'])) { ... } Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted July 24, 2013 Share Posted July 24, 2013 Codelinx, that wouldn't do anything, his submit is not named "submit" it is named "press". I would personally go with: <?PHP if($_SERVER['REQUEST_METHOD'] == 'POST') { ..... } ?> Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted July 24, 2013 Share Posted July 24, 2013 (edited) Your code worked fine for me, I changed it a tiny bit. <form action="" method="post"> <input type="text" name="user_input" size="20"> <input type="submit" name="press" value="Press da button!"> </form> <?php $user_input = trim($_POST['user_input']); if (isset($_POST['user_input']) && $user_input != '') { echo "You posted ".$user_input."."; } else { echo "Insert Something"; } ?> Leaving the action empty will take it to same page as the script is. Trim removes whitespace, no need for blank values, also added a check for if was blank Edited July 24, 2013 by QuickOldCar Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted July 24, 2013 Share Posted July 24, 2013 (edited) QuickOldCar, the variable $user_input should be inside of the if statement, otherwise it will display an error. Providing that errors are set to be displayed, which they should be. <?PHP if($_SERVER['REQUEST_METHOD'] == 'POST') { $user_input = isset($_POST['user_input']) ? trim($_POST['user_input']) : FALSE ; if(empty($user_input)) { echo 'No user input entered.'; } else { echo 'User Input: '. $user_input; } } ?> <form action="" method="POST"> <input type="text" name="user_input" size="20"> <input type="submit" name="press" value="Press da button!"> </form> Edited July 24, 2013 by PaulRyan Quote Link to comment 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.