0

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.

Mann
  • 576
  • 1
  • 9
  • 33
  • 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. May 16 '17 at 07:59
  • 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 May 16 '17 at 08:00
  • how can I handle this using javascript? @hassan – Mann May 16 '17 at 08:01
  • within your `change` callback , do your logic to select or fill the other select drop-down menu – hassan May 16 '17 at 08:03
  • @Mann read this article about passing variables http://www.coderslexicon.com/the-basics-of-passing-values-from-javascript-to-php-and-back/ There's also an SO question on this http://stackoverflow.com/questions/1917576/how-to-pass-javascript-variables-to-php – Rachel Gallen May 16 '17 at 08:04
  • but the values are based on PHP array, do I need to access PHP array in javascript. And compare javascript variable value with PHP variable value. Can you provide me a snippet on it. – Mann May 16 '17 at 08:05
  • to get values in jvasript from php var val = ; to get values from array i recomend var val = ; – Jose Marques May 16 '17 at 08:12
  • @Mann i didn't see your reply to your comment until just now, and i only saw it because I happened to re-visit this question. In future, if you are replying to a comment, tag the user by putting the '@' symbol before their user name like I have done here. So i'm '@RachelGallen' for instance.. then the message will go to my inbox. Are you still having difficulty? If so, leave a comment. Thanks – Rachel Gallen May 22 '17 at 09:10

1 Answers1

0

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
}
Shahrzad
  • 1,062
  • 8
  • 26
  • Hi, I have arrays of both dropdowns on same page. So is there any way to do it using AJAX but on same page? Please advise. – K Ahir Jul 06 '20 at 16:38