...

View Full Version : textarea value process



extremed
12-04-2010, 12:54 PM
Hi

I wanted to know how to get each line value of a textarea and add <br /> to end of each line before inserting it to database

lets say textarea is like



line1
line2
line3


i want to read line by line and add a <br /> to end of each line

line1<br />
line2<br />
line3<br />



any help would be great

sorry for the language mistakes, hope someone can understand what i mean

Regards

Lamped
12-04-2010, 01:00 PM
str_replace("\n", '<br />', $_POST['mytextarea']);

extremed
12-04-2010, 01:15 PM
ive inserted value like


$nfo = str_replace("\n", '<br />', $_GET['nfo']);

and retrieved it from data base, still text shows like

line1line2line3

Lamped
12-04-2010, 01:19 PM
Not sure why I overcomplicated it, there's a PHP function called nl2br():

http://php.net/manual/en/function.nl2br.php

extremed
12-04-2010, 01:25 PM
when i should use nl2br(), when insert into db or when retrieve from db?

i tried


$nfo = nl2br($_GET['nfo']);

and insert into db and retrieve it from db but still no luck

Lamped
12-04-2010, 01:39 PM
Either when inserting or retrieving should be fine. There's something else wrong here, so show us more of the code.

extremed
12-04-2010, 02:03 PM
i used ajax to submit data, maybe it caused problem

i tried to insert and retrieve without ajax and nl2br() works perfect

Lamped
12-04-2010, 02:08 PM
Well, without the code, we can only guess.

extremed
12-04-2010, 02:18 PM
index.php



<script src="js/submit.js"></script>
<form action="javascript:insert()" method="post">
<textarea id="nfo" name="nfo" cols="55" rows="1"></textarea>
<input name="Submit" type="submit" value="[ Insert Data ]"/>
</form>


submit.js looks like this



function createObject() {
var request_type;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}else{
request_type = new XMLHttpRequest();
}
return request_type;
}
var http = createObject();
var nocache = 0;

function insert() {
document.getElementById('insert_response').innerHTML = "<b>Processing...</b>"
var nfo = document.getElementById('nfo').value;
nocache = Math.random();

http.open('get', 'insert.php?nfo='+nfo+'&case='+1+'&nocache = '+nocache);
http.onreadystatechange = srvreply;
http.send(null);
}

function srvreply() {
if(http.readyState == 4){
var response = http.responseText;
document.getElementById('cver').value= response;
document.getElementById('insert_response').innerHTML = ""
}
}



insert.php



if($_GET['case']==1){

if($_GET['nfo']!=""){
$nfo = nl2br($_GET['nfo']);


$sql = "insert into test (info) values ('$nfo')";
mysql_query($sql) or die('Error Inserting Values' . mysql_error());
echo 'Successful!';
}
else
{
echo 'Please fill the field!';
}
}



This is how i retrieve


<?php
include ("connect.php");

$id = $_GET['id'];

$sql = "select * from posts where id='$id'";
$result = mysql_query($sql);
while($dat=mysql_fetch_array($result)){

echo $dat['info'];
}

?>

Lamped
12-04-2010, 02:58 PM
Unless I'm missing something, it should work. What's in your database? Does it have <br /> in there?

extremed
12-04-2010, 03:02 PM
no <br /> in database only text as it is

extremed
12-04-2010, 04:24 PM
Thanks Lamped for your help and time

finally got it work by replacing below line.


var nfo = document.getElementById('nfo').value.replace(/\n/g,'<br />')



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum