Hi,please help me.
I have a textbox called txtUsername, also have one called txtEmpID. what im looking for is a simple way for txtempid can get updated automattically based one what i type in txtusername.
Thank you

Dani AI

Generated

Short answer, then a safe fix.

If you only want to mirror what the user types, a client-side copy is the easiest (as suggested). If the EmpID must be looked up in the database (your getuser2.php approach), do a tiny AJAX lookup that returns just the ID (plain text or JSON) and write that into the EmpID input’s value instead of inserting HTML into a div. Use the input event for live typing, fetch the server response as text (or JSON), and assign it to the input’s .value. (developer.mozilla.org)

Example (client side — vanilla JS using Fetch and input):

document.getElementById('txtUsername').addEventListener('input', async (e) => {
  const name = e.target.value;
  if (!name) { document.getElementById('txtEmpID').value = ''; return; }

  try {
    const resp = await fetch('getuser2.php?q=' + encodeURIComponent(name));
    if (!resp.ok) throw new Error('HTTP ' + resp.status);
    const id = (await resp.text()).trim();
    document.getElementById('txtEmpID').value = id;
  } catch (err) {
    console.error(err);
  }
});

Server-side: return only the ID (no table markup) and use parameterized queries. Do not use the old mysql_* extension — use PDO or mysqli and prepared statements to avoid SQL injection. A minimal pattern with PDO:

<?php
header('Content-Type: text/plain; charset=utf-8');
if (empty($_GET['q'])) exit;
$name = $_GET['q'];

$pdo = new PDO('mysql:host=localhost;dbname=yourdb;charset=utf8mb4','dbuser','dbpass', [
  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);
$stmt = $pdo->prepare('SELECT ID FROM student WHERE name = ? LIMIT 1');
$stmt->execute([$name]);
echo $stmt->fetchColumn() ?: '';

Avoid returning HTML fragments from getuser2.php (that’s why your value ended up in the div). The old mysql_* functions were deprecated/removed; use PDO/mysqli and prepared statements instead. (php.net)

Quick troubleshooting: check the Network tab to confirm the server returns plain text or JSON (and an HTTP 200), trim whitespace before assigning, and make sure your input IDs match those used in the script. If you want instant mirroring without a server call, the simple client-side copy approach that and mentioned is perfectly fine.

Recommended Answers

All 3 Replies

So, doing this client side would be best so you dont have to use PHP on each postback to check/assign the values..

If you want simple, use jQuery...

here is an example of one way to do this...

<!DOCTYPE html>
<html>
<head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js">
 </script>

</head>
<body>
UserName: <input id="txtUsername" type="text" /><br/>
EmpID: <input id="txtEmpID" type="text" />

<script>
$('#txtUsername').keypress(function(){
   $('#txtEmpID').val($('#txtUsername').val())
});

</script>
</body>
</html>

jquery or javascript is a powerful tool to do that thing :)

Thank you for your response and answer.I appreciate it and I will try it.

For my code now, the ID appear when I typed the name,but I want the ID appear in textbox. Please help me.Here are my codes.

This is getuserform.php

  <html>
    <head>
    <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","getuser2.php?q="+str,true);
    xmlhttp.send();
    }
    </script>
    </head>
    <body>

    <form>
      <p>
        <input type = "text" name="name" id="name" size ="35" onChange="showUser(this.value,name.value)">
      </p>

    </form>
    <br>
    <div id="txtHint"><b>Person info will be listed here.</b></div>

    </body>
    </html>

This is getuser2.php

<?php


include("dbase.php");

 $q = $_GET['q']; 
 $query = "SELECT ID FROM student WHERE name = '".$q."'";

$result = mysql_query($query,$conn);



while($row = mysql_fetch_array($result)){

  echo "<tr>";

  echo "<td>" . $row['ID'] . "</td>";

  echo "</tr>";
  }



mysql_close($conn);
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.