duanmei1946 2016-03-15 02:39
浏览 195
已采纳

难以从<select>获取值

I'm trying to get the user's choice in a <option> tag.
The multiple <option> tags are generated from data from the database.

Here's the form:

    <form action="mudaVideo.php" method="post">

            <label for="sel1">escolher o vídeo</label>
            <select class="form-control" name="apagar">

              <?php

  $sql = "SELECT * FROM video ORDER BY idvideo";
  $result = mysqli_query($conn,$sql) or trigger_error("SQL", E_USER_ERROR);

  while ($list = mysqli_fetch_assoc($result)) {

          $idvideo = $list["idvideo"];
          $titulo = $list["titulo"];
          $subtitulo = $list["subtitulo"];
          $link = $list["link"];

          echo '<option>'.$titulo.'</option>';

  } // end while

               ?>

                <option>wow ate me passo</option> -->
           </select>
        <button type="submit" name="apagar" class="btn btn-primary" id="enviar" style="background-color:transparent; color:black;">apagar vídeo</button>

        </div>
      </form>

Here's my php:

    if(isset($_POST['apagar'])) {


  $id = $_POST['apagar'];

  echo 'a escolha foi ' . $id . '!';

  $sql = "DELETE FROM video
          WHERE idvideo='".$id."'";
$result = mysqli_query($conn, $sql);


 if ($conn->query($sql) === TRUE)

    {

   }

   else

   {
       echo "Error: " . $sql . "<br>" . $conn->error;
   }
}

When I try to echo the value of $id, it's empty, so something wrong is escaping me.

Thanks in advance

  • 写回答

2条回答 默认 最新

  • dongmu5106 2016-03-15 02:41
    关注

    You'll need the value attribute in <option>.

    For example:

    <select name="apagar">
      <option value="1">First</option>
      <option value="2">Second</option>
      <option value="3">Third</option>
    </select>
    

    You can solve this by using:

    echo '<option value="' . $titulo . '">'.$titulo.'</option>';
    

    Also, both the <button> and <select> have the same name apagar, thus it will have value of the latter as it comes after <select>:

    <select class="form-control" name="apagar">
    
    <button type="submit" name="apagar" class="btn btn-primary" id="enviar" style="background-color:transparent; color:black;">apagar vídeo</button>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻看一个题
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)