Jump to content

Get city id from ajax generating city select box to my original php page


Recommended Posts

ok dear.. thank u very much for your cooperation.

 

This is my entire register.php page...

 

<?php

// Require the configuration before any PHP code as the configuration controls error reporting:
require ('../includes/config.inc.php');

$page_title = "Institute | Registration - Step 1";
include ('includes/header.html');

// Require the database connection:
require (MYSQL2);

// For storing registration errors:
$reg_errors = array();



// Check for a form submission:
//if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if ( isset( $_POST['submitted'])) {

// Check for a institute name:
if (preg_match ('/^[A-Z \'.-]{2,80}$/i', $_POST['ins_name'])) {
	$insName = mysqli_real_escape_string ($dbc, $_POST['ins_name']);
} else {
	$reg_errors['ins_name'] = 'Please enter your Institute name correctly!';
}

// Check for a institute slogan:
$Slogan = (!empty( $_POST['ins_slogan'])) ? mysqli_real_escape_string ( $dbc, $_POST['ins_slogan']) : NULL; 


// Check for the introduction note:
if (!empty($_POST['present_situation'])) {
	$present = mysqli_real_escape_string($dbc, strip_tags($_POST['present_situation']));
} else {
	$reg_errors['present_situation'] = 'Please enter a Note of present situation!';
}	

// Check for the address one:
if (!empty($_POST['address1'])) {
	$address1 = mysqli_real_escape_string($dbc, $_POST['address1']);
} else {
	$reg_errors['address1'] = 'Please enter a Address One correctly!';
}

// Check for a institute slogan:
$address2 = (!empty( $_POST['address2'])) ? mysqli_real_escape_string ( $dbc, $_POST['address2']) : NULL; 



// Check for a mobile number:
if ( is_numeric( $_POST['mobile'])) {
	$mobile = mysqli_real_escape_string ( $dbc, $_POST['mobile']);
} else {
	$reg_errors['mobile'] = 'Please enter a Mobile number correctly!';
} 	


// Check for an email address:
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
	$email = mysqli_real_escape_string ($dbc, $_POST['email']);
} else {
	$reg_errors['email'] = 'Please enter a valid email address!';
}

// Check for a password and match against the confirmed password:
if (!empty($_POST['pass1'])) {
	if ($_POST['pass1'] == $_POST['pass2']) {
		$pass = mysqli_real_escape_string ($dbc, $_POST['pass1']);
	} else {
		$reg_errors['pass2'] = 'Your password did not match the confirmed password!';
	}
} else {
	$reg_errors['pass1'] = 'Please enter a valid password!';
}


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

	$district = $_POST['district'];
}



if (empty($reg_errors)) { // If everything's OK...

	// Make sure the email address and username are available:
	$q = "SELECT email FROM login WHERE email='$email'";
	$r = mysqli_query ($dbc, $q);

	// Get the number of rows returned:
	$rows = mysqli_num_rows($r);

	if ($rows == 0) { // No problems!

		echo 'there is no such a email';

		// Add the login details to the database...		
		$q = "INSERT INTO login (email, password, login_level, last_login) 
				VALUES ('$email', '"  .  get_password_hash($pass) .  "', 2, now() )"; 

		$r = mysqli_query ($dbc, $q);

		$loginId = mysqli_insert_id($dbc);

if ( $r ) {
	echo 'good';

	} else { echo 'bad';

	echo '<p>' . mysqli_error($dbc) . '<br />Query: ' . $q . '</p>'; // Debugging message.

}  			
	echo "<pre>"; 
		print_r($_POST)	;
	echo "</pre>";

	/* 
	-
	-
	-
	-
	-	here I need to get cityid from findcity page for database insertion..
	-
	-
	-
	-
	-
	-
	*/

		//if (mysqli_affected_rows($dbc) == 1) { // If it ran OK.
	} else { // The email address or username is not available.

		$reg_errors['email'] = 'This email address has already been registered. Try again with a different email';			

	} // End of $rows == 0 IF.
} // End of empty($reg_errors) IF.	
} // End of the main form submission conditional.
?>

	<div id="inner_wrapper">

		<div id="contents">

			<div id="content2">

				<h1>Tutor Registration</h1>

				<div style="height: 15px; background: #fff;" class="extra_top"></div>
				<div id="content_body">						

				<?php
					require ('/includes/form_functions.inc.php');
				?>					
					<h2>Basic Infomation</h2>

					<form name="centerRegister" action="register.php" method="post" accept-charset="utf-8" > 
					<p style="margin: -15px 0 10px 0; font-weight: normal;">Required fields are marked with <img src="../images/required_star.png" alt="required" /></p>

					<div class="group">
						<div>
							<label for="ins_name">Institute Name <img src="../images/required_star.png" alt="required" /> : </label>
							<?php create_form_input('ins_name', 'text', $reg_errors); ?>								
						</div>	
						<div>	
							<label for="ins_slogan">Institute Slogan : </label>
							<?php create_form_input('ins_slogan', 'text', $reg_errors); ?>
						</div>

					</div>

				</div>
				<div style="height: 15px; background: #fff;" class="extra_bottom"></div>
				<br /><div style="margin-bottom: 4px;"></div>			
				<div style="height: 15px; background: #fff;" class="extra_top"></div>
				<div id="content_body">						

					<h2>Contact Information</h2>

					<div class="group">

						<div>
							<label for="address1">Address 1 <img src="../images/required_star.png" alt="required" /> : </label>
							<?php create_form_input('address1', 'text', $reg_errors); ?>
						</div>							
						<div>
							<label for="address2">Address 2 : </label>
							<?php create_form_input('address2', 'text', $reg_errors); ?>
						</div>
						<div>
							<label for="district">District <img src="../images/required_star.png" alt="required" /> : </label>
							<?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 '<select name="district" class="text" onChange="getCity(' . '\'findcity.php?district=\'+this.value+\'&city=\'' . '+document.getElementsByName(\'city\')[0].value)">';
									echo '<select name="district" class="text" id="district" 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>								
						<div>
							<label for="mobile">Mobile <img src="../images/required_star.png"  alt="required" /> : </label>
							<?php create_form_input('mobile', 'text', $reg_errors); ?>								
						</div>
						<div>
							<label for="email">Email Address<img src="../images/required_star.png"  alt="required" /> : </label>
							<?php create_form_input('email', 'text', $reg_errors); ?>	
						</div>
						<div>
							<label for="pass1">Password <img src="../images/required_star.png"  alt="required" /> : </label>
							<?php create_form_input('pass1', 'password', $reg_errors); ?>								
						</div>
						<div>
							<label for="pass2">Conform Password <img src="../images/required_star.png"  alt="required" /> : </label>
							<?php create_form_input('pass2', 'password', $reg_errors); ?>								
						</div>
					</div>
				</div>

				<div id="content_body">						

					<div class="group">

						<div style="border-top: 1px dotted #bfbfbf; margin-top: 20px; padding: 8px 0;">
							<label> </label>
							<input type="submit"  name="submitted"  value="Register" class="formSubmit" />						

							<!-- <input type="hidden"   name="submitted"   value="TRUE" /> -->	
						</div>		

					</div>

					</form>
				</div>
				<div style="height: 15px; background: #fff;" class="extra_bottom"></div>



			</div>
		</div> <!-- end contents div -->
	</div> <!-- end inner_wrapper div --> 
</div> <!-- end wrapper1 div --> 

<?php
include ('includes/footer.html');
?>

 

You never added the id to your <select name="city"> field, to start.

 

Change it to this:

<select name="city" class="text" id="citylist">
    <option>-- Select A District First --</option>
</select>

now in my register.php coding to city dropdown list is similar to this.... but still nothing happen.. city dropdown list is not populating according to the selected district.

 

						<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" id="citylist" >
									<option>-- Select City --</option>
								</select>

							</div>
						</div>	

 

 

I found there is no any 'select' tag in findcity page or in json object......is it doesn't matter?

findcity.php

<?php

  require_once ('../includes/config.inc.php');
  require_once( MYSQL2 );
  
  $districtId = (int) $_GET['district']; 

  $query = "select city_id, city_name from city2 where district_id=$districtId";
  $result = mysqli_query( $dbc, $query);
  
  $jsonarray = array();
  while( $row = mysqli_fetch_array($result, MYSQLI_NUM) ){
    $jsonarray[$row[0]] = $row[1];
  }

  print json_encode( $jsonarray );

?>

then what has happend???  why my second dropdown is not populating?

 

I ask again this

I found there is no any 'select' tag in findcity page or in json object......is it doesn't matter?

No, all you want in the json object is the dynamic data from the database, then it creates the options and puts them between

<select id="citylist">OUTPUT GOES HERE</select>

using document.getElementById('citylist').innerHTML = output;

then what has happend???  why my second dropdown is not populating?

 

I ask again this

I found there is no any 'select' tag in findcity page or in json object......is it doesn't matter?

No, all you want in the json object is the dynamic data from the database, then it creates the options and puts them between

<select id="citylist">OUTPUT GOES HERE</select>

using document.getElementById('citylist').innerHTML = output;

Do some debugging on the findcity_ajax.js file and see what's happening. Use some alerts to see what's happening line by line.

ok.. wait I will check again..

 

just I found this code from your post

 

$districtId = (int) $_GET['district']; 

 

that line is not a "make or break" line, just good practice to avoid injection.

 

Here's a sample of how you can debug the js:

            alert(req.responseText);          
            var jsonresponse = eval("("+req.responseText+")");    
            var output = '';
            output   += '<option>-- Select City --</option>';
            for (var key in jsonresponse) {
                alert( jsonresponse[key] ); //that should alert the city name each time it loops
                output += '<option value="'+key+'">'+jsonresponse[key]+'</option>';
            }
            alert( output );
            document.getElementById('citylist').innerHTML = output; 

JS does not have good error handling built in like php, so we have to place alerts all over the place and test each piece of data to make sure we are getting what we expect.

The first alert should be a plain text view of the response it should like something like, {"2":"new york","5":"los angeles"}, depending on the data you've queried.

The alert inside the for loop should alert the city name for each of the returned rows.

The third and final alert should be the complete output to be inserted between the <select> tags. That alert should like <option value="2">New York</option><option value="5">Los Angeles</option>

wow..... My problem have solved......  now I can get cityid to my post array when i am submitting the form...

 

Thank you very much my dear.... you are a nice person!!!!! I respect you...

 

now I feel that took away a big part of my head....

 

Thank you again your cooperation.

  • 5 months later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.