dongyi1996 2016-01-16 13:15
浏览 67
已采纳

将表作为html表单中的隐藏值传递 - 奇怪的单/双引号问题

I have a php function that outputs a form with button (with post) to download a pdf of the table on the previous page. For some reason, I get weird results if I try to use double quotes, but single quotes in the string declaration work ok (except when I try to feed the butter to js innerhtml).

This works:

$form = '<form action="../php/pdf_and_die.php" method="POST" target="_blank">';
$form .= '<input type="hidden" name="table" value="'.$table.'">';
// more lines here
$form .= '<input type="hidden" name="page" value="portrait">';
$form .= '<input type="submit" value="     '.$button_name.'     " class="button"></form>';

But this doesn't:

$form = "<form action='../php/pdf_and_die.php' method='POST' target='_blank'>";
$form .= "<input type='hidden' name='table' value='{$table}'>";
// more lines here
$form .= "<input type='submit' value='{$button_name}     ' class='button'></form>";

When I try to do the second way with double quotes, the table is displayed before the button. screenshot with double quotes

Shouldn't single/double quotes be mostly the same as far as php and html are concerned, as long as they're used consistently? (barring differences like variables in strings like "foo{$bar}").

This wouldn't be a problem and I would just go with the working single quotes for the string declaration inside the php function, except that on another page, I have a pulldown menu with onclick and a js function that replaces one of these buttons for another based on the academic session the user chooses, and innerhtml doesn't seem to like the single quote (double quotes inside) option. screenshots

On the page that displays the button: (abbreviated for readability)

<?php
    foreach ($sessions as $session) {
        // string of attendance table
        $session_table = get_class_attn_table($course, $acad_year, $session);

        // get_pdf_form_button returns a string
        $attn_button = get_pdf_form_button($session_table);

        // add the output html to the array as a string
        $sessions_text[] = $attn_button;
    }
?>
<script type="text/javascript">
    function toggle_session() {
        var x = "" + document.getElementById("session_select").value;
        var session1_text = "<?php echo $sessions_text[0];?>";
        var session2_text = "<?php echo $sessions_text[1];?>";

        if (x == "1") {
            document.getElementById("change_session").innerHTML = session1_text;
        }
        else if (x == "2") {
            document.getElementById("change_session").innerHTML = session2_text;
        }
    }
</script>

<select id="session_select" onchange="toggle_session()">
    <option selected value="">Select...</option>
    <option value="1">1</option>
    <option value="2">2</option>
</select> 

I'm pretty new to php and js, so maybe I missed something simple.

  • 写回答

1条回答 默认 最新

  • duanqian6982 2016-01-16 18:20
    关注

    As had been worked out in the comments, the $table variable did have some quotes in it, which messed with the HTML.

    Solutions for problematic quotes:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?