通过表单将Textarea中的HTML代码发送到PHP。

时间:2022-11-12 13:20:18

i want to have a textarea where I can edit html code directly. After submitting the form the content if the textarea (with html tags) should be saved to a MySQL database. I use PHP to receive the date and save it to the database. My problem is, that the HTML code is not properly sent to PHP. I do not receive the HTML code but just the text. How could I fix this?

我想要一个可以直接编辑html代码的textarea。在提交表单之后,如果textarea(带有html标记)应该保存到MySQL数据库中,则应该将内容保存到MySQL数据库中。我使用PHP接收日期并将其保存到数据库中。我的问题是,HTML代码没有正确地发送到PHP。我不接收HTML代码,只接收文本。我该怎么解决这个问题呢?

my Form looks like this:

我的表格是这样的:

<form method="post" enctype="multipart/form-data" action="form.php">
   <textarea name="html_code">
      <a href="link">testlink</a>
   </textarea>
   <input type=submit value="submit"/>
</form>

The form.php should now be able to show the content of the textarea

表单。php现在应该能够显示textarea的内容

echo $_POST['html_code'];

shows: testlink

表明:testlink

I want: <a href="link">testlink</a>

我想:< a href = "链接" > testlink < / >

4 个解决方案

#1


4  

Thank you all for your answers. I found the problem. It was Joomla. Joomla removed HTML tags when I got strings via getVar. I had to use the mask option JREQUEST_ALLOWRAW to solve the issue.

谢谢大家的回答。我发现这个问题。这是Joomla。当我通过getVar获得字符串时,Joomla删除了HTML标记。我必须使用mask选项JREQUEST_ALLOWRAW来解决这个问题。

JRequest::getVar('html_code', '', 'post' , 'STRING', JREQUEST_ALLOWRAW);

#2


1  

Are you echoing it into an HTML page? Because the code will be parsed into an actual link.

您是否将其回显到HTML页面中?因为代码将被解析成一个实际的链接。

View the source of your output page.

查看输出页面的源代码。

#3


1  

You're using the wrong encoding type.

您使用了错误的编码类型。

Instead of "multipart/form-data", it should be "text/plain".

它应该是“text/plain”而不是“multipart/form-data”。

You don't have to encode the data as Doug says above; but it will be encoded for you when you submit the form, so don't forget to decode before using.

你不需要像Doug说的那样对数据进行编码;但是当您提交表单时,它将被编码,所以在使用之前不要忘记解码。

#4


1  

Your form should be:

表单应该是:

<form method="post" enctype="multipart/form-data" action="form.php">
   <textarea name="html_code">
      &lt;a href=&quot;link&quot;&gt;testlink&lt;/a&gt;
   </textarea>
   <input type=submit value="submit"/>
</form>

(No, it's not messed up. They're called HTML entities)

(不,没有搞砸。)它们叫做HTML实体)

You can use htmlentities() in PHP to achieve that.

您可以在PHP中使用htmlentities()来实现这一点。

#1


4  

Thank you all for your answers. I found the problem. It was Joomla. Joomla removed HTML tags when I got strings via getVar. I had to use the mask option JREQUEST_ALLOWRAW to solve the issue.

谢谢大家的回答。我发现这个问题。这是Joomla。当我通过getVar获得字符串时,Joomla删除了HTML标记。我必须使用mask选项JREQUEST_ALLOWRAW来解决这个问题。

JRequest::getVar('html_code', '', 'post' , 'STRING', JREQUEST_ALLOWRAW);

#2


1  

Are you echoing it into an HTML page? Because the code will be parsed into an actual link.

您是否将其回显到HTML页面中?因为代码将被解析成一个实际的链接。

View the source of your output page.

查看输出页面的源代码。

#3


1  

You're using the wrong encoding type.

您使用了错误的编码类型。

Instead of "multipart/form-data", it should be "text/plain".

它应该是“text/plain”而不是“multipart/form-data”。

You don't have to encode the data as Doug says above; but it will be encoded for you when you submit the form, so don't forget to decode before using.

你不需要像Doug说的那样对数据进行编码;但是当您提交表单时,它将被编码,所以在使用之前不要忘记解码。

#4


1  

Your form should be:

表单应该是:

<form method="post" enctype="multipart/form-data" action="form.php">
   <textarea name="html_code">
      &lt;a href=&quot;link&quot;&gt;testlink&lt;/a&gt;
   </textarea>
   <input type=submit value="submit"/>
</form>

(No, it's not messed up. They're called HTML entities)

(不,没有搞砸。)它们叫做HTML实体)

You can use htmlentities() in PHP to achieve that.

您可以在PHP中使用htmlentities()来实现这一点。