简体   繁体   中英

Variable was undefined when I get $q?

I working now with this code below, I want to display rfq list depends on rfq #. But when I choose, the $q was undefined.

Dropdown.php

   <?php
    $con = mysql_connect("localhost","root","");
    $db = mysql_select_db("app",$con);
    $get=mysql_query("SELECT rfq FROM procurement GROUP BY rfq ORDER BY rfq");
    $option = '';
    while($rows = mysql_fetch_assoc($get))
   {
       $option .= '<option value = "'.$rows['rfq'].'">'.$rows['rfq'].'</option>';
   }    
   ?>
   <form>
       <select name="users" onchange="showUser()">
             <option value="ALL">ALL</option>
            <?php echo $option; ?>
       </select>
   </form>
   <br>
   <div id="txtHint"><b>Person info will be listed here.</b></div>

getuser.php

    <?php
    include('connect.php');
    $q=$_GET["q"];
    $sql="SELECT rfq FROM procurement WHERE rfq='".$q."'";
    $result = mysql_query($sql);
    echo "<table border='1'>
    <tr>
    <th>MOD</th>
    <th>RFQ #</th>
    </tr>";
      while($row = mysql_fetch_array($result))
      {
           echo "<tr>";
           echo "<td>".$row['mode_of_procurement']."</td>";
           echo "<td>".$row['rfq']."</td>";
           echo "</tr>";
      }
      echo "</table>";
      echo $q;
      mysql_close();
  ?>

But the value of $q didn't display, when I echo the $q the value is UNDEFINED. Why is that?

And here's the script

<script>
function showUser(str)
{
if (str=="")
  {
     document.getElementById("txtHint").innerHTML="";
     return;
  }
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("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","getuser.php?q="+str,true);
  xmlhttp.send();
}
</script>

在此处输入图片说明

Pass this.value into your function call

        <select name="users" onchange="showUser(this.value)">
        <option value="ALL">ALL</option>
        <?php echo $option; ?>
        </select>

Newly Updated:

I think you want link this

    <html>
<script type="text/javascript">
    function show() {
        var e = document.getElementById('myOption');
        var str = e.options[e.selectedIndex].text;
        alert(str);

        if(str=='All')
        {
            var x = document.getElementById("myOption");
            var txt = "";
            var i;
            for (i = 1; i < x.length; i++) {
                txt = txt + "," + x.options[i].text;
            }

        }
        else{
            txt=str;
        }

        alert(txt);


    }   
</script>



<body onload="show();"> 
<form name="myForm">
<select name="myOption" id="myOption" onChange="show()">
<option value="All" >All</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
</select>
</form>

</body>
</html>

Fiddle DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM