would you know why would a text comes back in one line, i am trying to implement this pm system where i posted for some other isue, but this one is strange. when i am composing a message and keep writing, in the message body i have the similar window like i am typing here and when the line finishes it goes down and continues and if i enter
like this it goes to one down but after i send the message i go to inbox to view it but it comes in one line, does not limit the chracters typed in one line and does not see the enter, instead of going one line down when entered it just levaes a space.

would you know how i could fix this?

thank you

Recommended Answers

All 24 Replies

Are you using textarea?

<tr>
                <td width="150px" align="left" valign="top"><p>Message Body</p></td>
                <td width="" align="left" valign="top"><textarea name="message" type="text" id="message" value="" cols="50" rows="10"></textarea></td>
              </tr>

should it be something else?

Use nl2br function :)
ex.

$message_with_breaks = nl2br($_POST['message']);

hwo and where would i add that code to this?

<?php
session_start();
$user = $_SESSION['username'];
    
    include 'db.php';
    
    //This checks to see if a user is logged in or not by seeing if the sessioned username varialble exists.
	//You could change this check to however you want to validate your members, this is just how I did it.
    if(!$user)
        {
        echo "<br><p>Blah blah you arent logged in and stuff, you should do that or something</p><br>";
        }
        
    else
        {
        //Query the database to see how many messages the logged in user has, then do a little math
		//Find the percentage that your inbox is full (message count divided by 50)
		//50 messages maximum, you can change that
		$sql = mysql_query ("SELECT pm_count FROM users WHERE username='$user'");
		$row = mysql_fetch_array ($sql);
		$pm_count = $row['pm_count'];
		
		//This is the math to figure out the percentage.
		//The message could divided by 50 then multiplied by 100 so we dont have a number less than 1
		$percent = $pm_count/'50';
		$percent = $percent * '100';
        ?>
        <br>
        <center>
        <b><p><a href="inbox.php">Inbox</a> | <a href="compose.php">Compose</a> | <a href="sent.php">Sentbox</a></b>
        <b><p><?php echo "$pm_count"." of 50 Total  |  "."$percent"."% full"; ?></p></b>
        </center>
        <br>
        <?php
        //So here we get the variable submitted through the form to this page
        $reciever = $_POST['username'];
        $subject = $_POST['subject'];
        $message = $_POST['message'];
        $error = '0';
        
        //If they are all blank we jsut say to compose a message
        if(!$reciever AND !$subject AND !$message)
            {
            ?>
            <p><b>Please compose a message.</b></p>
            <br>
            <?php
            }
        
        //Since this form was partially filled out we need to return an error message
        else
            {
            if (!$reciever)
                {
                $error = 'You must enter a reciever to your message';
                }
            
            if (!$subject)
                {
                $error = 'You must enter a subject';
                }
            
            if (!$message)
                {
                $error = 'You must enter a message';
                }
            
            //If the variable error is not set to zero, we have a problem and should show the error message
            if($error != '0')
                {
                echo "<p>$error</p><br>";
                }
            
            //There are no errors so far which means the form is completely filled out    
            else
                {
                //Are the trying to send a message to a real user or to something they just made up?
                $user_check = mysql_query("SELECT username FROM users WHERE username='$reciever'");
                $user_check = mysql_num_rows($user_check);
                
                //The user is real and not made up if this is true
                if($user_check > '0')
                    {
                    //There might already be a sessioned time variable, if so we need to get it for the flood check
                    $time = $_SESSION['time'];
                    
                    //If there is a time variable already, set it to the varialbe $old_time
                    if($time > '0')
                        {
                        $old_time = $time;
                        }
                    
                    //Here we get the minutes and seconds on the server time using the date function, and set that to the $time variable
                    //Now we find the difference between this time ($time) and the time that the page was submitted ($old_time)
                    $time = date('is');
                    $difference = $time - $old_time;
                    
                    $_SESSION['time'] = $time;
                    
                    //If the two times have a difference greater or equal to 15, which is 15 seconds, they can submit the message, this is for flood protection
                    if($difference >= '15')
                        {
                        //Get their private message count
                        $sql = mysql_query ("SELECT pm_count FROM users WHERE username='$reciever'");
                        $row = mysql_fetch_array ($sql);
                        $pm_count = $row['pm_count'];
                        
                        //You cant have more than 50 private messages, if they try sending a message to a user with a full inbox return an error message
                        if(pm_count == '50')
                            {
                            $error = 'The user you are trying to send a message to has 50 private messages, sorry but we cant send your message untill that user deletes some of their messages.';
                            }
                            
                        else
                            {    
                            //And now we stick the message in the database with all the correct information
                            mysql_query("INSERT INTO messages (reciever, sender, subject, message) VALUES('$reciever', '$user', '$subject', '$message')") or die (mysql_error());
							//Add 1 to the pm count, update the reciever with the new pm count
							$pm_count++;
							mysql_query("UPDATE users SET pm_count='$pm_count' WHERE username='$reciever'");
                            }
                            
                        //Let the user know everything went ok.
                        echo "<p><b>You have successfully sent a private message!</b></p><br>";
                        }
                    
                    //Since they are trying to send messages faster than every 15 seconds, give them an error message    
                    else
                        {
                        $error = 'You must wait 15 seconds before sending another private message';
                        }
                    }
                
                //If they mis spelled or, made up a username, then give an error message telling them its wrong.
                else
                    {
                    $error = 'That username does not exist, please try again. Remember to check your spelling, and don\'t make stuff up at random.';
                    }
                }
            }
            
        //Since we may have set the error variable to something while trying to send the messae, we need another error check
        if($error != '0')
            {
            echo "<p>$error</p><br>";
            }
            
        else
            {
            //Here's the form for the input
            ?>
            <form name="send" method="post" action="compose.php">
            <table width="80%">
              <tr>
                <td width="150px" align="left" valign="top"><p>Username</p></td>
                <td width="" align="left" valign="top"><input name="username" type="text" id="username" value="<?php echo "$reciever"; ?>"></td>
              </tr>
              
              <tr>
                <td width="150px" align="left" valign="top"><p>Subject</p></td>
                <td width="" align="left" valign="top"><input name="subject" type="text" id="subject" value="<?php echo "$subject"; ?>"></td>
              </tr>
              
              <tr>
                <td width="150px" align="left" valign="top"><p>Message Body</p></td>
                <td width="" align="left" valign="top"><textarea name="message" type="text" id="message" value="" cols="50" rows="10"></textarea></td>
              </tr>
                  
              <tr>  
                <td></td>
                <td><input type="submit" name="Submit" value="Send Message"></td>
              </tr>
            </table>
            </center>
            </form>
            <?php
            }
        }    
    ?>

thank you

where would add that function here?

//We need to grab the msg_id variable from the URL.
        $msg_id = $_REQUEST['msg_id'];
        
        //Get all of the information about the message with the id number sent through the URL
        $view_msg = mysql_query("SELECT * FROM messages WHERE id = '$msg_id'");
        $msg = mysql_fetch_array($view_msg);
        
        $reciever = $msg['reciever'];
        $sender = $msg['sender'];
        $subject = $msg['subject'];
        $message = $msg['message'];
        
        //If the person who is supposed to recieve the message is the currently logged in user everything is good
        if($reciever == $user)
            {
            //The message was recieved, so lets update the message in the database so it wont show up in the sent page any more
            mysql_query("UPDATE messages SET recieved='1' WHERE id = '$msg_id'");
            
            //Query the database to see how many messages the logged in user has, then do a little math
			//Find the percentage that your inbox is full (message count divided by 50)
			//50 messages maximum, you can change that
			$sql = mysql_query ("SELECT pm_count FROM users WHERE username='$user'");
			$row = mysql_fetch_array ($sql);
			$pm_count = $row['pm_count'];
			
			//This is the math to figure out the percentage.
			//The message could divided by 50 then multiplied by 100 so we dont have a number less than 1
			$percent = $pm_count/'50';
			$percent = $percent * '100';
            
            //Now we will display the little navigation thing, the fullness of the inbox, then display message information stuff, like who its from, the subject, and the body
            ?>
            <br>
            <center>
            <b><p><a href="inbox.php">Inbox</a> | <a href="compose.php">Compose</a> | <a href="sent.php">Sentbox</a></b>
            <b><p><?php echo "$pm_count"." of 50 Total  |  "."$percent"."% full"; ?></p></b>
            </center>
            <br>
            
            <table width="80%">
              <tr>
                <td width="120px"><p>From:</p></td>
                <td width=""><p><a href = "<?php echo "../user/profile.php?user_name=$sender"; ?>"><?php echo $sender; ?></a></p></td>
              </tr>
              
              <tr>
                <td width="120px"><p>Subject:</p></td>
                <td width=""><p><?php echo $subject; ?></p></td>
              </tr>
              
              <tr>    
                <td width="120px"><p>Message Body:</p></td>
                <td width=""><p><?php echo $message; ?></p></td>
              </tr>
            </table>
            </center>
            <?php
            }
        //Everything is not good, someone tried to look at somone else's private message
        else

I don't understand, where is the textarea that you mentioned in your earlier post ?

i think this is to view the message and first post is the send the message and in the first code i posted has the text area

thank you

In here:

<td width=""><p><?php echo nl2br($message); ?></p></td>

thank you it is working on the entered lines but if someone does not enter it keep just going and going in one line where here when i am typing this all is without out entering but does not show it in one line, is there a solution for that too
thanks

Have you tried setting the width of <td> to an actual value? <p> tags should word wrap automatically if the width is specified.

Have you tried setting the width of <td> to an actual value? <p> tags should word wrap automatically if the width is specified.

i did this

<td width="120px"><p><?php echo nl2br($message); ?></p></td>

but did not work

Does the text overflow the width of the paragraph? Let me know if there is any CSS that would affect the <td> or the <p>.

yes the text is long, if that's what you mean and there is no css for it.

thank you

the code is above, that's all

any other ideas will be greatly apiriciated

thank you

if you mean when typing in the textarea the text just continues on one long line, add wrap="physical" to the textarea markup. <textarea name="" cols="" rows="" wrap="physical" class="" id=""></textarea> This will make the textarea wrap at word breaks [spaces] that are close to the end of the line. If it is one large line of non broken text then it will cause the text area to scroll horizontally.

when i am writing the text it is fine (i have the two codes up in the post one is to send one is to view the message) but when i view it is one long line. with this

<td width="250"><p><?php echo nl2br($message); ?></p></td>

at least it sees the enter but if no enter when the text being typed forget it. a longg line like infinte

thank you

You could use the wordwrap function and break the string at however long you would like.

Although there are some shortcomings, as it will treat Mr. Smith as two words and would break between the prefix and last name. I also do not believe it to be utf-8 capable, but there are some good examples on the manual page on php.net.

I've also seen some functions in the past that alleviate the breaking between parts of a name (prefix and suffix) etc, just cant think of one at the moment.

how owuld i use the wordwrap function? what would it look like and where would i add it?

thank you

<td width=""><p><?php echo wordwrap($message, 75, "<br />", true); ?></p></td>

Change 75 to whatever width you want.

commented: thank you +1

ok this worked but now the enter does not work if i write in one line it breaks it but when i am write and press enter it just puts a space and puts it in one line

is there anyway i can use it togetger with the other comand so it would see the enter


thank you

<td width=""><p><?php echo nl2br(wordwrap($message, 75, PHP_EOL, true)); ?></p></td>

The wordwrap function breaks your content down with PHP_EOL which is a constant that is defined to match the proper end of line charachter for your operating system.

Then uses nl2br to convert them to <br>'s including any ones the user entered.

the only thing i caution is against using the last parameter of "true" as this will allow php to cut words in half instead of breaking at the nearest word break

thank you very very much guys, it is solved it looks great,

Glad to be of assistance. Please mark the thread as solved if we've resolved your issue

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.