Jump to content

Recommended Posts

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.

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 by QuickOldCar

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 by PaulRyan
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.