PHP将JSON值设置为Textboxes

时间:2015-08-18 17:58:14

标签: php ajax json

这是html文件。

public class MyClass { ... }
public partial class MyForm : Form { ... }

test2.php文件

<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>

我无法将值加载到出错的文本框中。即使我已经看到stackoverflow但我无法得到它。当我通过将rollno值传递给q键入test2.php它将显示数据,但是当我从html传递一个值时,它无法将值设置为文本框字段。

2 个答案:

答案 0 :(得分:1)

从test2.php中删除所有html标记。 ajax文档不需要任何html标记。

答案 1 :(得分: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"}]';
?>