简体   繁体   中英

How can I insert HTML values in PHP using jquery ajax?

I have a text area and a button, when the button is clicked it will insert the data from the text area. Only problem is when I am not the one clicking / using it they say it is not working.

The first problem was they have putted some HTML characters that is why it was not inserting in the database.

Is there any way that I can put everything that was entered in the text area into database?

I am using jquery and php and tried htmlentities.

extract($_POST);

$message2 = htmlspecialchars($message) ;

$date = date("Y-m-d H:i:s");
$sql  = "INSERT INTO sampletable SET `message` = '$message2', `emailtype` = 'Report', `datectd`='$date' ";

Do not forget to totally convert HTML to entities. Use htmlentities .

htmlentities($message, ENT_QUOTES);

This will convert Quotes in HTML as well.

Example:

// 1st way : Converting to Entities
$msg = "<b>bold</b>";
$msg = htmlentities($msg, ENT_QUOTES);
echo $msg; // &lt;b&gt;bold&lt;/b&gt;

// 2nd way : Adding Backslashes
$msg = "<a href="something">bold</a>";
/*
Note: we are adding slashes 2 times
because we also want to escape Quotes in Database Query
1st slash is for PHP
2nd slash is for Database
*/
$msg = addslashes(addslashes($msg));
echo $msg; // <a href=\"something\">bold</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM