0

Hi i have this listbox in php populated using mysql

<?php

mysqli_query($con,"set character_set_server='utf8'");
mysqli_query($con,"set names 'utf8'");
$r=mysqli_query($con,"select id_livestream,name_match from Livestream");
?>
<br>تشكيلة مباراة :<select name=livestream size=1>
<?php 
while ($d=mysqli_fetch_array($r,MYSQLI_BOTH))

    {
        ?>

        <option value="<?=$d[0];?>"><?=$d[1];?></option>
        <?php 
    }
    ?>

</select>

How do i create and populate from mysql another listbox which based on the selected value from the first listbox ??

Any suggestions would be appreciated

5
  • 1
    Ajax is required to populate one from another without a submit Commented May 8, 2017 at 15:36
  • Thx a lot @clearshot66 Commented May 8, 2017 at 15:44
  • Is there anyway to do it with only Javascript ? Commented May 8, 2017 at 15:46
  • Is your second list box values are fixed values or that list varies depends on selection value from the first list? Commented May 8, 2017 at 16:11
  • @MahdiHraybi Two ways. You can make multiple arrays linking to one of your choices from the first , then display that array of choices in the second list box, with only javascript. OR Use ajax with jquery if the options are stored in a database. If it's DB interaction it can only be done via Ajax, if they're static lists you can program them statically in array Commented May 8, 2017 at 17:06

1 Answer 1

1

Assuming your second dropdwon data will be populated from database table and there is a foreign key relation to the livestream table.

I have tried using Ajax, I got below. please refer the attachment.Let me know if it works for you and accept my answer.

enter image description here

DisplayDropdown.php: This is the main page where you have a dropdown to select.

<?php
$con=mysqli_connect("localhost","UserName","","DatabaseName"); // Database connection


mysqli_query($con,"set character_set_server='utf8'");
mysqli_query($con,"set names 'utf8'");
$r=mysqli_query($con,"select id_livestream,name_match from Livestream");
?>

<html>
<head>
<script>
function getSubStream(str) {
    if (str == "") {
        document.getElementById("DivIDSubCategory").innerHTML = "";
        return;
    } else { 
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                document.getElementById("DivIDSubCategory").innerHTML = this.responseText;
            }
        };
        xmlhttp.open("GET","GetCategory.php?q="+str,true);
        xmlhttp.send();
    }
}


</script>
</head>
<body>

<form>

  Select Live Stream: <select name=livestream size=1 onchange="getSubStream(this.value)">
  <option value="select">Select</option>
<?php 
while ($d=mysqli_fetch_array($r,MYSQLI_BOTH))

    {
        ?>

        <option value="<?=$d[0];?>"><?=$d[1];?></option>
        <?php 
    }
    ?>

</select>
<br/>
<br/>
  <div id="DivIDSubCategory"></div>

</form>


</body>
</html>

GetCategory.php: Here we are creating a new dropdown based on the first dropdown selection

<?php
$con=mysqli_connect("localhost","UserName","","DatabaseName"); // Database connection
?>

<!DOCTYPE html>
<html>
<body>

<?php
$q = intval($_GET['q']);


mysqli_query($con,"set character_set_server='utf8'");
mysqli_query($con,"set names 'utf8'");


$sql = "SELECT subID,SubStreamName FROM substreams where livestreamID = '".$q."'";
$r=mysqli_query($con,$sql);


if(mysqli_num_rows($r) > 0) {
 echo "Select Sub Stream: <select name=substream><option value=Select>Select</option>";  

while ($d=mysqli_fetch_array($r,MYSQLI_BOTH))

    {
        ?>

        <option value="<?=$d[0];?>"><?=$d[1];?></option>
        <?php 
    }

echo "</select>";
}



mysqli_close($con);
  ?>
</body>
</html>

Some other resources:

Using PHP & MySQL to populate dropdown

Populate another select dropdown from database based on dropdown selection

How can i populate a dropdown list by selecting the value from another dropdown list

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.