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 5 of 5
  1. #1
    New Coder
    Join Date
    Sep 2010
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Ajax/Php auto fill form

    Hi, I originally posted this in the php section but then as my script has evolved and i was told i needed ajax I have the php side fully working and the whole thing working in IE but not Firefox.

    Im trying to achieve a form in which the user will use a select box and the choice will determine what some text fields are filled with allowing the user to still edit the textfield sif necersary.

    I have the script fully working in IE as stated but not in Firefox. Below is my code:

    Form Page
    PHP Code:
    <html>
    <
    head>
    <
    script type="text/javascript">
    function 
    showSupplier(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==&& xmlhttp.status==200)
        {
        
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","getsupplier.php?q="+str,true);
    xmlhttp.send();
    }
    </script>
    </head>
    <body>

    <form>
    <select name="users" onChange="showSupplier(this.value)">
    <option value="">Select a person:</option>
    <option value="1">Supplier1</option>
    <option value="2">Supplier2</option>
    <option value="3">Supplier3</option>
    <option value="4">Supplier4</option>
    </select>
    </form>
    <br />
    <div id="txtHint"><b>Supplier info here</b></div>

    </body>
    </html> 
    getsupplier.php:
    PHP Code:
    <?php
    $q
    =$_GET["q"];

    $con mysql_connect('localhost''''');
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }

    mysql_select_db(""$con);

    $sql="SELECT * FROM suppliers WHERE Supplier_ID = '".$q."'";

    $result mysql_query($sql);

    echo 
    "<table border='1'>
    <tr>
    <th>Supplier</th>
    <th>Contact</th>
    <th>Telephone</th>
    <th>Email</th>
    </tr>"
    ;

    while(
    $row mysql_fetch_array($result))
      {
      echo 
    "<tr>";
      echo 
    "<td>" $row['Supplier'] . "</td>";
      echo 
    "<td>" $row['Contact'] . "</td>";
      echo 
    "<td>" $row['Telephone'] . "</td>";
      echo 
    "<td>" $row['Email'] . "</td>";
      echo 
    "</tr>";
      }
    echo 
    "</table>";

    mysql_close($con);
    ?>

  • #2
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    I think you need:
    Code:
    onChange="showSupplier(this.options[this.selectedIndex].value)"

  • #3
    New Coder
    Join Date
    Sep 2010
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks for your help.

    I have just tried your suggestion but unfortunately it is the same result. Works in IE but not Firefox.

    Reading around I believe it has something to do with the following part:
    PHP Code:
    <script type="text/javascript">
    function 
    showSupplier(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==&& xmlhttp.status==200)
        {
        
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","getsupplier.php?q="+str,true);
    xmlhttp.send();
    }
    </script> 

  • #4
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    Ok. I know this might sound like a stupid question - the fact that you're fetching a PHP page at least suggests otherwise - but you're not running this off a local machine are you? It'll need to be on a server to return a HTTP status code.

  • #5
    New Coder
    Join Date
    Sep 2010
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts
    no im not running this on my local machine it is on the server.


  •  

    Posting Permissions

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