سؤال

I create a simple backed area for my client to post new job openings and was wanting to give them the ability to format the text a little by adding line breaks in the job description text that will be visible on the front end.

The job openings are stored in a MySQL database.

Example of what I'm talking about:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla quis quam sollicitudin, bibendum enim a, vulputate turpis.

Nullam urna purus, varius eget purus quis, facilisis lacinia nibh. Ut in blandit erat.

I've would like the breaks to happen when my client hits enter / return on the keyboard.

Any help on this matter would be appreciated.

------------UPDATE------------

okay so after much trial and error I got it somewhat working. I added this line in my upload / action code.

$description = nl2br(htmlspecialchars($_POST['description']));

Full upload code is:

<?php
include($_SERVER['DOCUMENT_ROOT'] . "/connections/dbconnect.php");



$date = mysql_real_escape_string($_POST["date"]);
$title = mysql_real_escape_string($_POST["title"]);
$description = mysql_real_escape_string($_POST["description"]);
$description = nl2br(htmlspecialchars($_POST['description']));

// Insert record into database by executing the following query:
$sql="INSERT INTO hire (title, description, date) "."VALUES('$title','$description','$date')";
$retval = mysql_query($sql);


echo "The position was added to employment page.<br />
<a href='employment.php'>Post another position.</a><br />";

?>

Then on my form I added this to the textarea, but I get an error. FYI that is line 80 the error is refering to.

Position Details:<br />
<textarea name="description" rows="8"><?php echo str_replace("<br />","",$description); ?></textarea>
</div>

Here is what the error looks like.

enter image description here

Here is my results page code:

<?php
$images = mysql_query("SELECT * FROM hire ORDER BY ID DESC LIMIT 10");
while ($image=mysql_fetch_array($images))
{
    
    ?>
<li data-id="id-<?=$image["id"] ?>">
<div class="box white-bg">
<h2 class="red3-tx"><?=$image["title"] ?> <span class="date-posted blue2-tx"><?=$image["date"] ?></span></h2>
<div class="dotline"></div>
<article class="blue3-tx"><?=$image["description"] ?><br />
<br />

For more information please call ###-###-####.</article>
</div>
</li>
    <?php
}
?>

If I delete all that error copy and write out a real position with line breaks it works. I have no idea how to fix the error though. Again any help would be appreciated. Thanks!

هل كانت مفيدة؟

المحلول

you can use str_replace

$statement = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla quis quam sollicitudin, bibendum enim a, vulputate turpis.

Nullam urna purus, varius eget purus quis, facilisis lacinia nibh. Ut in blandit erat."

$statement = str_replace(chr(13),"<br/>", $statement);

query example : INSERT INTO table (statement) VALUES ('$statement');

hope this can help you

EDIT :

if you want display the result at textarea from database u can using this code

$des = $row['description'] //my assumption that your feild name at table inside mySQL is description

Position Details:<br />
<textarea name="description" rows="8"><?php echo str_replace("<br />",chr(13),$des); ?></textarea>
</div>

hope this edit can help your second problem

نصائح أخرى

I would start by answering a couple of questions first

  • Do I want my database to store html-formatted user input?
  • Is the data going to be editable afterwards?

Since you seem to want only nl2br, a simple approach would be to save the content as is in the database, then use nl2br() on the output side as Marcin Orlowski suggested.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top