简体   繁体   English

如何使用jquery ajax在PHP中插入HTML值?

[英]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. 第一个问题是他们放置了一些HTML字符,这就是为什么它没有插入数据库中的原因。

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. 我正在使用jquery和php并尝试了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. 不要忘记将HTML完全转换为实体。 Use htmlentities . 使用htmlentities

htmlentities($message, ENT_QUOTES);

This will convert Quotes in HTML as well. 这也将转换HTML中的引号。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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