0

This is the html file.

<html>
<head>
<script>
function showUser(str) {
    if (str == "") {
                return;
    } else {
        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) 
    {
         var doc = window.document.createElement("doc");
         var a = JSON.parse(xmlhttp.responseText);

     document.getElementById("stuname").value=a.first;
         document.getElementById("branch").value=a.second;
         document.getElementById("year").value=a.third;
         document.getElementById("category").value=a.four;
      }
        }
        xmlhttp.open("GET","test2.php?q="+str,true);
        xmlhttp.send();
    }

}

</script>
</head>
<body>

<form>
Roll Number:<br>
<input type="text" name="rollno" id="rollno" onblur="showUser(this.value)">
<br>
Student Name:<br>
<input type="text" name="stuname" id="stuname" value="">
Branch:<br>
<input type="text" name="branch" id="branch" value="">
Year:<br>
<input type="text" name="year" id="year" value="">
Category:<br>
<input type="text" name="category" id="category" value="">
</form>
<br>

</body>
</html>

test2.php file

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
$q = $_GET['q'];

$con=mysqli_connect("localhost","root","neel","sitams");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }


$sql="SELECT rollno,stuname,branch,category FROM studet where rollno='".$q."' and academic='2014-2015'";

if ($result=mysqli_query($con,$sql))
  {
  while ($obj=mysqli_fetch_object($result))
    {
     $queryResult[] = $obj->rollno;
     $queryResult[] = $obj->stuname;
     $queryResult[] = $obj->branch;
     $queryResult[] = $obj->category;
    }
}

$textboxValue1 = $queryResult[0];
$textboxValue2 = $queryResult[1];
$textboxValue3 = $queryResult[2];
$textboxValue4 = $queryResult[3];
echo json_encode(array('first'=>$textboxValue1,'second'=>$textboxValue2,'third'=>$textboxValue3,'four'=>$textboxValue4));
?>

</body>
</html>

I could not able to load values to the text boxes where is fault. Even I have seen stackoverflow but I could not able to get it. when I type test2.php by passing rollno value to q it will display data but when I pass a value from html it could not able to set the values to the textbox fields.

2
  • did you tryed console.info(a) in order to see what you are getting from ajax call Commented Aug 18, 2015 at 18:06
  • What is the html code for in test2.php? Commented Aug 18, 2015 at 18:08

2 Answers 2

1

Remove any html tags from test2.php . An ajax document does not need any html tags.

Sign up to request clarification or add additional context in comments.

Comments

0
Hi this one will be helpful to you .
<html>
<head>

</head>
<body>

<input type="button" onclick="showUser('ok');" value="click to ru me" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script>
function showUser(str) 
{
    if (str=="") 
    {
      return;
    } 
    else 
    {
        if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); }
        else {  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");   }
        xmlhttp.onreadystatechange = function() 
        { 
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
            {
                //var doc = window.document.createElement("doc");
                var parsed = JSON.parse(xmlhttp.responseText);
                for(var x in parsed)
                {
                    var First=parsed[x].first;
                    var Second=parsed[x].second;
                    var Third=parsed[x].third;
                    var Fourth=parsed[x].fourth;
                    console.log("first="+First+" second="+Second+" third="+Third+" fourth="+Fourth);

                     document.getElementById("stuname").value=parsed[x].first;
                     document.getElementById("branch").value=parsed[x].second;
                     document.getElementById("year").value=parsed[x].third;
                     document.getElementById("category").value=parsed[x].fourth;
                }

            }
         }
        xmlhttp.open("GET","phpex.php?q="+str,true);
        xmlhttp.send();
    }

}
</script>

<input type="text" id="stuname" />
<input type="text" id="branch" />
<input type="text" id="year" />
<input type="text" id="category" />
</body>
</html>

Ajax File Name phpex.php . ajax page code is below

<?php
echo '[{"first":"1111","second":"2222","third":"3333","fourth":"4444"}]';
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.