hello, I have some problem with this code.

Jacasript used in AJAX:

var xmlHttp; 
function showUser(str) { 
if (xmlHttp==null) { 
alert ("Browser does not support HTTP Request"); return; 
var url="getuser.php"; 
xmlHttp.open("GET",url,true); xmlHttp.send(null); } 

function stateChanged() { 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {     document.getElementById("txtHint").innerHTML=xmlHttp.responseText; } } 

function GetXmlHttpObject() { 
var xmlHttp=null; 
try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } 
catch (e) { //Internet Explorer 
try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } 
catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } 
return xmlHttp; }

The PHP page:

<?php $q=$_GET["q"]; 
$con = mysql_connect('localhost', 'peter', 'abc123'); 
if (!$con) { die('Could not connect: ' . mysql_error()); 
mysql_select_db("ajax_demo", $con); 

$sql="SELECT * FROM user WHERE id = '".$q."'"; 
$result = mysql_query($sql); 

    while($row = mysql_fetch_array($result)) { 
    echo "<tr>"; 
    echo "<td><input type = 'text' name='username' id='username' size ='35' value=". $row['username'] ." ></td>";   
    echo "</tr>"; } echo "</table>"; 

    mysql_close($con); ?>

I had tried this code but I want to display the result in a text box. The user was found and it displayed in the text box. But only one word appeared in the text box. I don't want to separate the user name to first name and last name. So, the user name is in one field only.
So, for example the user name is 'hulk hogan'. Only hulk displayed in the text box. When I just displayed it without the text box, the name displayed all word, 'hulk hogan'. So, what should I do so the name in text box will display all word. Please help me.

Recommended Answers

All 7 Replies

Maybe there is something you have configured incorrectly with the element that has the ID = "txtHint"? You are not showing this element in your code. could it be a size property that you added or something else preventing all of the text from showing?

I already put the txtHint in HTML page. I just didn't show it here because I assumed all already know the HTML part.

Here the code for HTMl page:

<script src="selectuser.js"></script> 
<body> <form> Select a User: 
<select name="users" onchange="showUser(this.value)"> 
<option value="1">Hulk Hogan</option> 
<option value="2">Iron Man</option> 
<option value="3">Shrek</option>  

<p> <div id="txtHint"><b>User info will be listed here.</b></div> </p> 

I don't know. I already tried change the size and others code. But, still no solution.

Do you want to update a record and getting values using ajax? or only showing?

I want to get the values and update the record so thats why I want the user to appear in the text box. I'm sorry, I paste the wrong code. Here the real code and its working.

HTML page:

<form action="serah.php" method="post">
 <td><strong>Staff ID </strong></td>
 <td><label><strong><input type = "text" name="noPekerjaSerah"    id="noPekerjaSerah" size ="35" onBlur = "this.value=this.value.toUpperCase()" onChange="showUser2(this.value,noPekerjaSerah.value)" required/</strong></label></td>

   <td><strong>Staff Name</strong></td>
   <td><label><div id="txtHint2"><b></b></div>

Here Javasript code:

function showUser2(str)
if (str=="")
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  if (xmlhttp.readyState==4 && xmlhttp.status==200)


PHP code / getuser4.php:



 $q = $_GET['q']; 
 $query = "SELECT namaStaff FROM user WHERE ID = '".$q."'";

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

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

  echo "<tr>";
  echo "<td><input type = 'text' name='serahKepada' id='serahKepada' size ='35' value=". $row['namaStaff'] ." ></td>"; //only first word of user name appear in the text box. I wat to get this value and record it in database.
  echo "<td>". $row['namaStaff'] ."</td>";// Full word appear if not using textbox.
  echo "</tr>";

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

  echo "<tr>";
  echo "<td><input type = 'text' name='serahKepada' id='serahKepada' size ='35' value='". $row['namaStaff'] ."' ></td>"; //only first word of user name appear in the text box. I wat to get this value and record it in database.
  echo "<td>". $row['namaStaff'] ."</td>";// Full word appear if not using textbox.
  echo "</tr>";

focus on "$row['namaStaff']", here you have to include single quotes for value attribute of html as value=''. So the line becomes as in your case:

value='". $row['namaStaff'] ."'

Hope its the answer of question. :)

ohh,its work. Thank you for your answer. I didn't expect that's the problem. I took 2 weeks just want to solve this problem. Thank you again. You saved me. :)

Be a part of the DaniWeb community

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