1

2つのテキストフィールドがあり、最初のテキストフィールドにユーザーが入力すると、Ajaxによって2番目のテキストフィールドに表示されます。forループを使用して複数の行を作成する必要があり、すべての行に独自の値があります。 HTML

<form style="text-align:center" method="post" action="" name="form1">
<?php for($x=0; $x<4; $x++){ ?>
<input type="text" size="30"  id="country" onChange="getCurrencyCode('find_ccode.php?country='+this.value)" />
<input type="text" name="cur_code" id="cur_code" ></p>

アヤックス

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) 
    {
        req.onreadystatechange = function()
        {
            if (req.readyState == 4) 
            {           
                if (req.status == 200)
                {                       
                    document.getElementById('cur_code').value=req.responseText;                     
                } 

            }               
         }          
         req.open("GET", strURL, true);
         req.send(null);
    }           
}

PHP

$country=$_REQUEST['country'];
echo $country;

手伝ってください。

4

1 に答える 1

1

このようにしてみてください...

<form style="text-align:center" method="post" action="" name="form1">
<?php for($x=0; $x<4; $x++){ ?>
<input type="text" size="30"  id="country<?php echo $x; ?>" onChange="getCurrencyCode(<?php echo $x; ?>);" />
<input type="text" name="cur_code" id="cur_code<?php echo $x; ?>" ></p>

javascript関数

function getCurrencyCode(num)
{       
    thisval = document.getElementById('country'+num).value;
         if(thisval=="") {
             alert("Enter Proper Values!...");
             return false;   
        }
    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    }catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
     var strURL = "find_ccode.php?country="+thisval;
     ajaxRequest.onreadystatechange =getresult;      
     ajaxRequest.open("GET", strURL, true);
     ajaxRequest.send(null);
}
 function getresult(){
    if(ajaxRequest.readyState == 4){
        document.getElementById('cur_code'+num).value = ajaxRequest.responseText;
    }
 }
于 2012-04-23T08:47:26.293 に答える