mardi 24 octobre 2017

How to populate li value into another dropdown after you select from first dropdown?

I'm sorry, I still a new beginner for coding.What I doing IS NOT selection option dropdown. The problem is I can get the value of li tag from database but how to populate to another dropdown with ajax and php?

Which mean, I have 2 dropdown list, the first dropdown list is to select MOVIE, the second dropdown list is to select DATE. SO when I select MOVIE, the second dropdown list will populate the date of the MOVIE which I select.

I found a solution with using Select tag and option tag, but for more advanced I need to add image into dropdown list, So I have to use ul tag and li tag to dropdown list.

I hope you guys can help me! Thank.

Below is my coding:

HTML:

////////////////select movie//////////////////

          <div class="select_movie" style="float:left;">
                <div class="nice-select dropdown-toggle" id="selectmovie"  data-toggle="dropdown" tabindex="0" style="margin-left: 200px;  margin-top: 29px;"><span class="current">Select Movie</span>
                    <ul class="dropdown-menu scrollable-menu ul_movie" id="m_movie" >
                        <?php
                        $sql = "SELECT id,name,date,time,image FROM movie";
                        $res = mysqli_query($conn, $sql);
                        if(mysqli_num_rows($res) > 0) {
                            while($row = mysqli_fetch_object($res)) {
                                echo '<li data-value="' . $row->id . '"class="option"  style="margin-top:10px; margin-bottom:10px;"><img src="' . $row->image . '" style="width:50px; height:80px; margin-right:10px;">' . $row->name . '</li>';

                            }
                        }

                        ?>
                    </ul>
                </div>
         </div>

////////////////select date//////////////////

  <div class="select_date" style="float:left">
                <div class="nice-select dropdown-toggle" data-toggle="dropdown" tabindex="0" style="margin-left: 150px;  margin-top: 29px;"><span class="current">Select Date</span>
                    <ul class="dropdown-menu scrollable-menu ul_date" id="selectdate"  >

                    </ul>
                </div>
            </div>

Javascript:

<script>
    $(document).on('click', '.ul_movie li', function () {
//        alert('movie');
        var movie_id = $(this).attr('data-value');
        console.log(movie_id)
        if(movie_id !== "") {
            $.ajax({
                url:"get_datetime.php",
                data:{m_id:movie_id},
                type:'POST',
                success:function(response) {
//                    var resp = $.trim(response);
                    $('.ul_date li').html(response);
                }
            });
        }
        else {
            $('.ul_date li').html("<li value=''>------- Select --------</li>");
            alert("tghhh") ;
        }
    });
</script>

PHP:

 <?php include("database/conn.php"); ?>
 <?php
 if(isset($_POST['m_id'])) {
 $sql = "select `id`,`date` from movie where 
`id`='".mysqli_real_escape_string($conn, $_POST['m_id'])."'";
$res = mysqli_query($conn, $sql);
//to test what value given
echo "<option value=''>".$_POST['m_id']."</option>";
if(mysqli_num_rows($res) > 0) {
    echo "<option value=''>------- Select --------</option>";
    while($row = mysqli_fetch_object($res)) {
 //            echo "<li data-value='" . $row->id . '"class="option" >' . $row->date . '</li>';

        echo "<li data-value='".$row->id."' class='option'>".$row->date."</li>";
    }
}
else{
    echo "<option value=''>No showing time</option>";
}
} else {
header('location: ./');
 }
 ?>




Aucun commentaire:

Enregistrer un commentaire