dongsuo9982 2013-11-13 05:55
浏览 181

如何使用ajax从一个文本框值自动填充另一个文本框值

Hi I wanted to place search box like just dial site when we type the company name it will automatically fill the city name in the another text box

" onfocus="javascript:this.value='';" onblur="javascript: if(this.value==''){this.value='';}" onKeydown="javascript: if (event.keyCode==13)serachProduct();"/>
                <div class="textCurve1" style="float:left;margin-left:10px;">
                    <div class="selectsSR">
                        <input type="text" id="cityname" name="cityname" onKeydown="javascript: if (event.keyCode==13)serachProduct();" value="<?php if(isset($cityName)){echo ucfirst($cityName);}else{echo"City";} ?>" onfocus="javascript:this.value='';" onblur="javascript: if(this.value==''){this.value='<?php if(isset($cityName)){echo ucfirst($cityName);}else{echo"City";} ?>';}"/>
                    </div>
                </div>

SELECT select_company.company_name,select_city.city FROM select_company LEFT JOIN select_city ON(select_company.city=select_city.id) WHERE select_company.company_name LIKE ".$this->db->escape($company."%")." LIMIT 10"

want to update the city field text box automatically

  • 写回答

1条回答

  • doujuanxun7167 2013-11-13 06:02
    关注

    What you can do is you can fire a ajax call on the keyup, keydown or onblur of the first text box.

    Pass the first text box data to ajax php file

    Now search your company database and find if the company is there and if there what is the relevant city in the database.

    Pick the city name and echo it in the ajax.php file to send response.

    When you got the response just replace the innerHtml of the second textbox with the city name you got in the response.

    I have given you the base logic try it yourself and you can find examples on web also still if you not able to achieve result please post with the code you tried i wil help you surely.

    Below is the ajax function

    function loadcity(val) {
        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    
                var res = xmlhttp.responseText;// The response contain the city name returned from the php file from the query result 
                document.getElementById('citytextbox').value=res;// insert the city name in the city text box
                document.getElementById('loaderdivid').style.display='none';//hide loader when you got the response  
            }
        }
        var url = 'ajax.php'; // Url for the ajax.php file where you will fire query to get the city name 
    
        //Before response
        //Write the code if you want to add some loder image untill the response comes
        document.getElementById('loaderdivid').style.display='block';// show the loader when the request is made 
    
        xmlhttp.open("POST", url, true);
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.send("data=" + val);
    }
    

    Already you have the query write it on the ajax.php file

    and pass the first textbox value in the function and rest you can understand by the comments in the code

    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大