Can't figure out what's wrong with my variable passing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MelindaM
    New Member
    • Nov 2009
    • 6

    Can't figure out what's wrong with my variable passing

    Hi guys, I'm new to PHP and AJAX/JS so I apologize if this is an incredibly obvious error.

    I'm in the process of creating multiple listboxes that are chained together for an advanced search form. I have everything working for the ajax and the issue I'm running into is when I try to pass a PHP variable to a JS function. I've searched multiple forums and read multiple posts about this issue but cannot figure out what's going on. I know that everything else is working because if I pass in something besides my PHP variable things come out fine.

    The method is called whenever the selection changes:
    <select multiple size=5 name="Package" style="width: 15em;" onchange='setPa ckage($partType , this.value)'>

    It's the "$partType" that I'm having issues with. I've tried passing it about a dozen different ways with no luck and I'm not sure how to debug the issue. The function is pretty basic:
    function setPackage(str, blah)
    {
    ....
    }

    Any advice would be appreciated.

    -Melinda
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    try
    Code:
    setPackage(<?php echo $partType; ?>, this.value)

    Comment

    • MelindaM
      New Member
      • Nov 2009
      • 6

      #3
      Thanks for the reply. I tried what you suggested and nothing happens when I select something in the listbox. If I change the code to pass in "this.value " for both params it works just fine so I'm not missing anything anywhere else that I know of.

      Here's what the javascript looks like:
      Code:
      function setPackage(str, blah)
      {
      xmlhttp=GetXmlHttpObject();
      if (xmlhttp==null)
        {
        alert ("Browser does not support HTTP Request");
        return;
        }
      var url="getVendor.php?";
      url=url+"package="+str;
      url=url+"&partType="+blah;
      url=url+"&sid="+Math.random();
      xmlhttp.onreadystatechange=stateChanged2;
      xmlhttp.open("GET",url,true);
      xmlhttp.send(null);
      }
      
      function stateChanged2()
      {
      if (xmlhttp.readyState==4)
      {
      document.getElementById("vendorSelect").innerHTML=xmlhttp.responseText;
      }
      }
      and the PHP for it:
      Code:
      <?php require_once('../Connections/dev/admin.php'); ?>
      <?php require_once('../Connections/dev/phtdeelib.php'); ?>
      <?php
      if(isset($_GET['partType'])) {
        echo "PartType = " . $_GET['partType'] . "<br>";
      }
      
      if(isset($_GET['package'])) {
        echo "Package = " . $_GET['package'] . "<br>";
      }
      
      ?>
      Is there a way to debug it and see what's wrong with how the variable is being passed?

      Thanks in advance!
      -Melinda

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        Originally posted by MelindaM
        Is there a way to debug it and see what's wrong with how the variable is being passed?
        If you use Firefox and the Firebug Add-on, you can check the GET/POST requests in the network tab.

        Comment

        • MelindaM
          New Member
          • Nov 2009
          • 6

          #5
          I just figured out that I can pass "this.form" instead and then in the method parse out all the values that I need, such as:

          Code:
          var val1=form.PartType.options[form.PartType.options.selectedIndex].value;
          var val2=form.Package.options[form.Package.options.selectedIndex].value;
          It only requires passing the one form variable instead of passing multiple values which will be better if this form gets very complex. Hopefully this is a good solution.

          I'll probably still try to figure out what's going on the other way using Firebug because I want to understand why the original way won't work.

          Thanks for the quick responses guys!
          -Melinda

          Comment

          Working...