Coding Help! (POSTS)!

Ask about a PHP problem here.
Post Reply
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

Coding Help! (POSTS)!

Post by Shahlin »

I have a text box and a submit button...which posts...the text typed in the text box!
It's A Very Simple Script!
<?php
if (isset($_POST['post'])) {
        $post = $_POST['post'] ;
if ($_POST['submit']) {
   if (!empty ($post)) {
        echo $post.'<br>' ;

	} else {
 echo 'Please Type In Something!' ;	
		}
	}
}

?>

<form action="" method="POST">
	<input type="text" name="post"> <input type="submit" name="submit" value="Post">
</form>
When I type something and post...it is echoed out and when I type something else and post the previous text goes away!
What I want is...when someone post something, it should be echoed out and when that person post something again! the old post should stay there and the new post should come above it.

Thanks!
Just A PHP Beginner!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Coding Help! (POSTS)!

Post by jacek »

For something this simple you have over complicated it quite a lot. If you are checking to see if the post isset then there is o need to check if the submit button has been pressed :?

So you can make it this
if (isset($_POST['post'])) {
        $post = $_POST['post'] ;
        if (!empty ($post)) {
              echo $post.'<br>' ;
        } else {
              echo 'Please Type In Something!' ;    
        }
}
Then there is no point creating the $post variable since you are just assigning one variable to the other without making any changes to it''s value, so you can make it
if (isset($_POST['post'])) {
        if (!empty ($_POST['post'])) {
              echo $_POST['post'].'<br>' ;
        } else {
              echo 'Please Type In Something!' ;    
        }
}
then, the easiest way to get what you want would probably be to have the previous messages stored in the session, so every time there is a valid message store it in the session, then have that output at the top of the page.
Image
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

Re: Coding Help! (POSTS)!

Post by Shahlin »

I didn't mean this!

What I meant was....when a user types something it should post above....and he posts something else..it should be posted above the old post! For Example :

User : 2
User : 1
<Text Box> <Submit Button>

Should We Connect It To The DataBase For This?
Just A PHP Beginner!
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: Coding Help! (POSTS)!

Post by bowersbros »

store it in the database, when you fetch it, use order by id desc to reverse it
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

Re: Coding Help! (POSTS)!

Post by Shahlin »

Thanks!
Just A PHP Beginner!
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

Re: Coding Help! (POSTS)!

Post by Shahlin »

bowersbros wrote:store it in the database, when you fetch it, use order by id desc to reverse it
This is my current code! :
<?php
require 'config.php' ;
if (isset($_POST['post'])) {
        $post = $_POST['post'] ;
        if (!empty ($post)) {
		$query = mysql_query ("SELECT * FROM `post` ORDER BY id Desc LIMIT 30") ;
		$row = mysql_fetch_assoc($query) ;
                $posts = $row ['post'] ;
		if (mysql_num_rows($query)== 0) {
		 	echo 'No Posts Currently!' ;
		} else if  (mysql_num_rows($query)>=1){
                        echo $post.'<br>' ;
			echo $posts.'<br>' ;
		}
		$querypost = mysql_query ("INSERT INTO `post` VALUES ('','$post')") ;
        } else {
              echo 'Please Type In Something!' ;    
        }
}

?>

<form action="" method="POST">
	<input type="text" name="post"> <input type="submit" name="submit" value="Post">
</form>
But It Doesn't Work! Please Help!
Just A PHP Beginner!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Coding Help! (POSTS)!

Post by jacek »

In what way does it not work ?
Image
Post Reply