在 php 表中填充下拉列表


Populating a DropDown List in php Table

在 php 代码中,以下代码用于构建一个表格,其中包含格式和所有 3 个用于文本的单元格和一个用于图像上传的单元格。

 echo "<table border=1 align=center>
                                   <tr bgcolor='#FFC600'>
                                   <th>sectionID</th>
                                   <th>name</th>
                                   <th>price</th>
                                   <th>image</th>
                                   <th>add</th>
                                   </tr>";
                                   echo "<form action=Data.php enctype=multipart/form-data method=post>";
                                   echo "<tr>";
                                   // give the insert row a special color
                                   echo "<tr bgcolor='#FFC600'>";
                                   echo "<td><input type=text name=cat_id></td>";
                                   echo "<td><input type=text name=item_name></td>";
                                   echo "<td><input type=text name=item_price></td>";
                                   echo "<td><input type=file name=uploaded_file></td>";
                                   echo "<td>" . "<input type=submit name=add value=addNew" . " </td>";
                                   echo "</tr>";
                                   echo "</form>";
                                   echo "</table>";
                                   echo "<br>";

我需要将cat_id的输入更改为下拉列表,我尝试实现此代码但没有成功。

<select>
  <option value="a">aa</option>
  <option value="b">bb</option>
  <option value="c">cc</option>
  <option value="d">dd</option>
</select>

这是我到目前为止尝试的:将上面的 6 行添加到代码中,如下所示:

echo "<form action=myItemsData.php enctype=multipart/form-data method=post>";
                                   echo "<tr>";
                                   // give the insert row a special color
                                   echo "<tr bgcolor='#FFC600'>";
                                   echo "<td>
                                   <select name=cat_id>
                                   <option value="a">aa</option>
                                   <option value="b">bb</option>
                                   <option value="c">cc</option>
                                   <option value="d">dd</option>
                                   </select>
                                   </td>";
                                   echo "<td><input type=text name=item_name></td>";
                                   echo "<td><input type=text name=item_price></td>";
                                   echo "<td><input type=file name=uploaded_file></td>";
                                   echo "<td>" . "<input type=submit name=add value=addNew" . " </td>";
                                   echo "</tr>";
                                   echo "</form>";
                                   echo "</table>";
                                   echo "<br>";

当我尝试在浏览器上加载页面时,我得到了这个:

内部服务器错误

服务器遇到内部错误或配置错误,无法完成您的请求。

请与服务器管理员联系,并告知他们错误发生的时间,以及您可能执行的任何可能导致错误的操作。

有关此错误的详细信息,请参阅服务器错误日志。

因此,我通过使用 HEREDOC 简化了事情,这对于长而多行的字符串非常有用。 唯一真正需要的是将选择简单地剪切并粘贴到输入所在的单元格中。

echo <<<EOT
<table border=1 align=center>
  <tr bgcolor='#FFC600'>
    <th>sectionID</th>
    <th>name</th><th>price</th>
    <th>image</th>
    <th>add</th>
  </tr>
  <form action=Data.php enctype=multipart/form-data method=post>
  <tr>;
  // give the insert row a special color
  <tr bgcolor='#FFC600'>
    <td>
      <select name=cat_id>
        <option value="a">aa</option>
        <option value="b">bb</option>
        <option value="c">cc</option>
        <option value="d">dd</option>
      </select>
    </td>
    <td><input type=text name=item_name></td>
    <td><input type=text name=item_price></td>
    <td><input type=file name=uploaded_file></td>
    <td><input type=submit name=add value=addNew> </td>
  </tr>
  </form>
</table>
<br>
EOT;

编辑:

除非出于某种原因必须以字符串形式使用此 HTML(即您只是渲染 HTML),否则为什么不这样做呢?

如果您需要在标记中插入变量,则只需将变量放在PHP标签中:

<keygen><?php echo $myVariable ?></keygen>

试试这个:

<?php
    ... some preceding php here ...
?>
<table border=1 align=center>
  <tr bgcolor='#FFC600'>
    <th>sectionID</th>
    <th>name</th><th>price</th>
    <th>image</th>
    <th>add</th>
  </tr>
  <form action=Data.php enctype=multipart/form-data method=post>
  <tr>;
  // give the insert row a special color
  <tr bgcolor='#FFC600'>
    <td>
      <select name=cat_id>
        <option value="a">aa</option>
        <option value="b">bb</option>
        <option value="c">cc</option>
        <option value="d">dd</option>
      </select>
    </td>
    <td><input type=text name=item_name></td>
    <td><input type=text name=item_price></td>
    <td><input type=file name=uploaded_file></td>
    <td><input type=submit name=add value=addNew> </td>
  </tr>
  </form>
</table>
<br>
<?php
    ... some following php here ...
?>