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 2 of 2
  1. #1
    New Coder
    Join Date
    Apr 2011
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Lightbulb carry data into a text box

    hi..i want to ask how can i carry a data from a database into a text box after using some php coding
    first, i have a run.php file
    Code:
    <html>
    <head>
    <script type="text/javascript">
    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>
    </head>
    <body>
    
    <?php
    $host="127.0.0.1"; // Host name 
    $username="root"; // Mysql username 
    $password=""; // Mysql password 
    $db_name="ofcccv3"; // Database name 
    
    // Connect to server and select databse.
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");
    
    $query = "SELECT * FROM car";
    	 
    $result = mysql_query($query) or die(mysql_error());
    
    ?>
    
    
    <form>
    
    <select name="users" onchange="showUser(this.value)">
    
    				<?php 
    				
    				
    				// Print out the contents of each row into a table 
    				while($row = mysql_fetch_array($result))
     				 {
     					 echo "<option value=".$row['id'].">" . $row['plat'] . "</option>";
     				 }
    	
    				$result = mysql_query($query) or die(mysql_error());
    				
    				?>
    				</select>
    
    </form>
    
    
    
    <br />
    <div id="txtHint"><b>Person info will be listed here.</b></div>
    <p></p>
    <input name="engineCC" type="text" >
    </body>
    </html>
    and also getuser.php file
    Code:
    <?php
    $q=$_GET["q"];
    
    $con = mysql_connect('127.0.0.1', 'root', '');
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("ofcccv3", $con);
    
    $sql="SELECT * FROM car WHERE id = '".$q."'";
    
    $result = mysql_query($sql);
    
    echo "<table border='1'>
    <tr>
    <th>CAR PLAT NUMBER</th>
    <th>CAR MODEL</th>
    <th>CAR CC</th>
    <th>CAR TRANSMISSION</th>
    </tr>";
    
    while($row = mysql_fetch_array($result))
      {
      echo "<tr>";
      echo "<td>" . $row['plat'] . "</td>";
      echo "<td>" . $row['type'] . "</td>";
      echo "<td>" . $row['cc'] . "</td>";
      echo "<td>" . $row['trans'] . "</td>";
      echo "</tr>";
      }
    echo "</table>";
    
    mysql_close($con);
    ?>
    ::after i run the run.php file.it is success for me to retrieve the data from database using a drop down list..next, what i need to do to make sure that the echo "<td>" . $row['cc'] . "</td>"; value also appear in the 'engineCC text box'
    Thanks you for your time

  • #2
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    What errors are you getting?

    You can debug and check what is going on in getuser.php by using echo statements and then displaying the responsetext in an alert().

    For example, say you want to see what the sql query being run is:

    Then in your getuser.php you could do something like this

    Code:
    $sql="SELECT * FROM car WHERE id = '".$q."'";
    echo $sql; die();
    and then back in your ajax function

    Code:
    if (xmlhttp.readyState==4 && xmlhttp.status==200)     {     
        //document.getElementById("txtHint").innerHTML=xmlhttp.responseText;     
        alert(xmlhttp.responseText);
    }
    and the sql query you are about to run should appear in the alert.

    The same technique can be used to echo out other variables' values in your php script as well.
    Last edited by bullant; 05-13-2011 at 09:56 AM.


  •  

    Posting Permissions

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