如何从PHP中的下拉列表中获取所选值的值?

时间:2016-06-02 07:07:33

标签: php html

这是我的下拉:

 <form name="form" method="POST" style="display:inline;">
            <select name="category" id="category" value="category" class="form-control ddplaceholder" style="width:220px;font-size:18px;font-family:Roboto;">
                 <option value="" disabled selected>Select Category</option>
            <?php
            $sth = $conn->prepare('Select name From category');
            $sth->execute();
            $data = $sth->fetchAll();   
            foreach ($data as $row ){
                if($row['name']!="")
             echo ' <option id=\"CategoryName\" nameCategoryNameVendorName\" value="' .$row['name']. '">'.$row['name'].'</option>';
            }
            ?> 
            </select>
     </form>

我正尝试使用以下代码访问它:

if(!empty($_POST['category']))
     $category=$_POST['category'];

当我回显$category的值时,它返回null。这是为什么?如何从下拉列表中获取所选值的值?

2 个答案:

答案 0 :(得分:1)

您好在PHP中,我们可以使用提交按钮或使用javascript提交表单。 如果你只想使用php,你应该添加一个提交按钮并检查是否已发布,然后检查这样的类别。

var roledataPromise = $http({url: 'someurl.php', method: "GET", params: {user_id: eid}});
var rolePromise = $http({url: 'someurl.php', method: "GET", params: {comp_id: comp_id}}); 

roledataPromise.then(function(roledataResponse){
    $scope.roledata = roledataResponse;

    rolePromise.then(function(roleResponse){
         $scope.role = roleResponse;

         //at this point you have $scope.roledata and $scope.role
         $scope.role.forEach(function(item){
           $scope.roledata.forEach(function(savedItem){
              if(savedItem.currentroledes === item.value){
                 item.selected = true;
                 console.log(item);
                 return true
               }
            })
        });

     });
 });

答案 1 :(得分:0)

您需要在表单中使用输入类型提交来获取php中的值

<form name="form" method="POST" style="display:inline;">
                <select name="category" id="category" class="form-control ddplaceholder" style="width:220px;font-size:18px;font-family:Roboto;">
                     <option value="" disabled selected>Select Category</option>
                <?php
                $sth = $conn->prepare('Select name From category');
                $sth->execute();
                $data = $sth->fetchAll();   
                foreach ($data as $row ){
                    if($row['name']!="")
                 echo ' <option id="CategoryName" value="' .$row['name']. '">'.$row['name'].'</option>';
                }
                ?> 
                </select>    <input type="submit" />
         </form>

    <?php
    if(!empty($_POST['category']))
         $category=$_POST['category'];
    print_r($category);
    ?>