PHP将变量传递给隐藏字段

时间:2013-08-07 02:30:15

标签: php simplexml php-5.3

好的,我使用PHP来解析XML文件并显示它; HTML的上下文。这是代码。 示例文本是 - 不要放弃!

$xml = simplexml_load_file('data/quotes.xml');
            foreach ($xml as $quote) {
                $text = $quote->text;
                echo '<div class="itemWrapper">'.
                            '<div class="quoteHolder">'.
                                    '<p class="quote">'.$text.'</p>'.
                            '</div>'.

                            '<form class="selectionButtons">'.
                                    "<input type='hidden' value='$text' name='quote'>".
                                    '<input class="submitButton" type="button" value="create your design">'.
                            '</form>'.
                     '</div>';
            }

所以,当我在段落中使用$ text变量时它显示正确,但当我将它传递给表单的隐藏字段时,我只得到:Don(所以它在该单引号之前停止)它发生在每个带引号的文本中。为什么会这样,这里有什么问题?

2 个答案:

答案 0 :(得分:1)

HTML input字段要求转义内容。方便的是,PHP具有为您完成所有工作的功能:

$display_text = "Don't give up!";
$input_text = htmlspecialchars($text);

Reference

答案 1 :(得分:1)

尝试使用这个......

$this_text = "Don't give up!";
$text = htmlspecialchars($this_text, ENT_QUOTES);

echo "<input type='text' value='$text' />";

我已经测试过了..

相关问题