在文本框值更改时,文本框值为php变量

时间:2013-06-24 16:25:37

标签: php variables onchange

确定。我不了解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>

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

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

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

非常感谢。 :d

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

2 个答案:

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

我希望这会有所帮助。

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

?>
相关问题