POST & GET - Passing a string value from javascipt to php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • siddhanta
    New Member
    • Oct 2009
    • 33

    POST & GET - Passing a string value from javascipt to php

    hi,
    i have two drop down list.
    First one is populated from database and its working fine.
    Second one will also be populated from database but as per the value selected from the first drop-down list.

    Code:
    <head>
    <script type="text/javascript">
    function xyz_list()
    {
    
    // xyz_list is the id of my first drop-down list
      var xyz_list=document.getElementById("mob_list");
      brand=xyz_list.options[mob_list.selectedIndex].text;
     if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("xyz").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","XYZ.php?brand=" + brand,true);
    xmlhttp.send();
    }
    </script>
    </head>
    
    <body>
    <form>
    <select id="xyz" onfocus="xyz_list()">
    <OPTION VALUE=All>All 
    <?php echo $options?> 
    </SELECT> 
    </form>
    </body>
    Below is the xyz.php script

    Code:
    <?php 
    $brand=$_GET['brand']; 
    //alert($brand); 
    
     //below is just inserting a blank value in drop list 
     $options_mobile.="<OPTION VALUE=abc>".$brand.'</option>'; 
    $con = mysql_connect('localhost', 'root', ''); 
    if (!$con) 
      { 
      die('Could not connect: ' . mysql_error()); 
      } 
    
    mysql_select_db("mobile1", $con); 
    
    
    $sql="SELECT xyz FROM abc where pqr= '$brand'"; 
    
    $result = mysql_query($sql); 
    $options.="<OPTION VALUE=\"$result\">".$result.'</option>'; 
    while($row = mysql_fetch_array($result)) 
      { 
     // $xyz=$row["xyz"];  
      $xyz=$row["xyz"]; 
       $options.="<OPTION VALUE=\"$xyz\">".$xyz.'</option>'; 
     } 
    mysql_close($con); 
    ?>

    I am new to both of the languages, but i know wat my code wants to do. i googled a lot to find the error, But i cant figure out where i am wrong.
    Any help will be highly appreciated.
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    Where is the error and what error?

    Give us the error detail

    Comment

    • siddhanta
      New Member
      • Oct 2009
      • 33

      #3
      sorry i missed it.
      Error is, instead of expected value, only a blank value is inserted in my second drop list.
      I have checked the 'brand' value using alert box in jscript and its working fine.
      also i have included some dummy output like $options_mobile .="<OPTION VALUE>Choose11" .'</option>'; in php file. its too working fine.

      thanks..

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        two things, line #6 (php) throws a warning because you're appending a string to an undefined variable and on line #19 you append a resource to a string (which should give some unexpected output)

        Comment

        • AutumnsDecay
          New Member
          • Mar 2008
          • 170

          #5
          I see that you have "<OPTION VALUE=ALL>All", but you never close that option. Try:

          <option value='All'>All </option>

          Also, for standards'-sake, don't use uppercase letters in elements and element attributes.

          <OPTION></OPTION> will still work, but it's best practice to do it properly, all lower-case: <option></option>.

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            actually, the standard for HTML 4 is uppercase element names and lowercase attribute names. but personally, I'd stick to all being lowercase (easier to read).

            besides that the closing option tag is optional, but it's always a good idea to close elements properly (that can be closed).

            Comment

            • siddhanta
              New Member
              • Oct 2009
              • 33

              #7
              hi
              thanks for the replies.

              I tried above things still the same issue. A blank output.
              Is there any way to check what is the 'brand' value in php. I mean something like alert box in jscript. So that i can figure out if the 'brand' value is actually passed from jscript to php.

              Thanks

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                that's called var_dump()

                Comment

                Working...