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

Add new selection with options from database

本文关键字:新选择 选择 添加 选项 数据库      更新时间:2023-09-26

我有一个函数,可以使用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数据库。我为尚未从"添加新"按钮生成的代码制作了代码:

<?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 代码与变量"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); ?>;

它:)