PDA

View Full Version : سوال: مشکل در اجرای کد انتخاب کشور و استان و شهر



hamidhassas
دوشنبه 05 خرداد 1393, 19:57 عصر
من با کد زیر اطلاعات رو نمایش میدم اما خطا میده

database


CREATE TABLE `city` (
`id` tinyint(4) NOT NULL auto_increment,
`city` varchar(50) default NULL,
`stateid` tinyint(4) default NULL,
`countryid` tinyint(4) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 ;

CREATE TABLE `country` (
`id` tinyint(4) NOT NULL auto_increment,
`country` varchar(20) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 ;

CREATE TABLE `state` (
`id` tinyint(4) NOT NULL auto_increment,
`countryid` tinyint(4) NOT NULL,
`statename` varchar(40) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 ;


findCity.php


<? $countryId=intval($_GET['country']);

$stateId=intval($_GET['state']);

$link = mysql_connect('localhost', 'root', ''); //changet the configuration in required

if (!$link) {

die('Could not connect: ' . mysql_error());

}

mysql_select_db('db_ajax');

$query="SELECT id,city FROM city WHERE countryid='$countryId' AND stateid='$stateId'";

$result=mysql_query($query);


?>

<select name="city">

<option>Select City</option>

<? while($row=mysql_fetch_array($result)) { ?>

<option value><?=$row['city']?></option>

<? } ?>

</select>


findState.php


<? $country=intval($_GET['country']);

$link = mysql_connect('localhost', 'root', ''); //changet the configuration in required

if (!$link) {

die('Could not connect: ' . mysql_error());

}

mysql_select_db('db_ajax');

$query="SELECT id,statename FROM state WHERE countryid='$country'";

$result=mysql_query($query);


?>

<select name="state" onchange="getCity(<?=$country?>,this.value)">

<option>Select State</option>

<? while($row=mysql_fetch_array($result)) { ?>

<option value=<?=$row['id']?>><?=$row['statename']?></option>

<? } ?>

</select>



index.php


<html>

<head>

<title>Roshan's Triple Ajax dropdown code</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script language="javascript" type="text/javascript">

// Roshan's Ajax dropdown code with php

// This notice must stay intact for legal use

// Copyright reserved to Roshan Bhattarai - nepaliboy007@yahoo.com

// If you have any problem contact me at http://roshanbh.com.np

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 getState(countryId) {



var strURL="findState.php?country="+countryId;

var req = getXMLHTTP();



if (req) {



req.onreadystatechange = function() {

if (req.readyState == 4) {

// only if "OK"

if (req.status == 200) {

document.getElementById('statediv').innerHTML=req. responseText;

} else {

alert("There was a problem while using XMLHTTP:\n" + req.statusText);

}

}

}

req.open("GET", strURL, true);

req.send(null);

}

}

function getCity(countryId,stateId) {

var strURL="findCity.php?country="+countryId+"&state="+stateId;

var req = getXMLHTTP();



if (req) {



req.onreadystatechange = function() {

if (req.readyState == 4) {

// only if "OK"

if (req.status == 200) {

document.getElementById('citydiv').innerHTML=req.r esponseText;

} else {

alert("There was a problem while using XMLHTTP:\n" + req.statusText);

}

}

}

req.open("GET", strURL, true);

req.send(null);

}



}

</script>

</head>

<body>

<form method="post" action="" name="form1">

<table width="60%" border="0" cellspacing="0" cellpadding="0">

<tr>

<td width="150">Country</td>

<td width="150"><select name="country" onChange="getState(this.value)">

<option value="">Select Country</option>

<option value="1">USA</option>

<option value="2">Canada</option>

</select></td>

</tr>

<tr style="">

<td>State</td>

<td ><div id="statediv"><select name="state" >

<option>Select Country First</option>

</select></div></td>

</tr>

<tr style="">

<td>City</td>

<td ><div id="citydiv"><select name="city">

<option>Select State First</option>

</select></div></td>

</tr>

<tr>

<td>&nbsp;</td>

<td>&nbsp;</td>

</tr>

<tr>

<td>&nbsp;</td>

<td>&nbsp;</td>

</tr>

</table>

</form>

</body>

</html>




خطا

Notice: Undefined variabale: row in C:\**********\findState.php on line 27 > Notice: Undefined variabale: row in C:\**********\findState.php on line 28

119534

abolfazl-z
دوشنبه 05 خرداد 1393, 20:56 عصر
تابع mysql_fetch_array در فایل findState.php on line 27 را به تابع mysql_fetch_assoc تغییر دهید.

دلیل : تابع mysql_fetch_array خروجی رو بصورت یک آرایه ساده با اندیس عددی بر میگرداند ولی تابع mysql_fetch_assoc خروجی رو بصورت یک آرایه انجمنی بر میگرداند !

hamidhassas
دوشنبه 05 خرداد 1393, 21:08 عصر
جواب نداد میشه فایل ضمیمه رو دانلود و تست و مشکل رو برطرف کنید

abolfazl-z
دوشنبه 05 خرداد 1393, 21:19 عصر
من متوجه نشدم در خط ذیل قبل از row آن مساوی برای چی هست ؟


<option value=<?=$row['id']?>><?=$row['statename']?></option>

hamidhassas
دوشنبه 05 خرداد 1393, 23:21 عصر
statename اسم فیلد state در دیتابیس هست که گفته چاپ بشه

abolfazl-z
سه شنبه 06 خرداد 1393, 01:15 صبح
بفرمایید.

مشکل شما این بود که از ?> استفاده میکردید !