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 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题