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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2077903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:51:50  来源:igfitidea点击:

send HTML Code in Textarea to PHP via Form

htmlformspostjoomlatextarea

提问by dominik

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 数据库中,则内容。我使用 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

form.php 现在应该能够显示 textarea 的内容

echo $_POST['html_code'];

shows: testlink

显示: testlink

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

我想要: <a href="link">testlink</a>

采纳答案by dominik

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 标签。我不得不使用掩码选项 JREQUEST_ALLOWRAW 来解决这个问题。

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

回答by Matt

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.

查看输出页面的来源。

回答by Robert Elwell

You're using the wrong encoding type.

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

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

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

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 上面所说的那样对数据进行编码;但是当你提交表单时它会为你编码,所以在使用前不要忘记解码。

回答by Felix

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()来实现这一点。