问题:使用数据库中的选项添加新选择

我有一个使用 IDu003d"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>\"\;
\?>'

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

解答

这不起作用,因为 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); ?>;

它有效:)

Logo

PostgreSQL社区为您提供最前沿的新闻资讯和知识内容

更多推荐