Hi,

I'm writing a simple page for University.

I need it to basically just echo user input from a text box back onto the screen. Here is the code I have so far:

Code blocks are created by indenting at least 4 spaces
... and can span multiple lines


<!DOCTYPE html>
<meta charset='UTF-8'>
<title>Comments</title>
</head>
<body>
<h3>Post Comment</h3>
<form action="index.php" method="post">
<textarea name ="content" rows="3" cols="100"></textarea>
<br>
<input type="submit" value="POST"/>
</form>
</body>
</html>

I have no idea how to get the user input to echo back to the screen. My friend suggested PHP and says it's fairly simple and can be done in a few lines of code.

I attempted to write a PHP code to do this, but it didn't work. I would be greatful if someone could point me in the right direction of where to start.

Seems like your friend who said it could easily be done in a few lines of code would be the best place to start.

It is fairly simple.

  • Check the form has been posted
  • Check the content isn't empty
  • Display/echo the comment

And here is the code to do it:

<?php

    if(isset($_POST['content'] && !empty($_POST['content'])) {

        echo $_POST['content'];
    }
?>

Put this code within the page where you want the comment to be echo'd. Alternatively assign the posted data to a variable so that you can use it within the page more than once and also means you can hide the form if you also want to do that.

To assign to a variable, replace echo $_POST['content'] with $comment = $_POST['content'] and place the php above all html instead of within the page and when you need to display the comment you just echo it

<?php echo $comment; ?>

I am not going to go into replacing the form but maybe something you could look into and try for yourself.

Hey, Thanks for your response. I had something a little more complicated originally involving writing and reading from a text file
but in both cases (The php I wrote and the php you gave me, nothing is posted to the page). I click post and all it does is clear the
form. I don't see anything get submitted. :\

I tried the alternative you suggested as well and that also didn't work.
This is the complete code as it stands now.

<html>
<meta charset='UTF-8'>
<title>Comment</title>
</head>
<body>

<?php
    if(isset($_POST['content'] && !empty($_POST['content'])) {
        echo $_POST['content'];
    }
?>

<h3>Post Comment</h3>
<form action="index.php" method="POST">
<textarea name ="content" rows="3" cols="100"></textarea>
<br>
<input type="submit" value="POST"/>
</form>
</body>
</html>

Should this work?

Should this work?

yes, your <head> tag doesn't open though

Also depending on your php server, some are set to supress notices by default so you can actually just do this:

<html>
<head>
<meta charset='UTF-8'>
<title>Comment</title>
</head>
<body>
<?php
    echo $_POST['content'];
?>
<h3>Post Comment</h3>
<form method="POST">
<textarea name ="content" rows="3" cols="100"></textarea>
<br>
<input type="submit" value="POST"/>
</form>
</body>
</html>

Try taking out the action like i did above, not setting action defaults to post back to itself - maybe index.php is a different page?

just copied the code and uploaded to one of our servers and runs fine, if it's not working for you there must be something up with your server.

or you arn't saving it as a .php file

EDIT

just found the error there was a typo in pixies code, isset() is missing the closing bracket after the closing ]

<?php
    if(isset($_POST['content']) && !empty($_POST['content'])) {
        echo $_POST['content'];
    }
?>

Yes, that's fixed it : )

Thank you for your help everyone!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.