【问题标题】:Add new selection with options from database使用数据库中的选项添加新选择
【发布时间】:2015-11-24 13:16:57
【问题描述】:

我有一个使用 ID="maTable" 的表格中的按钮添加新选择的功能

<button type="button" onclick ="addNew()"> Add More Agent</button>

使用JS:

 function Addnew(){
 var table=document.getElementById("maTable");
 var row=table.insertRow([table.rows.length]-1);
 var len=(table.rows.length);
 var newid="agentID"+len;
 var newida="agentGroup"+len;

 var cell1=row.insertCell;
 var cell2=row.insertCell;
 var cell3=row.insertCell;

 var new_optionAgent =
 "<select class=\"opsi\"id="'+newida+'">"
 +"<option selected=\"selected\"disabled=\"disabled\">Agent<\/option>"
 +"<option value=\"agentA\">Agent A<\/option>"
 +"<option value=\"agentB\">Agent B<\/option>"
 +"<option value=\"agentC\">Agent C<\/option>"
 +"<option value=\"agentD\">Agent D<\/option>"
 +"<\/select>"

cell1.innerHTML="Choose Agent" +" "+len;
cell2.innerHTML=":";
cell3.innerHTML= new_optionAgent;
}

有了这个我可以得到一个按钮,它将生成一个带有 4 个选项的新选择(它有效)。但是,当我想使用数据库中的列表更改选项时,问题来了。我使用 php 和 postgres 数据库。我为尚未从“AddNew”按钮生成的代码制作了代码:

<?php
        $que=pg_query("SELECT agentname FROM Agent");

        echo "<select name=\"agentname1\"class=\"opsi\" id=\"agentGroup1\" required>";
        echo "<option value=\"\" selected=\"selected\"disabled='disabled'>Agent</option>";
        While($row=pg_fetch_array($que))
        {
            echo '<option value="'.$row['agentname'].'"> '.$row['agentname'].'</option>';
        }
        echo "</select>";
    ?>

现在我想制作“AddNew”按钮,该按钮会从数据库中生成带有选项列表的选择。我通过在一些符号中添加“\”将 php 代码与变量“new_optionAgent”结合起来。但它不起作用。

我是这样组合的

    var new_optionAgent =
'<\?php
\$que=pg_query(\"SELECT agentname FROM Agent\")\;

echo \'<select name=\\\"agentname1\\\"class=\\\"opsi\\\" id=\\\"agentGroup1\\\" required>\'\;
echo \'<option value=\\\"\\\" selected=\\\"selected\\\"disabled=\\\'disabled\\\'>Agent</option>\'\;
While(\$row=pg_fetch_array(\$que))
{
    echo \'<option value=\"\'\.$row[\'agentname\']\.\'\"> \'\.$row[\'agentname\']\.\'</option>\'\;
}
echo \"<\/select>\"\;
\?>'

这个组合似乎很错误,有什么帮助吗?谢谢你

【问题讨论】:

    标签: javascript php postgresql variables selection


    【解决方案1】:

    这不起作用,因为 PHP 使用转义的反斜杠来转义双引号。因此,您将需要添加另一组反斜杠并将其转义,或者在 PHP 中使用单引号字符串:

    <?php
        $que=pg_query("SELECT agentname FROM Agent");
    
        echo '<select name=\"agentname1\"class=\"opsi\" id=\"agentGroup1\" required>';
        echo '<option value=\"\" selected=\"selected\"disabled=\'disabled\'>Agent</option>';
        While($row=pg_fetch_array($que))
        {
            echo '<option value="'.$row['agentname'].'"> '.$row['agentname'].'</option>';
        }
        echo "</select>";
    ?>
    

    编辑

    您不能在这样的 Javascript 变量中内联 PHP。试试这个:

    <?php
        $que=pg_query("SELECT agentname FROM Agent");
    
        $whateverYouWannaCallThisString = '';
    
        $whateverYouWannaCallThisString .= '<select name=\"agentname1\"class=\"opsi\" id=\"agentGroup1\" required>';
        $whateverYouWannaCallThisString .= '<option value=\"\" selected=\"selected\"disabled=\'disabled\'>Agent</option>';
        While($row=pg_fetch_array($que))
        {
            $whateverYouWannaCallThisString .= '<option value="'.$row['agentname'].'"> '.$row['agentname'].'</option>';
        }
        $whateverYouWannaCallThisString .= "</select>";
    ?>
    
    <script type="text/javascript">
        var new_optionAgent = "<?php echo $whateverYouWannaCallThisString; ?>";
    </script>
    

    关于转义的更多信息

    您转义字符的全部原因是因为您使用了围绕字符串本身的字符。例如:如果您要定义一个带有双引号 " 的字符串,如下所示:var myString = "Yolo",并且您希望在该字符串中使用双引号 ",如下所示:var myString = "Dude, wheres "my" car",那么您需要转义双引号 "就是在那个字符串里面是这样的:var myString = "Dude, wheres \"my\" car".

    这同样适用于 PHP

    //编辑: 我编辑了变量:

     var new_optionAgent =
     <?php echo json_encode($whateverYouWannaCallThisString); ?>;
    

    它有效:)

    【讨论】:

    • 所以我只为每个 ' " . $ ? 和 \ ,,, 加上反斜杠,对吗?
    • 如果你使用的是单引号字符串,你只需要转义单引号,如果你使用的是双引号字符串,你只需要转义双引号。
    • 为双引号字符串添加另一组转义黑线?喜欢 ; echo \'
    • 我确实在答案中包含了应该是一个工作示例。要让它在 PHP 中使用双引号字符串,您首先需要转义反斜杠,然后转义双引号:\\\"
    • @Joachim 我做到了。它没有给出任何错误。但它也不起作用。恐怕我为其他符号赋予黑色是错误的。还有哪些其他符号必须和不可以我给 backsles?
    猜你喜欢
    • 2011-06-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-19
    • 1970-01-01
    • 2012-12-10
    • 1970-01-01
    • 2013-02-10
    • 1970-01-01
    相关资源
    最近更新 更多