0

I am creating a registration form for my web site. In my registration form, there are two select boxes to select user's district and there city. So I need to do it, when a user select their district then automatically display city select box with cities which relevant to above selected district. To do this I used Ajax and php. I used findcity.php page to display the city in my register.php page. My problem is when I am try to get city id from the register.php page I cant get it. It I need to get to send with other data from register.php page to database. Please anybody help me to do it. and suggestions are greatly appreciated. thank you.

from my register.php page

<?php

    require_once ('../includes/config.inc.php');    
    require_once( MYSQL2 );

    $query="select * from district order by district_id";
    $result = mysqli_query( $dbc, $query);

        echo '<select name="district" class="text" onChange="getCity(' . "'" . 'findcity.php?district=' . "'" . '+this.value)">';
        echo '<option value="">-- Select District --</option>';

        while( $row = mysqli_fetch_array($result, MYSQLI_NUM)) { 
            echo '<option value="' . $row[0] . '"';

            // Check for stickyness: 
            if ( isset( $_POST['district']) && ( $_POST['district'] == $row[0] ))    
                echo ' selected="selected"';

                echo " >$row[1]</option>";    
        }
        echo '</select>';
?> 

</div>    
<div>
    <label for="city">City <img src="../images/required_star.png" alt="required" /> : </label>
    <div id="citydiv" style="position: relative; top: -14px; left: 130px; margin-bottom: -26px;">
        <select name="city" class="text">
            <option>-- Select City --</option>
        </select>
    </div>
</div>

this is findcity.php page

<?php

$districtId=$_GET['district'];

require_once ('../includes/configaration.inc.php'); 
require_once( MYSQLCONNECTION );

$query="select city_id, city_name from city2 where district_id=$districtId";
$result=mysqli_query( $dbc, $query);



echo '<select name="city" class="text">
        <option>-- Select City --</option>';

while($row=mysqli_fetch_array($result, MYSQLI_NUM)) { 

    echo '<option value="' . $row[0] . '"';

    // Check for stickyness: 
    if ( isset( $_POST['city']) && ( $_POST['city'] == $row[0] )) { 
        echo ' selected="selected"';

        //echo '<input type="hidden" name="city"  value="' . $row[0] . '"'; 

    }
        echo " >$row[1]</option>";  

}

echo '</select>';

?>

this is my ajax functions

function getXMLHTTP() { //fuction to return the xml http object
    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 getCity(strURL) {      

    var req = getXMLHTTP();

    if (req) {

        req.onreadystatechange = function() {
            if (req.readyState == 4) {
                // only if "OK"
                if (req.status == 200) {                        
                    document.getElementById('citydiv').innerHTML=req.responseText;                      
                } else {
                    alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                }
            }               
        }           
        req.open("GET", strURL, true);
        req.send(null);
    }

}
4
Contributors
14
Replies
18
Views
5 Years
Discussion Span
Last Post by broj1
0

You said you are using a form, right. Well, I can't see any form tag on page. You should correct that first. Also make sure that if your form uses the POST method, you access the form variables through $_POST global variable in your PHP scripts, and the get $_GET global variable if your form uses the GET method. If you mix them up, you will never get any data from your fom.

0

yes I have used form tag. this is a long registraiton form I post here only my form's two select box only. those two select box also working properly. my problem is i used separate php script to populate my city select box according to my district select box value. actually I need to get city id from that inner html page to my origianal register.php page to send cityid to the database along with other registration details.. .
so can any body help me to get cityid to my register.php page.

any reply are greatly appreciatted.
thank you

0

Try to find out whether the findcity.php, which is supposed to return HTML code for a select element, works OK. I would put the following on the line 10 of the findcity.php:

die($query);

which will display the query instead of the select element. Now you can test the query in phpmyadmin.

If the query is OK then you can additionally check what the query returns by placing this in line 17:

die($row[0] . ' ' . $row[1]);

which should display city ID and city name.

0

thanks for your reply...

querry is working I did as you said....

the problem is how can i get that city id to my register.php page.

0

The ajax call will return the code for the select element and place it in the div which has the id=citydiv. Maybe you should check whether the ajax call generates the corect html code for select element. You can try this by inserting the following after the line 34 in the ajax functions code above:

alert(req.responseText);

It is a bit of a primitive way to debug but sometimes it helps. Also have a look at the javascript error console (in Firefox) whether there are any errors.

I am leaving now and won't be able to do more extensive testing of your codefor next 4 hours or so. If your problem does not get solved until then I'll be back.

0

thank you for your reply

I did it then I got a aler box with this massege. it consist my city select box values. it change according to the district. there also display city_id and city_name clearly...

but the problem is how can i get that city id to my register.php page.

thank you...

0

Try putting the whole ajax function together. I dont know why you broke it up like that.
Change your select statement to this.

echo '<select name="district" class="text" onChange="getCity(this.value)">';

And try using this for your ajax on register.php

function getCity(str) {
var xmlhttp;
if (str=="") {
    document.getElementById("citydiv").innerHTML="";
    return;
}
if (window.XMLHttpRequest) {
    xmlhttp=new XMLHttpRequest();
} else {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
        document.getElementById("citydiv").innerHTML=xmlhttp.responseText;
    }
}
xmlhttp.open("GET","findcity.php?district="+str,true);
xmlhttp.send();
}

Try that.

0

it alse working... if I select a value from 1st select box second one is populating....

problem is when i try to get city id to register.php page..

0

problem is when i try to get city id to register.php page..

I am not sure whether I understand what the problem is. Do you actualy want to know how to get the city ID once the user selects the city from the second select element? This is just a classic: check the request array. Somewhere In the beginning of the register.php page put something like:

// check if the form was submitted
// use $_GET if the method was get, use $_POST if the method was post
if(isset($_GET['submit']) && isset($_GET['city'])) {
    // do whatever you want when the user has submitted the city ID
    // ...
    // then redirect to some other page
    header('location:somewhere.php');
}

// if the form was not submitted just do the usual script
// ...

Edited by broj1

0

ya actually I need to get city id... it is need for me to send to the database with other input valus from my register.php page. other input values i can collect through my register.php. but city id I cant. becoz it has in separate php page called findcity.php.

0

findcity.php only creates a html code for a select element with appropriate ID and city names and javascript inserts this select element into the request.php page so request.php page should have a city ID in $_POST (or $_GET) array. What do you get if you print_var($_POST) after submitting the form?

0

I didnt see any value in city in my $_POST arrya after submitting the forum.. every other input values goto post array when I am submitting the form. what I do??

0

I tested the code and it returns city ID OK. The only thing I added was onchage event for the second select element:

 echo '<select name="city" class="text" onChange="this.form.submit();">

in the findcity.php so the second select also submits the form (but you might have a submit button for that).

In the beginning of the request.php I put this code to test:

if(isset($_POST['city'])) {

    $str  = '****************************************<br />';
    $str .= "{$_POST['city']}<br />";
    $str .= '****************************************<br />';

    die($str);
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.