views:

95

answers:

2

Hi,

I have two text boxes. Country and City. I am using type ahead in city text box. I want to show the city name according to the country which i typed. How can i pass country value using java script to my PHP function to get correct city name.

+1  A: 

You could use AJAX to send the country name to your PHP function.

But you could also do it with Javascript like this if you do not need to determine the city dynamically from the server side.

    <script type="text/javascript">
        function sync() {
            var country = document.getElementById('country').value;
            var city = document.getElementById('city');
            switch (country) {
                case 'China':
                    city.value = 'Shanghai';
                    break;
                case 'France':
                    city.value = 'Pairs';
                    break;
            }
        }
    </script>

    <html>
        <form style="text-align:center">
            Country:<input id="country" type="text" title="Country" onblur="sync()"/><br/>
            City:<input id="city" type="text" title="City"/><br/>
        </form>
   </html>
Jichao
A: 

document.getElementById('yourCountryidhere').value

naveed