简单修改AJAX脚本,将返回的输出赋值,而不是打印到文本框中


Easy modification to AJAX script to assign the returned output in a value instead of printing it in textbox

我使用以下脚本

<script>
function getXMLHTTP() { 
        var xmlhttp=false;  
        try{
            xmlhttp=new XMLHttpRequest();
        }
        catch(e)    {       
            try{            
                xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e){
                try{
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch(e1){
                    xmlhttp=false;
                }
            }
        }
        return xmlhttp;
    }

function getCurrencyCode(strURL)
{       
    var req = getXMLHTTP();     
    if (req) 
    {
        //function to be called when state is changed
        req.onreadystatechange = function()
        {
            //when state is completed i.e 4
            if (req.readyState == 4) 
            {           
                // only if http status is "OK"
                if (req.status == 200)
                {                       
                    document.getElementById('cur_code').value=req.responseText;                     
                } 
                else 
                {
                    alert("There was a problem while using XMLHTTP:'n" + req.statusText);
                }
            }               
         }          
         req.open("GET", strURL, true);
         req.send(null);
    }           
}
</script>

使用AJAX获取一些国家的货币,基于下拉值。

<select id="termid" class="selectfield" onChange="getCurrencyCode('find_ccode.php?country='+this.value)">

find_ccode.php

$country=$_REQUEST['country'];
switch($country)
{
    case "1" :
        echo "USD";
        break;
    case "2" :  
        echo "GBP";
        break;
    case "3" :  
        echo "NPR";
        break;
}
我的文本框是
<input type="text" name="cur_code" id="cur_code">

我想要的是将国家的货币代码分配到PHP变量中,并在每次更改时回显它。如何做到这一点?

谢谢

有几种方法可以做到这一点:

  1. 存储在PHP会话中。你可以这样写find_ccode。php

  2. 使用PHP将其存储在cookie中。还是在find_ccode。php

  3. 使用JavaScript将其存储在cookie中。您可以在ajax响应处理程序中执行此操作。

如果你坚持选择1或2,你就会得到这样的伪代码:

$currCode = $.cookie('currcode');

一旦页面被加载,你就不能再在该页面上运行php代码,这是使用ajax的部分原因,它允许你从另一个文件中运行php代码,并在现有页面上使用它(使用javascript)。

如果你不喜欢把结果放在一个文本框里,你可以把它放在任何其他有id的html标签里,比如一个表格单元格,一个div,一个span等等。

你只需写innerHTML=而不是value=

document.getElementById('thisdiv').innerHTML=req.responseText;

如果你使用jQuery,为什么要自定义HTTPRequest呢?

<select id="termid" class="selectfield" onChange="getCurrencyCode(this.value)"><option>...</option></select>
<input type="text" name="cur_code" id="cur_code">
<script type="text/javascript">
    function getCurrencyCode(setCountry){
        $.get('find_ccode.php', { country: setCountry }, function(data) {
            $('#cur_code').val(data);
        });
    }
</script>

后来编辑:则find_ccode.php可以为

session_start();
$country=$_REQUEST['country'];
switch($country)
{
    case "1" :
        echo "USD";
        $_SESSION['currency'] = 'USD';
        break;
    case "2" :  
        echo "GBP";
        $_SESSION['currency'] = 'GBP';
        break;
    case "3" :  
        echo "NPR";
        $_SESSION['currency'] = 'NPR';
        break;
}