0

Not sure I have worded the title correctly, so apologies if it's confusing!

Here is my problem

I have a dynamic select that is populated via data on an ajax call. I have a function that runs with a "change" of the first select. This works perfectly fine if the user manually selects the first dropdown items, however I am stuck on how to get this to run properly when they are editing.

This is what I want to achieve:

  1. Load the page based on the ProdID and populate other form fields (this is happening now, so don't need help with that ;-))
  2. Check what's pulled in from getData.php
  3. Populate the CatID select and the mark the current selection as "selected".
  4. If the user changes either of the select boxes, the function runs as it does currently.

Here is my function:

$(function() {  
  $("#topcatid").change(function() {
    $("#catid").load("getData.php?choice=" + $("#topcatid").val());
  });
});

Here is my html:

<select name="topid" id="topid" data-placeholder="Choose a category...">
  <option value="" <?php if (!(strcmp( "", ($rsProd->getColumnVal("topcatid"))))) {echo "selected=\"selected\"";} ?>>Select</option>
  <?php while(!$rsCat->atEnd()) {?>
  <option value="<?php echo($rsCats->getColumnVal(" topcatid ")); ?>"<?php if (!(strcmp($rsCat->getColumnVal("topid"), ($rsProd->getColumnVal("topid"))))) {echo "selected=\"selected\"";} ?>>
    <?php echo($rsCat->getColumnVal("catName")); ?>
  </option>
  <?php $rsCat->moveNext(); } $rsCat->moveFirst(); ?>
</select>

<select name="catid" required class="chzn-select" id="catid" style="width:350px" tabindex="2" data-placeholder="Choose a sub-category...">
  <option value="">Select from above...</option>
</select>

If the user manually changes the TopID select, the function runs, goes and gets the data and populates "catid". However this is on a page that is an edit/update page so I need the function to run as per my ideal scenario above.

Your comments and code edits would be very much appreciated.

Thanks Nick

5
  • Set another bind on #catid.change()? Commented Jan 13, 2016 at 16:23
  • Nice specification. Please read What topics can I ask about and How to ask a good question And the perfect question SO is not a free coding service You have to show that you have made some effort to solve your own problem. Commented Jan 13, 2016 at 16:30
  • "Your comments and code edits would be very much appreciated." - huh? You want us to edit what exactly? Commented Jan 13, 2016 at 16:30
  • @RiggsFolly that isn't a specification, it sounds more like a "request". Commented Jan 13, 2016 at 16:31
  • 1
    Whoa, hang on...this was never a request for a coding service neither was it a specification! The code in place is what I have created myself and have made an effort to resolve it. I was giving as much info as I could so everyone would know what I was trying to achieve rather than asking someone to code it for me. "...code edits..." was in relation to the fact that people will supply their code ideas to get solve the problem. That also means that they could take my code and edit it to give a response...not do it for me! Commented Jan 13, 2016 at 20:42

1 Answer 1

2

For me you should do this in a calback function that will be executed just after the load.

$("#topcatid").change(function() {
    $("#catid").load("getData.php?choice=" + $("#topcatid").val(), function() {
        // Manage the old and the new catid
    });
});

You can also use the jquery.ajax function that will give you more options for functions before and after the query:

$.ajax({
  url: ""getData.php?choice=" + $("#topcatid").val()",
  beforeSend: function( xhr ) {
    // Do something
  }
})
  .done(function( data ) {
    // Do something with data
  });
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.