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);   
         }
      }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀