Get value from dropdown list for use in MySQL query

So, I had a problem getting the value from the dropdown in HTML to a variable so that I can execute the mysql query. It will be a little difficult to explain, but I will do my best. (Feel free to correct my English or my expressions to make the question more specific and understandable).

So, I have this dropdown that gets its values ​​from mysql query.

<td>Designação atual :</td> <td><select name="desig_act" id="desig_act">
<?php
while ($row2 = mysql_fetch_assoc($result4)) {
echo "<option size=30 value=".$row2['new_name_freg'].">".$row2["new_name_freg"]."</option>";
}   
?>
</select>

This "connects" to this request:

$sql4 = ("SELECT DISTINCT new_name_freg FROM freguesias WHERE codg_cc = '$pesq'");
$result4 = mysql_query($sql4, $link);

This query will populate a drop-down list with values. What I want to do is fill out another drop down list. For examples, I select a list of countries, and when I select a country, it should display all its cities in another drop-down list.

I was looking for guys. Believe me.

P.s: , , , , , , . , . , , .

+4
2

1: PHP script,

, $_GET.

2: json jquery

PHP, , , select.

<?php
//Step 1 - The posted ajax data that will return our json request.
if(isset($_GET['fetchrow'])) //Is our ajax request called on page load? If yes, go to this code block
{
    //Other stuff like DB connection
    $pesq = mysql_escape_string($_GET['fetchrow']); //Put our variable as the variable sent through ajax
    $sql4 = ("SELECT DISTINCT new_name_freg FROM freguesias WHERE codg_cc = '$pesq'"); //Run our query
    $result4 = mysql_query($sql4, $link); //Please change from mysql_* to mysqli
    $data = array(); //The array in which the data is in
    while($row = mysql_fetch_assoc($result4)) //Look through all rows
    {
        array_push($data, $row); //Put the data into the array
    }
    echo json_encode($data); //Send all the data to our ajax request in json format.
    die; //Don't show any more of the page if ajax request. 
}

?>

<html>
    <head>
        <script type='application/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.min.js'></script> <!--Include jquery -->
        <script>
        //Step #2:
        //The jquery script calls ajax request on change of the first select
        $( "#desig_act" ).change(function() {
            $.getJSON('thisfilename.php', {fetchrow:$("#desig_act").val()}, function(data){ //Get the json data from the script above
                var html = '';
                var len = data.length;
                for (var i = 0; i< len; i++) { //Loop through all results
                    html += '<option value="' + data[i].new_name_freg + '">' + data[i].new_name_freg + '</option>'; // Add data to string for each row
                }
                $('#otherselect').html(html); //Add data to the select.
            });
        });

        </script>
    </head>
    <body>
    <!-- Your html code -->
    <td>Designação atual :</td> <td><select name="desig_act" id="desig_act">
    <?php
    while ($row2 = mysql_fetch_assoc($result4)) {
    echo "<option size=30 value=".$row2['new_name_freg'].">".$row2["new_name_freg"]."</option>";
    }   
    ?>
    </select>
    </td>
    <!-- The new select -->
    <select name='otherselect' id='otherselect'>
    </select>
    <!-- Rest of html code -->
    </body>
</html>
+2

ajax jquery.

<!-- index.php -->
<select name="desig_act" id="desig_act">
<?php while ($row2 = mysql_fetch_assoc($result4)): ?>
   <option value="<?=$row2['new_name_freg']?>"> 
      <?=$row2["new_name_freg"]?>
   </option>
<?php endwhile; ?>
</select>
<!-- select tag for countries -->
<select name="country" id="country"></select>

script, json

<?php //ajax-countries.php
$link = mysql_connect(); // connect as usual
$query = ("SELECT * FROM countries");
$result = mysql_query($query, $link);
$json = array();
while ($row = mysql_fetch_assoc($result)) $json[] = $row;
echo json_encode($json);
?>

- script :

// script.js
$("#desig_act").change(function(){
  $.getJSON( "ajax-countries.php", function( data ) {
    $.each( data, function(key, val) {
      $("#desig_act").append("<option val='" + key + "'>" + val + "</option>");
    });
});

,

+3

Source: https://habr.com/ru/post/1622067/


All Articles