在数据库中用PHP填充选择框

时间:2019-03-27 21:30:07

标签: php html variables while-loop

基本上,我在页面顶部有1个选择框,称为杯子。每个杯子有8个团队。选择杯子并点击提交后,我希望与该杯子相关的所有团队在页面上的其他位置填充8个选择框,以便用户选择哪个团队参加另一个。

我已经尝试了很多事情,而且我已经可以回音团队了,只是他们并没有被回音到同一页面上不同div的选择框中

<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>

因此,我在回应正确的结果,只是不希望选择框中的选项

2 个答案:

答案 0 :(得分:1)

最简单的方法可能是在它们应出现的位置回显它们:

<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>

答案 1 :(得分:0)

您可以使用jQuery处理此问题-在我的示例中,我只是从一个cms脚本中剪切并粘贴了

下面的表单以较大的形式包含在内,因此该示例中没有form标记和submot。

表单:

<?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>';
?>

注意<div id="categoryHTML" class="col_3"></div> ...,这是jQuery呈现所需选择框的地方。

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);}); 
        }       
    });

当用户更改选择框#getAssignedCats时,jQuery会收集该值并将其发布到parse.php文件中。

解析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>';
    }
?>

希望这会有所帮助。