...

View Full Version : <br> in a text area



Snitz
03-29-2007, 09:24 AM
How can I include the <br> whenever someone writes something in a textarea field and has many <br> in it?
I did it and its showing it as a 1 line!

Nightfire
03-29-2007, 09:32 AM
\n is a newline in plaintext

$str = "blah \n blah \n blah";

Snitz
03-29-2007, 09:54 AM
Yes. but I'm using it as variables.

I have 2 files, index.php and insert_db.php and this is the code of each

index.php

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Thoughts</title>
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="527" bgcolor="#CCCCCC">&nbsp
<form action="insert_db.php" method="POST">
<label>
<div align="center">
<p>
<textarea name="quote" cols="50" rows="20"></textarea>
<br />
<br />
name:
<input type="text" name="name" />
<br />
<br />
email:
<input type="text" name="email" />
<br />
</p>
</div>
</label>
<div align="center">
<input type="submit" value="Submit" />
</div>
</form>;</td>
<td width="500" bgcolor="#666666"><table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td valign="top"><div align="center"><font color="#FFFFFF"><h1>Latest Submissions</h1></font></div></td>
</tr>
<tr>
<td valign="middle"><div align="center">
<?php
$mysqli = mysqli_connect("localhost", "root", "", "blackquote");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
} else {
$sql = "SELECT * FROM data ORDER BY id desc LIMIT 5";
$res = mysqli_query($mysqli, $sql);
if ($res) {
while ($newArray = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
$id = $newArray['id'];
$quote = $newArray['quote'];
$email = $newArray['email'];
$name = $newArray['name']; ?>
<?php echo"".$quote."" ?> - <i>By <a href="mailto:<?php echo "".$email."" ?>"><?php echo "".$name."" ?></a></i><br />
Quote #<?php echo "".$id."" ?><br />
<br />
<?php
// echo"".$quote." - <i>By ".$name."</i><br />Thought #".$id."<br/><br />";
}
} else {
printf("Could not retrieve records: %s\n", mysqli_error($mysqli));
}
mysqli_free_result($res);
mysqli_close($mysqli);
}
?>
</div></td>
</tr>
<tr>
<td valign="bottom"><div align="center">
<?php
$link = mysql_connect("localhost", "root", "");
mysql_select_db("blackquote", $link);
$result = mysql_query("SELECT * FROM data", $link);
$num_rows = mysql_num_rows($result);
?>
<div align="center">
<span class="style3">Now Serving</span>
<INPUT
style="FONT-WEIGHT: bold; FONT-SIZE: 8pt; FONT-FAMILY: verdana; TEXT-ALIGN: center"
onfocus=this.blur() size=11 value="<?php echo "$num_rows"; ?>">
</div></td>
</tr>
</table></td>
</tr>
</table>
</body>
</html>

insert_db.php


<?php
$con = mysql_connect("localhost","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}mysql_select_db("blackquote", $con);
$sql="INSERT INTO data
(quote,name,email)
VALUES
('$_POST[quote]','$_POST[name]','$_POST[email]')";if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
header("location: redirect.php");
exit();
?>

as you can see, i'm not using a plain text, but im fetching records from the database, how can I include the "\" in it?

Nightfire
03-29-2007, 10:05 AM
Anything in a textarea is plaintext, which is why you can type html in there and it'll show as text.

First thing, change these to have quotes



VALUES
('".$_POST['quote']."','".$_POST['name']."','".$_POST['email']."')


So you have no line breaks in your code at all and have <br>'s only?

use


$str = str_replace("<br>","\n",$str);

That'll replace the <br>'s to newlines in the textarea

Snitz
03-29-2007, 10:07 AM
Where would I use it?

Snitz
03-29-2007, 10:09 AM
I don't think it's working coz I added it into insert_db.php


<?php
// -- add the name values of other fields you want here
$required_fields = array('quote','name','email');
foreach($_POST as $key => $value)
{
if (in_array($key,$required_fields) && strlen(trim($value)) == 0)
{
// -- required field was not filled in!
$errors[] = 'You need to fill in '.$key.'!';
}
}
if (is_array($errors))
{
// -- there were some errors, lets print them out
echo 'There were some errors with your form : <br />';
echo implode('<br />',$errors);
echo 'Please correct them before continuing!';
} else {
// -- continue with the rest
}
$con = mysql_connect("localhost","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}mysql_select_db("blackquote", $con);
$str = str_replace("<br>","\n",$str);
$quote=mysql_real_escape_string($_POST['quote']);
$name=mysql_real_escape_string($_POST['name']);
$email=mysql_real_escape_string($_POST['email']);
$sql="INSERT INTO data (quote,name,email) VALUES ('$quote','$name','$email')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}header("location: redirect.php");

// }
?>
I've added the upper code to make all my fields required but if I leave an empty field it'll tell me that I need to fill in all the fields but it'll add an empty record to the database, how can I fix that?

Thank you for your help man.

Nightfire
03-29-2007, 10:10 AM
Actually, I think I misread it. Are you wanting <br>'s to replace a newline in a textarea, or <br>'s to be replaced by newlines?

If it's the first one, then use nl2br() (http://php.net/nl2br).

You use it on whatever variable it is that you're wanting it used on.

Snitz
03-29-2007, 10:18 AM
It's just not working, can you look at the code above and guide me please?
Thank you!

Nightfire
03-29-2007, 10:19 AM
Tell me which way you're wanting it first. Are you wanting <br>'s to show or newlines to show?

Snitz
03-29-2007, 10:27 AM
I have a text area and for example if I wanna write a text with new lines in it, when I fetch it I want them to have break lines: for example:

while entering my data in textarea:
line1
line2
line3

when I fetch them, I don't want them to look like this:
line1 line2 line3, I want them to look just like I posted them!

Nightfire
03-29-2007, 10:30 AM
Ok..


$sql="INSERT INTO data (quote,name,email) VALUES ('nl2br($quote)','$name','$email')";

Snitz
03-29-2007, 10:32 AM
Ok..


$sql="INSERT INTO data (quote,name,email) VALUES ('nl2br($quote)','$name','$email')";


That didn't work
it gave me the following

nl2br(this is whaty I want) - By snitz
Quote #26

Nightfire
03-29-2007, 10:34 AM
Sorry, I'm still half asleep.


$quote=mysql_real_escape_string(nl2br($_POST['quote']));
$name=mysql_real_escape_string($_POST['name']);
$email=mysql_real_escape_string($_POST['email']);
$sql="INSERT INTO data (quote,name,email) VALUES ('$quote','$name','$email')";

That should work

Snitz
03-29-2007, 10:37 AM
Sorry, I'm still half asleep.


$quote=mysql_real_escape_string(nl2br($_POST['quote']));
$name=mysql_real_escape_string($_POST['name']);
$email=mysql_real_escape_string($_POST['email']);
$sql="INSERT INTO data (quote,name,email) VALUES ('$quote','$name','$email')";

That should work
Finally it worked.
can I ask other questions or are you sick of me now? :$

Snitz
03-29-2007, 05:27 PM
Can somebody help create a small search page out of these records?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum