0

How can I pass the variable from php to my javascript so it a unique number autoincrementing. I think have the wrong format. Any help with would be appreciated. I am trying to get "txtHint" + autoincrementing $i to go to javasript as unique id. So it will work in the loop. How can I pass it so java can translate it correctly with php as txtHint+$i. I cannot get it to bring any results. Any help much appreciated..

    <script type="text/javascript">
     function showCustomer(str)
      {
       var xmlhttp;    
    if (str=="")
       {
     document.getElementById("txtHint"+$i).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"+$i).innerHTML=xmlhttp.responseText;
   }
   }
   xmlhttp.open("GET","somepage.php?q="+str,true);
   xmlhttp.send();
      }
   </script>


$result=mysql_query("SELECT * from items");


echo "<table  >
</tr>";


for ($i = 0; $Update = mysql_fetch_assoc($result); ++$i)
{

   <td><input type='text' size='5' id='cno' name='cno' value='{$Update['line']}'  
       readonly/></td>


   <td><input type='text'size='5' name='con' value='{$Update['con']}' readonly/>

     <td><select name='Description' id= 'cfname' onchange='showCustomer(this.value);

  <td width='20'><div id='txtHint".$i."' ></div></span></td>
  • Please don't use `mysql_*` functions anymore, they are deprecated. See [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) for details. Instead you should learn about [prepared statements](http://bobby-tables.com/php.html) and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you can't decide which, [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you. If you pick PDO, [here is a good tutorial](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers). – Marcel Korpel Nov 25 '13 at 14:50
  • You should use jQuery instead of XMLhttprequest – Charles Nov 25 '13 at 14:50
  • @goddfree jQuery uses `XMLHttpRequest`s internally, no need for jQuery here. Though the code is not properly indented and will not work if statuscode ≠ 200. And the OP should use [`encodeURIComponent`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) to properly encode query string parameters (variable names as well as values). – Marcel Korpel Nov 25 '13 at 14:52

0 Answers0