doujiexin1136
doujiexin1136
2019-03-27 21:30

使用PHP从数据库发出填充选择框

  • variables
  • php
  • html
  • while-loop
已采纳

Basically, I have 1 selection box at the top of my page, called cups. There are 8 teams assigned to each cup. Upon selecting a cup and hitting submit I want all of the teams associated in that cup to fill 8 selection boxes elsewhere on the page allowing the user to choose which team plays the other.

I've attempted many things, and I have got to the point where I can echo the teams, they simply aren't being echoed into the selection boxes in a different div on the same page

<input type="submit" name="submit" value="Generate" class="submit">
<?php
if (isset($_POST['submit'])) { //checking if submit button was clicked

    include_once 'action/dbcon.php';

    $cname = $_POST['cupname'];

    if (empty($cname)) {
        header("Location: ../table.php?field=empty"); //return them if fields are empty
        exit();
    } else {
        $sql = "SELECT * FROM teams WHERE cup_name='$cname'";
        $show_teams = mysqli_query($conn, $sql);

        if (!$show_teams) {
            echo "Could not load teams! " . "(" . mysqli_error($conn) . ")";
        }
        while ($team = mysqli_fetch_assoc($show_teams)) {
            echo $team["team_name"];
        }
    }
    echo "dank";
}
?>
</form>


    <div class="seed-container">
        <div class="column-1">
        <form class="seed-form">
            <select name="team" required>
            <option></option>
            </select>
            <select name="team" required>
            <option></option>
            </select>

So, I'm echoing the right results, just not as options in the selection boxes where I want them

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dsp1836 dsp1836 2年前

    most easy might be to echo them where they should appear:

    <select name="team" required>
    <?php while ($team = mysqli_fetch_assoc($show_teams)) { ?>
        <option value="<?php echo $team["team_id"]; ?>"><?php echo $team["team_name"]; ?></option>
    <?php } ?>
    </select>
    
    点赞 评论 复制链接分享
  • drzdu44226 drzdu44226 2年前

    You can handle this with jQuery - In my example I simply cut and pasted from one of my cms scripts.

    The form below is included in a larger form so the the form tag and submot are not in the example.

    The form:

    <?php
        echo '<div class="row">';       
            # page-type
            echo '<div class="col_3">';
                $getPgType = $db->prepare('SELECT page_type_id, page_type FROM page_types WHERE page_type_id >= 1 order by page_type asc');
                $getPgType->execute();
                echo '<label>Page Type</label><br>';                
                echo '<select id="" name="page_type" class="expand">';
                    echo '<option value=""'; if($page_type == ''){echo 'selected';} echo '>Select Page Type</option>';
                    while($row = $getPgType->fetch(PDO::FETCH_ASSOC)){
                        $id = $row['page_type_id']; $pagetype = $row['page_type'];                      
                        echo '<option value="'.$pagetype.'"'; if($page_type == $pagetype){echo 'selected';} echo '>'.ucwords($pagetype) .'</option>';
                    }   
                echo '</select>';               
            echo '</div>';
    
            # classification_type
            echo '<div class="col_3">';
                echo '<label for="classification_type">Classification Type</label><br>';
                echo '<select id="classificationType" name="classification_type" class="expand">';
                    echo '<option value=""'; if($classification_type == ''){echo 'selected';} echo '>Select Classification Type</option>';
                    echo '<option value="classification"'; if($classification_type == 'classification'){echo 'selected';} echo '>Classification</option>';
                    echo '<option value="category"'; if($classification_type == 'category'){echo 'selected';} echo '>Category</option>';
                    echo '<option value="content"'; if($classification_type == 'content'){echo 'selected';} echo '>Content</option>';               
                echo '</select>';
            echo '</div>';
    
            # classification
            echo '<div class="col_3">';
                $selstmt = $db->prepare('SELECT classification_id, classification FROM classifications WHERE classification_id >= 1');
                $selstmt->execute();
                echo '<label>Classification</label><br>';
                echo '<select id="getAssignedCats" name="classification" class="expand">';
                    echo '<option value=""'; if($classification == ''){echo 'selected';} echo '>Select Classification</option>';
                    while($row = $selstmt->fetch(PDO::FETCH_ASSOC)){
                        $classification_id = $row['classification_id']; $classN = $row['classification'];
                        echo '<option value="'.$classification_id.'"'; if($classification == $classification_id){echo 'selected';}
                        echo '>'.ucwords(str_replace('-',' ',$classN)).'</option>';
                    }
                echo '</select>';
            echo '</div>';
    
            # category
            echo '<div id="categoryHTML" class="col_3"></div>';
        echo '</div>';
    ?>
    

    Notice the <div id="categoryHTML" class="col_3"></div>... this is where the jQuery renders the desired selection box.

    The jQuery

    /* PARSE GET ASSIGNED CATEGORIES
    ======================================================================================== */ 
        $("#getAssignedCats").change(function(){
            var type = $("#classificationType").val();
            if(type != 'classification'){
                var class_id = $(this).val();
                var url = 'parse/get-assigned-categories.php';
                var postit = $.post( url, {class_id:class_id});     
                postit.done(function( data ) {$('#categoryHTML').html(data);}); 
            }       
        });
    

    When the selection box #getAssignedCats is changed by the user the jquery collects the value and posts it to the parse.php file.

    Parse PHP

    <?php
        $level = '../../../';
        include($level.'core/ini/start.php');
    
        $class_id = $_POST['class_id'];
    
        if($class_id != 888888){
    
            $selCat = $db->prepare('SELECT cat_id, category FROM categories WHERE classification = :THEID ORDER BY category asc');
            $selCat->bindValue(':THEID',$class_id,PDO::PARAM_INT); $selCat->execute(); $cat_ct = $selCat->rowCount();
            echo '<label>Category</label>';
            echo '<select name="category" class="expand">';
                echo '<option value=""'; if($category == ''){echo 'selected';} echo '>Select Parent category</option>';
                if($cat_ct <= 0){
                    echo '<option value="777777"'; if($category == '777777'){echo 'selected';} echo ' class="blue">This is a Category Page</option>';}
                elseif($cat_ct >= 1){
                    while($row = $selCat->fetch(PDO::FETCH_ASSOC)){
                        $cat_id = $row['cat_id'];
                        $category = $row['category'];
                        echo '<option value="'.$cat_id.'"'; if($category == $cat_id){echo 'selected';}
                        echo '>'.ucwords(str_replace('-',' ',$category)).'</option>';
                    }
                }       
            echo '</select>';
        }
    ?>
    

    Hope this helps.

    点赞 评论 复制链接分享