【问题标题】:Passing a href as string through hidden input field (value)通过隐藏的输入字段(值)将 href 作为字符串传递
【发布时间】:2016-08-19 17:13:31
【问题描述】:

我正在尝试通过隐藏的输入字段传递一些数据。我希望每个部分都由链接到正确页面的锚标记包装。但是当我发送它时,可能会显示隐藏的值,因为不允许通过值字段发送锚标记。我该怎么做?

代码:

foreach($offertecr as $offerte1){
    if($offerte1['id'] != ''){
        $message .= '<a href="#">'.$offerte1['title'].'</a><br>';
    }
}

<input type="hidden" class="form-control-products" name="products" id="products" value="'.$message.'">

这会导致以下结果:

我可能必须为此使用 javascript?

【问题讨论】:

  • 您可以在发送和打印值时使用 htmlspecialchars 和 htmlspecialchars_decode 函数
  • 它与锚标记无关,与 "' 的一切有关,您的 html 输出类似于 &lt;input type="hidden" class="form-control-products" name="products" id="products" value="&lt;a href="#"&gt;Product 4 &lt;/a&gt;"&gt;,您的浏览器很可能会缩短,只是如果您不知道,这是无效的 HTML 标记。

标签: javascript php html


【解决方案1】:

您可以使用 htmlspecialchars 将 html 标签转换为实体,以便它们可以正确显示在您的表单中。

foreach($offertecr as $offerte1){
    if($offerte1['id'] != ''){
        $message .= htmlspecialchars('<a href="#">'.$offerte1['title'].'</a><br>');
    }
}

<input type="hidden" class="form-control-products" name="products" id="products" value="'.$message.'">

当您获得发布的信息时,您必须使用 htmlspecialchars_decode 将实体转换回其对应的标签。

$html_value = htmlspecialchars_decode($posted_value);

另一方面,如果您只想要指向正确页面的 url 的信息,您可以跳过锚标签并仅添加 url 的逗号分隔值。您可以稍后在发布表单以获取网址时分解explode(',', $values) 这些值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    • 2011-03-05
    • 2011-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多