The textarea saves the text correctly except that the output of this text:
Lorem Ipsum is simply the dummy text of printers and text files. Lorem Ipsum is just the dummy text for printers and text files. Lorem Ipsum is just the dummy text for printers and text files.
New paragraph. Lorem Ipsum has been the industry standard dummy text since the 1500s. Lorem Ipsum is simply the dummy text of printing presses and text files.
I save it like this:
Lorem Ipsum is simply the dummy text of printers and text files. Lorem Ipsum is just the dummy text for printers and text files. Lorem Ipsum is just the dummy text for printers and text files. New paragraph. Lorem Ipsum has been the industry standard dummy text since the 1500s. Lorem Ipsum is simply the dummy text of printing presses and text files.
I would like the paragraphs to be saved when rendering $_POST
and also to implement a function to make the text bold , include the hyperlinks.
<form method="POST" action="" enctype="multipart/form-data">
<div class="form-group">
<input name="user_id" type="hidden" class="form-control" id="user_id" aria-describedby="user_id" value="<?php echo $user_id; ?>" required>
</div>
<div class="form-group">
<input name="title" type="title" class="form-control" id="title" aria-describedby="title" placeholder="The title..." required>
</div>
<div class="form-group">
<textarea placeholder="Question content..." name="body" class="form-control" id="Body" rows="10" required></textarea>
</div>
<div class="form-group">
<button type="submit" id="Qasked" name="Qasked" class="btn btn-primary">Publish</button>
</div>
</form>
My insert code in Mysql database:
<?php
if(isset($_POST['Qasked'])){
$form = $_POST;
$user_id = test_input($form['user_id']);
$title = test_input($form['title']);
$body = test_input($form['body']);
$product_asks = '0';
$pdo->beginTransaction();
try{
$sql = "INSERT INTO `products` (`user_id`, `product_name`, `product_description`, `product_asks`)
VALUES (?, ?, ?, ?)";
$stmt = $pdo->prepare($sql);
$stmt->execute(array(
$user_id,
$title,
$body,
$product_asks,
)
);
$pdo->commit();
}
catch(Exception $e){
echo $e->getMessage();
$pdo->rollBack();
}
?>
I've tried putting in the text <br/>
and <b>Texto</b>
, but it doesn't work either, it saves everything as text. I apply a test input class, maybe that gives the problem:
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
Inside a
textarea
HTML cannot be rendered neither for bold nor for links. what I suggest is to use a with the adiv
attribute .contenteditable
true
With the code
jQuery
that I add is to be able to click on the links inside the div. (in the snippet it will not direct you to the pages, it will be blank)To get the text out of the div (with tags and such), you can use:
To pass the value through
_POST
you can use a hidden input text field that holds the value ofinnerHTML
y is the one you send with:and you can do it by putting a method before the
submit
Code:
I use in my web page a form from which I insert text with html tags in the MySQL database without any problem. I do this almost every day.
textarea
in a file, called hereinicio.php
inicio.php
launches a jQuery that sends the entire content of the form to a helper file called hereform-action.php
. jQuery is told to send the data as html (dataType: "html"
)form-action.php
fetches the content of the form submitted by jQuery and inserts it into the database.Everything is done transparently for the user, without having to refresh the page or hide or escape content.
Usage would basically be like this:
home.php
The file where the textarea is
jQuery in start.php
jQuery code in home.php, without forgetting to add the jQuery library
Note: The jQuery, for code clarity, you can have it in a separate file and include it in the start.php header, something like this:
form-action.php
The php file that will be called from Ajax in the post method transparently. Here you must connect to the database and insert Note that this php file must have the same name indicated in the
action
form tag and the full path, if both files are not in the same directory, it must be specified.