donglu8344812 2015-12-31 02:06
浏览 92
已采纳

无法获取html5的<option>元素以在PHP中显示值

Users can choose an item from a category, and update their choices if they choose to do so. I am displaying the items using the select element. The items are displayed via php to keep the html code succinct.

     <div class="form-group">   
        <label for="category">category</label>
         <select name="category" id="">               
<?php
    getAllCategories();
?>          
         </select>
     </div>

getAllCategories is a function stored in a separate file.

function getAllCategories() {

global $connection;
$categoryQuery = "SELECT * FROM categories";
$runCategoryQuery = mysqli_query($connection, $categoryQuery);


checkQuery($runCategoryQuery);                               
$row = mysqli_fetch_assoc($runCategoryQuery);

while ($row) {


    $categoryName = $row['category_name'];  
    $categoryId = $row['category_id']; 

    echo "<option value='' name='category_name'> $categoryName </option>";


     $row = mysqli_fetch_assoc($runCategoryQuery);   
   }
}

I am able to display this data correctly. Now if the user wants to change the category, he can click the category section and choose any from the drop-down list. Once he clicks the submit button, the POST request runs.

problem: I am unable to get the updated choice of the user, as I get the following message:

notice: Undefined index: category_name in /Applications/XAMPP/xamppfiles/htdocs/demo/cms/admin/includes/edit_post.php on line 8

The code on that line is : $postCategory = $_POST['category_name'];

category_name is defined in the getAllCategories function.

What am I doing wrong?

I've already seen this question, but as I mentioned above, I know what the error means. I have no problem retrieving values from a form or a file. I am having problems with retrieving values from the select element.

  • 写回答

1条回答 默认 最新

  • dougang5088 2015-12-31 02:12
    关注

    if u want to get value of a option in php, then use name of select, not option element

    $postCategory = $_POST['category_name']; 
    

    change to

    $postCategory = $_POST['category'];
    

    $postCategory will return value of selected option

    function

    function getAllCategories() {
    
      global $connection;
      $categoryQuery = "SELECT * FROM categories";
      $runCategoryQuery = mysqli_query($connection, $categoryQuery);
    
    
      checkQuery($runCategoryQuery);                               
      $row = mysqli_fetch_assoc($runCategoryQuery);
    
      while ($row) {
    
    
          $categoryName = $row['category_name'];  
          $categoryId = $row['category_id']; 
    
          echo "<option value='".$categoryId."'> $categoryName       </option>";
    
    
           $row = mysqli_fetch_assoc($runCategoryQuery);   
         }
      }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料