微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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 =’resultmunculdisini’)放在输入标签中,但没有任何反应:

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

解决方法:

输入不会有innerhtml,它会有价值.

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

你给了多个dom元素(div和输入)相同的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);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐