Jump to content

[SOLVED] Comment positioning


onthespot

Recommended Posts

Ok so the problem I have.

When a user enters a comment on my site, if the comment is either too short, none existent or successful, an echo comment is displayed.

 

$from1=$_SESSION['username'];
$to1=$_GET['comp'];
$comment1=$_POST['comment'];

$query="INSERT INTO leaguecomments VALUES (NULL, '$to1','$comment1', now(), '$from1')";
if (!empty($_POST)) {
if(!$comment1 || strlen($comment1 = trim($comment1)) == 0)
    echo "Comment not entered";
else if(!$comment1 || strlen($comment1 = trim($comment1)) < 10)
    echo "Comment too short, must be 10 characters at least";
else if (!$comment1 || strlen($comment1 = trim($comment1)) > 10){
    echo "".$_SESSION['username'].", you have added a comment ";
	mysql_query($query);}}

 

Thats the code for processing the following form.

 

<form action="" method="POST">
<textarea name="comment" rows="4" cols="66" ></textarea>
<div align="right"><input type="submit" name="submit" value="Add Comment"></div>
</form>

 

My problem is that the form is in the body of the page, whereas the processing code is at the very top of the php script, before I even start the html tags.

 

Therefore when I enter a comment, the echo from the processing page displays at the very top of the page. But if I move the processing code to the body, it doesn't work as it uses POST and therefore must be at the top.

 

How can I get the comments to display near the form, but the whole thing still function correctly?

Link to comment
https://forums.phpfreaks.com/topic/168824-solved-comment-positioning/
Share on other sites

It is fairly easy,

Just wrap the whole code in an "if else" statement

if ($_POST['submit']) {
$from1=$_SESSION['username'];
$to1=$_GET['comp'];
$comment1=$_POST['comment'];

$query="INSERT INTO leaguecomments VALUES (NULL, '$to1','$comment1', now(), '$from1')";
if (!empty($_POST)) {
if(!$comment1 || strlen($comment1 = trim($comment1)) == 0)
    echo "Comment not entered";
else if(!$comment1 || strlen($comment1 = trim($comment1)) < 10)
    echo "Comment too short, must be 10 characters at least";
else if (!$comment1 || strlen($comment1 = trim($comment1)) > 10){
    echo "".$_SESSION['username'].", you have added a comment ";
      mysql_query($query);}}
}
else
{

// and then you put another } inside PHP tags after the HTML closing tags

 

Hope it helps.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.