Web Hosting Talk







View Full Version : <br> in a text area


Snitz
03-29-2007, 04:27 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!

I have only 2 files

this is 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>

and this is 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();
?>

if you could fix me the errors i've done, i'd be thankful!

mikexec
03-29-2007, 05:34 AM
In index.php, change $quote = $newArray['quote']; to $quote = nl2br($newArray['quote']);

This will convert the newlines from the textarea quote into html line breaks.

Snitz
03-29-2007, 05:43 AM
It worked, now I have another problem.
I have added a code to make all 3 fields required but it's not working.
When I click submit without filling the fields, it says that I must fill in the fields before I continue and it gives me a php error but when I look in the database I see a new empty record added.

This is the code of insert_db.php

<?php
$con = mysql_connect("localhost","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}mysql_select_db("blackquote", $con);
$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')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
// -- 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
}
header("location: redirect.php");

// }
?>

It needs fixing, I appriciate the help!

mikexec
03-29-2007, 05:54 AM
The problem with the above is the following:

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}

The query is being sent with the mysql_query, even when blank, THEN you're checking the fields. Move all of that to the else line towards the bottom, replacing "// -- continue with the rest ". This will check and send the query AFTER the data checking.

Snitz
03-29-2007, 06:23 AM
Yes, thanks alot.
I moved the code to the top and its working now!
Now, how can I make the email field to be a real email field which means requires an "@"

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

Ks Jeppe
03-29-2007, 05:19 PM
Have you considered using google a bit? I'm sure that it'll generate faster answers than here, at least to some of your questions (like the email one...) :)

horizon
03-30-2007, 07:05 PM
Now, how can I make the email field to be a real email field which means requires an "@"

Replace:


$email=mysql_real_escape_string($_POST['email']);

with:


$email = (isset($_POST['email']) && preg_match("/^\w(\.?[\w-])+@\w(\.?[\w-])+\.[a-z]{2,4}(\.[a-z]{2})?$/i", $_POST['email'])) ? mysql_real_escape_string(stripslashes($_POST['email'])) : "";


Can somebody help create a small search page out of these records?

Replace:


$name=mysql_real_escape_string($_POST['name']);

with:


$name = (isset($_POST['name']) && preg_match("/[^a-zA-Z]/i", $_POST['name'])) ? (stripslashes(trim($_POST['name']))) : "";

Find:


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

add above (not below) :


$search_name = strip_tags(trim($_POST['name']));


Then, find:


// -- continue with the rest


add below:


$sql = "

SELECT name
FROM data
WHERE name LIKE '$search_name'

";

$result = mysql_query($sql);

if ($result) {
while ($search_results = mysql_fetch_array($result)) {
$returned_name = $search_results['name'];
}
echo trim($returned_name); // Edit this line in order to fit your needs if you do not wish to output the results as a simple line.
}


That's it. ;)