0

I am using two dropdowns in a form for searching. The second dropdown value will change based on the selection of the first one. The ajax request goes but somehow not returning any value from the php script. Any suggestions for change in code?

search.php

<select name="shop_name" id="shop_name">
                  <option value="blank">Select one</option>
                  <?php
                  $searchItem = new dbhandler();
                  $shopData = $searchItem->all_data('food_shops');
                  $i = 0;
                  foreach($shopData as $sd) {
                    ?>
                    <option value="<?php echo $shopData[$i]->r_id; ?>"><?php echo $shopData[$i]->name; ?></option>
                    <?php $i++; } ?>
 </select>

script.js

<script type="text/javascript">
$(document).ready(function()
{
  $("#shop_name").change(function()
{
  var id=$(this).val();
  var dataString = 'id=' + id;
$.ajax
({
  type: "GET",
  url: "dynamic_select.php",
  data: dataString,
  success: function(data)
  {
    $("#location").html(data);
  }
  });

  });

  });

dynamic_select.php

<?php

if($_GET['id']) {

  $crs = $_GET['id'];
?>

  <select name="location" id="location">
<?php
  $searchItem = new dbhandler();
  $searchItem->find('food_shops' , 'r_id', $crs);
  $data = $searchItem->data();
  $j = 0;
  foreach($data as $d) {
    ?>
    <option value="<?php echo $data[$j]->address; ?>"><?php echo $data[$j]->address; ?></option>
  </select>
<?php $j++; } } ?>

The all_data() queries SELECT * FROM table_name and find() queries SELECT * FROM table_name WHERE r_id = $crs

8
  • 1
    You don't have an url option for your ajax call. Commented Nov 18, 2016 at 6:28
  • In your ajax you need to add url and pass the id parameter for getting related values. Commented Nov 18, 2016 at 6:30
  • ^^^^ + $("#location").replaceWith(data); Commented Nov 18, 2016 at 6:30
  • @A.OzanEkici gave still not working Commented Nov 18, 2016 at 6:33
  • So @AbhijitBorkakoty can you update your question with the ajax url option, so we can look at it. Commented Nov 18, 2016 at 6:35

2 Answers 2

1

you can check your response with inspector looks like following.In your page you just check that what you are getting in your response.you just let open the inspector network>xhr and then select value from your first dropdown. on selection of dropdown it will call the dynamic_select.php and show to inspector and just select response you will be able to see the response there.

enter image description here

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

2 Comments

what was the response for dynamic_select.php?id=7811178?
Thanks @Alfiza for your help...Found the solution. I was not including the autoload file for the classes. Included that and made sure the correct url goes to the php script. Thanks
0
<script type="text/javascript">
    $(document).ready(function()
    {
      $("#shop_name").change(function()
    {
      var id=$(this).val();
      var dataString = 'id='+ id;

    $.ajax
    ({
      type: "GET",
      data: dataString,
      cache: false,
      url : "dynamic_select.php",
      success: function(data)
      {
        $("#location").html(data);
      }
      });

      });

      });
  </script>

add the url with id in your ajax script ,change the dynamic_select.php path if required.

1 Comment

@Alfizamalek You are passing data two times. One at data option and one at end of the url option.

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.