onthespot Posted August 4, 2009 Share Posted August 4, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/168824-solved-comment-positioning/ Share on other sites More sharing options...
WolfRage Posted August 4, 2009 Share Posted August 4, 2009 You need to use an if statement to detect if the form has been submitted if so you display the page but with the corresponding message. Else you display the form. Quote Link to comment https://forums.phpfreaks.com/topic/168824-solved-comment-positioning/#findComment-890719 Share on other sites More sharing options...
_DarkLink_ Posted August 4, 2009 Share Posted August 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/168824-solved-comment-positioning/#findComment-890720 Share on other sites More sharing options...
onthespot Posted August 4, 2009 Author Share Posted August 4, 2009 That helps to an extent, but just means I have a blank white page. Im looking to get the comment intergrated into the site. Quote Link to comment https://forums.phpfreaks.com/topic/168824-solved-comment-positioning/#findComment-890725 Share on other sites More sharing options...
onthespot Posted August 4, 2009 Author Share Posted August 4, 2009 solved it a slightly different way, thanks though Quote Link to comment https://forums.phpfreaks.com/topic/168824-solved-comment-positioning/#findComment-890728 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.