0
votes

I have dropdown which when any option in it selected, another dropdown values should populate.

I need to check that first drop down value with some variable and based on that condition other drop down values populate based on it.

My first dropdown,

    <select id="mySelect" name="name">
      <option>Select any city</option>
      <?php foreach ($data as $c) { ?>
      <option><?php echo ($c[0]['value']); ?></option>
      <?php } ?>
    </select>

Am gettting the above dropdown selected value using jquery, but i need in php

jQuery("#mySelect").change(function () {
        ...
    });

For second dropdown,

<select>
......
if ($myPhpVar == $data[1][$j][$i]['c_name']){  ?>
    <option><?php echo $data[1][$j][$i]['a_name']; ?></option> 
    <?php }
.... ?>
</select>

here $myPhpVar should be first dropdown selected value, I need to compare it with some data.

How to get the first dropdown value and set it to $myPhpVar

UPDATE:

Managed drop down using jQuery:

jQuery("#mySelect").change(function () {
    var end = this.value;
    var firstDropVal = jQuery('#mySelect').val();
    <?php for($j=0; $j<count($data[1]); $j++ ){ 
        //print_r ($data[1][$j]);
        for($i=0; $i<count($data[1][$j]);$i++){
            if ($myPhpVar == $data[1][$j][$i]['city_name']){  
            echo $data[1][$j][$i]['area_name']; ?>
            jQuery('#dsad').append("<option><?php echo json_encode($data[1][$j][$i]['area_name']); ?></option> ")
            <?php
            }
        }
    } ?>
    console.log(end);
    console.log(firstDropVal);
});

Yet not getting dropdown values for the second dropdown.

1
You will have to send an AJAX-request if you want to modify the second dropdown after the user selected an option in the first one. - Tobias F.
you can not do that directly using php, instead you will need 1) use full javascript to handle this, or 2) submit a new http request to make php take effects or 3) using ajax - hassan
how can I handle this using javascript? @hassan - Mann
within your change callback , do your logic to select or fill the other select drop-down menu - hassan
@Mann read this article about passing variables coderslexicon.com/… There's also an SO question on this stackoverflow.com/questions/1917576/… - Rachel Gallen

1 Answers

0
votes

You have to write a web service. Create a new page that will be called when there is a change in first dropdown (use AJAX). Pass the selected value to that page send a response in JSON (may be). Set that response to the other component.

jQuery("#mySelect").change(function () {
   var val = jQuery(this).val();
   jQuery.ajax({
          url: 'your_new_page.php',
          data: 'dropvalue='+val,
          processData: false,
          type: 'GET',
          dataType:'json',
          success: function(response){
             //load json data from server use it   
          },
          error: function(jqXHR, textStatus, errorThrown) {
                 //there is an error
          }
    });
});

your_new_page.php

if(isset($_GET['dropvalue'])) {
    $myPhpVar = $_GET['dropvalue'];
    //do what you want and return the response
} else {
   //value not submitted, send an error
}