Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Ajax - php - combos

    Hi Folks,

    my issue is both a php and a javascript (AJAX) one so please forgive it being here - its one or the other. I have two combo boxes one dependant on the other, populated by AJAX but when I submit the form the secondary combo is not being registered as a php $_POST['var'] in the following action page. The secondary combo is contained and altered using divs and inner.HTML. Is this an expected behaviour or is my issue else where?

    Please help - im near to tears

    :-)

  • #2
    Regular Coder
    Join Date
    Feb 2009
    Location
    NJ, USA
    Posts
    476
    Thanks
    2
    Thanked 70 Times in 69 Posts
    Couple of ideas:
    1) If you're editing a dropdown (select element) with innerHTML, are you giving it a name attribute when you re-create it?
    2) Is the dropdown within the <form> and </form> tags?
    3) Try editing the options of the dropdown through the script interface to it instead of messing with / recreating it via innerHTML. Here's an example, assuming an array is returned by the ajax call, and each item of the array has properties 'text' and 'value':
    Code:
    function loadDropdown( responseArray ) {
      var dropdown = document.getElementById( 'myDropdown' );
      dropdown.options.length = 0;  // Start by removing all dropdown options
    
      // Load the new options into the dropdown
      for( var i = 0, i < responseArray.length; i++ ) {
        dropdown.options[ i ] = new Option( responseArray[ i ].text, responseArray[ i ].value );
      }
    }
    If those don't help, post your JavaScript and html code and I can try to help you from there.

  • #3
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool

    Thanks for that - I solved the problem in the end.

    What was happening was because my dropdown was changing on the basis of getElementById of the div tag around the "<select ...</select>" My $_POST variable was not being populated.

    My workaround was to pass the document.getElementById("var1").value of the select box to some AJAX .....

    which sent the value as a querysting to another php file ....

    which then set the $_GET variable as a session variable to be used later on


    Can I thank myself on this forum?

  • #4
    Regular Coder
    Join Date
    Feb 2009
    Location
    NJ, USA
    Posts
    476
    Thanks
    2
    Thanked 70 Times in 69 Posts
    Oh yikes, that sounds like a seriously hacked workaround for something that shouldn't be an issue. Not to mention the creation of confusing, and down the road, unmaintainable code. Then of course, you have the problem of using a session variable (a shared variable) that could accidentally be overwritten by another page before your original form submission page gets to use it. Talk about a nightmare.

    Really, the value from that dropdown should be getting submitted by the form, and therefore be available in the $_POST superglobal when your php page runs. The fact that it's not available is definitely an issue with your javascript code and the method that you are using to repopulate that dropdown.

    Again, if you want, post your html/javascript code and I'll take a look as to why that dropdown is not being submitted with the form.

    P.S. No you cannot thank yourself in this forum, nor should you for such a seriously hacked approach


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •