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

Add new selection with options from database

我有一个功能可以使用 table 中的按钮添加一个新的选择,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>\"\;
\?>'

这个组合似乎很不对,有帮助吗?谢谢

这不起作用,因为转义的反斜杠被 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); ?>;

而且有效:)