Page 1 of 1
Textfied with auto append <BR>
Posted: Tue Jul 20, 2010 10:30 am
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.
Re: Textfied with auto append <BR>
Posted: Tue Jul 20, 2010 11:26 am
by kaszu
Re: Textfied with auto append <BR>
Posted: Tue Jul 20, 2010 7:03 pm
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.
Re: Textfied with auto append <BR>
Posted: Tue Jul 20, 2010 7:49 pm
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.
Re: Textfied with auto append <BR>
Posted: Tue Jul 20, 2010 7:53 pm
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
}
}
?>
Re: Textfied with auto append <BR>
Posted: Tue Jul 20, 2010 8:10 pm
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
}
}
?>
Re: Textfied with auto append <BR>
Posted: Wed Jul 21, 2010 11:33 am
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
}
}
?>
Re: Textfied with auto append <BR>
Posted: Fri Jul 23, 2010 9:14 am
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