如何将Ajax的结果(从服务器获取数据)放入输入标记?

时间:2015-07-23 02:36:22

标签: php ajax

看我的照片:

我希望将结果填入输入标记。

如果我这样做,它会起作用:

<?php
    include('c2gcon.php');
    include('logincheckadmin.php');
    $nameajax=isset($_REQUEST['nameajax'])?$_REQUEST['nameajax']:null;
    if($nameajax<>null){
        $username=$nameajax;
        $_SESSION['simpanun8']=$_SESSION['simpanun8'].','.$username;
        $_SESSION['simpanun8'][0]='';
        echo "username=$_SESSION[simpanun8]";
        exit;
    }
?>
<html>
<head>
    <title>DELETE RECORD</title>
    <link rel="shortcut icon" href="http://c2gdragonteam.com/img/dragonhead.ico">
    <link href="/css/bootstrap.css" rel="stylesheet">
    <style>
        p,td {
            font-size: 70%;
        }
    </style>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular-resource.min.js"></script>
</head>
<body ng-app="ctrljs">
<?php
    $_SESSION['simpanun8']=null;
    $ssloginadmin=$_SESSION['ssloginadmin'];
    $page=isset($_REQUEST['page'])?$_REQUEST['page']:null;
    if($page=='form'){
?>
    <form method='post' name='formupgrade' ng-controller='formctrl'>
        <table>
            <tr><td>Username : <em>(using comma if multiuser)</em></td></tr>
            <tr>
                <td>
                    <select name='username' id='username' ng-model='username' required>
                        <option value='' selected></option>
<?php
                            $q=mysql_query("SELECT username FROM t_un WHERE upgrade='1' and t1count='0' and t2count='0' ORDER BY username");
                            while($f=mysql_fetch_object($q)){
?>
                                <option value='<?php echo "$f->username";?>'><?php echo "$f->username";?></option>
<?php
                            }
?>
                    </select>
                    <input type='button' value='add' onclick='checkusername();'></input>
                </td>
            </tr>
            <tr>
                <td>
                    <input name='usernameresult' ng-model='usernameresult' required ng-pattern="/^[A-Za-z0-9/\.\-\s\,]*$/"></input>
                </td>
            </tr>
        </table>
        <input type='submit' value='submit' ng-hide="!formupgrade.$valid"></input>
    </form>
    <br>
    <div id='resultmunculdisini'></div>
<?php   
    }
?>
<script>
function checkusername(){
    var status = document.getElementById("resultmunculdisini");
    var u = document.getElementById("username").value;
    if(u != ""){
        status.innerHTML = "<font color='red'><img src='/img/loading.GIF'></img> checking...</font>";
        var hr = new XMLHttpRequest();
        hr.open("POST", "admintreedelrec.php", true);
        hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        hr.onreadystatechange = function() {
            if(hr.readyState == 4 && hr.status == 200) {
                status.innerHTML = hr.responseText;
            }
        }
        var v = "nameajax="+u;
        hr.send(v);
    }
}
</script>
<script>
var app = angular.module('ctrljs', []);
app.controller('formctrl', function($scope, $http){
  $scope.digits = {};
});
</script>
</body>
</html>

我使用的是AJAX,如果我喜欢上面的话就像魅力一样, 但我不知道如何将结果放入输入标签。 我尝试过这样的做法,我把ID(id =&#39; resultmunculdisini&#39;)放在输入标签中,但没有任何反应:

            <tr>
                <td>
                    <input name='usernameresult' id='resultmunculdisini' ng-model='usernameresult' required ng-pattern="/^[A-Za-z0-9/\.\-\s\,]*$/"></input>
                </td>
            </tr>

我将旧ID更改为非活动状态,如下所示:

<!--<div id='resultmunculdisini'></div>-->

2 个答案:

答案 0 :(得分:1)

输入不会有innerhtml,它会有value

document.getElementById("resultmunculdisini").value=hr.responseText;

你给了多个dom元素(div和input)相同的id(resultmunculdisini)。 id应该是唯一的。将div的id删除为resultmunculdisini

another.php

<?php
    include('c2gcon.php');
    include('logincheckadmin.php');
    $nameajax=isset($_REQUEST['nameajax'])?$_REQUEST['nameajax']:null;
    if($nameajax<>null){
        $username=$nameajax;
        $_SESSION['simpanun8']=$_SESSION['simpanun8'].','.$username;
        $_SESSION['simpanun8'][0]='';
        echo "username=$_SESSION[simpanun8]";
        exit;
    }
?>

你的php脚本

    hr.open("POST", "another.php", true);
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    hr.onreadystatechange = function() {
        if(hr.readyState == 4 && hr.status == 200) {
            status.innerHTML = hr.responseText;
        }
    }
    var v = "nameajax="+u;
    hr.send(v);

答案 1 :(得分:0)

这是没有XMLHttpRequest

<tr>
<td>
<input name='usernameresult' id='resultmunculdisini' ng-model='usernameresult' required ng-pattern="/^[A-Za-z0-9/\.\-\s\,]*$/" value="<?php echo "$_SESSION[simpanun8]";?>"></input>
</td>
</tr>

<form method='post' name='formupgrade' ng-controller='formctrl' action="admintreedelrec.php">
<select name='nameajax' id='username' ng-model='username' required>
<option value='' selected></option>
<?php
$q=mysql_query("SELECT username FROM t_un WHERE upgrade='1' and t1count='0' and t2count='0' ORDER BY username");
while($f=mysql_fetch_object($q)){
?>
<option value='<?php echo "$f->username";?>'><?php echo "$f->username";?></option>
<?php
                        }
?>
</select>
<input type='button' value='add' onclick='checkusername();'></input>
<input type='submit' value='add'></input>
</form>