0

好的。我不了解 jQuery 或 Javascript,但需要做一个可能非常简单的简单任务。

我有一个用户注册页面,表单代码如下:

<form method="post" action="check.php">
    Minecraft Username:<input type="text" name="user">
    Password:<input type="password" name="pass">
    Password Again:<input type="password" name="passConfirm">
    <input type="submit" value="Login" id="buttonLogin">
</form>

好的。所以我想要做的是,当“我的世界用户名”字段发生变化时,它会更新一个变量。如果有人可以编写和解释代码,这就是我需要的。

在文本框值更改时,它会将值设置为名为 $registerUsername 的 php 变量,并且该变量用于 img...

<img src="https://minotar.net/helm/<?php echo $registerUsername ?>/20.png">

非常感谢。:D

编辑:我希望它实时更改,而不是在发布后。

4

2 回答 2

3

您需要进行 AJAX 调用。以下是解释代码 ajax_call.php

<html>
<head>
<script type="text/javascript">
//function which is called as soon as a user types a single character
function update()
{   
    //Step 1:create XMLHttpRequest object to make AJAX call.
    try{
        //for firefox,chrome and opera.
        xmlHttp=new XMLHttpRequest();
    }catch(e){
        try{
        //for IE    
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch(e2){
        //Otherwise, notify browser doesn't support ajax.
        alert('Sorry ! AJAX not supported');
    }
    }
    //create a handler function to handle the response
    xmlHttp.onreadystatechange=function(){
        //execute the code only when response is successfull
        //readyState=4 denotes success
        //HTTP status=200 denotes OK.
        if(xmlHttp.readyState==4&&xmlHttp.status==200){
            //update the div inside HTML with the respone text received.
            document.getElementById('content').innerHTML=xmlHttp.responseText;
        }
    }
    //make AJAX call
    xmlHttp.open('GET','ajax_reply.php?content='+document.getElementById('search_text').value,true);
    xmlHttp.send(null);
}
</script>
</head>
<body>
<form name='myForm'>
    <input type="text" id="search_text" onkeyup="update()">
</form>
<div id="content"></div>
</body>
</html>

现在ajax_reply.php,您可以设置变量或回复响应或做任何您喜欢的事情的代码。

<?php
if(isset($_GET['content']))
echo $_GET['content'];
//you can set the variable here like:-
//$text = $_GET['content'];
?>

我希望这有帮助。

于 2013-06-25T07:57:40.637 回答
2

您可以尝试以下代码:

<form method="post" action="check.php">
    Minecraft Username:<input type="text" name="user" id="user" />
    Password:<input type="password" name="pass" />
    Password Again:<input type="password" name="passConfirm" />
    <input type="submit" value="Login" id="buttonLogin" />
</form>
<img id="minecraft_img" src="" />
<script type="text/javascript">
    $(function(){
       $("#user").on('change', function(){
            $.ajax({
               url      : 'ajax.php',
               data     : {'user' : $("#user").val()},
               type     : 'POST',
               success  : function(resp){
                    $("#minecraft_img").attr('src', resp);
               },
               error    : function(resp){
                    alert('Ajax Error !');
               }
            });
       }); 
    });
</script>

<?php
    if(isset($_POST['user']) && $_POST['user'] != ""){
        $session['minecraftUser']   = $_POST['user'];
        echo 'https://minotar.net/helm/'.$session['minecraftUser'].'/20.png';
    }

?>
于 2013-06-25T08:56:58.900 回答