Textfied with auto append <BR>

HTML, CSS and anything else that deals with client side capabilities.

Moderator: General Moderators

Post Reply
aybyd
Forum Newbie
Posts: 5
Joined: Tue Jul 20, 2010 10:17 am

Textfied with auto append <BR>

Post by aybyd »

Hi, I'm new here, I need to find answer to my question thats why I got a way here.
I just wanted my textfield to be automatically append <BR> tag in every end of the lines before the contents save to my database.

Example:

this is just a test records for my database from my textfield.
This is just a test.
Just another test


After saving it in my database that will looks like this inside may database.

this is just a test records for my database from my textfield.<BR>
This is just a test.<BR>
Just another test<BR>


I need to make in that way coz without <BR> tag, data are appeared in my table cell in only one line.
Thanks.
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Textfied with auto append <BR>

Post by kaszu »

aybyd
Forum Newbie
Posts: 5
Joined: Tue Jul 20, 2010 10:17 am

Re: Textfied with auto append <BR>

Post by aybyd »

Btw, it's not textfield, I am using TextArea for user inputs.
I tried this codes

Code: Select all

<?php
$value = $_POST['newsArticle']
?>

Code: Select all

$post['newsArticle'] = str_replace(array("\r\n", "\r", "\n"), "<br />", $value);
But it doesn't work. Did I missed a thing?
Anyway it save my data to database but still no <br> tag in each new line.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Textfied with auto append <BR>

Post by superdezign »

Firstly, you are likely not saving the altered data to your database, correctly. $post and $_POST are not the same.

Secondly, nl2br() does what you want, as suggested by ~kaszu.
aybyd
Forum Newbie
Posts: 5
Joined: Tue Jul 20, 2010 10:17 am

Re: Textfied with auto append <BR>

Post by aybyd »

superdezign wrote:Firstly, you are likely not saving the altered data to your database, correctly. $post and $_POST are not the same.

Secondly, nl2br() does what you want, as suggested by ~kaszu.
My data is saving successfully in my database but in straight line with no <br> tag append.
Can you help me to revise my code with nl2br2()? Sorry for that I'm newbie php.

here is the other part of the code:

<?php
$value = $_POST['newsArticle']
?>

Code: Select all

<?php
    require_once 'News.php'; // Include The News Class
    $news = new News(); // Create a new News Object
    if(isset($_POST['addNews'])) // If the submit button was clicked
    {
        $post['newsTitle'] = $_POST['newsTitle'];
        // add the news title to the $post array
        
		$post['newsArticle'] = str_replace(array("\r\n", "\r", "\n"), "<br />", $value);
        // add the news article to the $post array
        //$post['postDateTime'] = date;
        // add the current time and date to the $post array
        $post['userId'] = 1;
        // You can ignore userId, unless you have mutilple users
        $post['currentStatus'] = 'Enabled';
        // This will make sure its displayed
        if($news->addNews($post))
        {
            echo 'The News was Added Succesfully<br/>'; // if adding was successful display this message

        }
    }
	

	
?>
aybyd
Forum Newbie
Posts: 5
Joined: Tue Jul 20, 2010 10:17 am

Re: Textfied with auto append <BR>

Post by aybyd »

I tried to use nl2br()
But no data was save into my database after submit. although other info are save but not the content of my textarea
Here is my codes

Code: Select all

<?php

function nl2br2($alterednews) {
$alterednews =str_replace(array("\r\n", "\r", "\n"), "<br />", $_POST['newsArticle']);
return ($alterednews);
}
?>

<?php
    require_once 'News.php'; // Include The News Class
    $news = new News(); // Create a new News Object
    if(isset($_POST['addNews'])) // If the submit button was clicked
    {
        $post['newsTitle'] = $_POST['newsTitle'];
        // add the news title to the $post array
        
		$post['newsArticle'] = $alterednews;
        // add the news article to the $post array
        //$post['postDateTime'] = date;
        // add the current time and date to the $post array
        $post['userId'] = 1;
        // You can ignore userId, unless you have mutilple users
        $post['currentStatus'] = 'Enabled';
        // This will make sure its displayed
        if($news->addNews($post))
        {
            echo 'The News was Added Succesfully<br/>'; // if adding was successful display this message
        }
    }
	

	
?>
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Textfied with auto append <BR>

Post by kaszu »

In you first code $value and in second code $alterednews are undefined (not defined). Try

Code: Select all

<?php
    require_once 'News.php'; // Include The News Class
    $news = new News(); // Create a new News Object
    if(isset($_POST['addNews'])) // If the submit button was clicked
    {
        $post['newsTitle'] = $_POST['newsTitle'];
        // add the news title to the $post array
       
        $post['newsArticle'] = nl2br($_POST['newsArticle']);    //$value and $alterednews were used here, but not defined before
        // add the news article to the $post array

        //$post['postDateTime'] = date;
        // add the current time and date to the $post array
        $post['userId'] = 1;
        // You can ignore userId, unless you have mutilple users
        $post['currentStatus'] = 'Enabled';
        // This will make sure its displayed
        if($news->addNews($post))
        {
            echo 'The News was Added Succesfully<br/>'; // if adding was successful display this message

        }
    }
?>
aybyd
Forum Newbie
Posts: 5
Joined: Tue Jul 20, 2010 10:17 am

Re: Textfied with auto append <BR>

Post by aybyd »

Ouch sorry for that..I forgot to define first. Anyways, I solve my problem. I use nl2br() function, I just define just what you said. Thanks
Post Reply