homepage Welcome to WebmasterWorld Guest from 54.211.95.201
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

This 35 message thread spans 2 pages: 35 ( [1] 2 > >     
Error with ajax (Country, State, City) DropDown List
calabar




msg:4658038
 6:33 pm on Mar 27, 2014 (gmt 0)

When I tested this Code (Country, State, City Dropdown List) in the console, I clicked
the Country DropDown List , selected Network in the Console and the program GETSTATE.PHP
pop up. I then clicked GETSTATE.PHP and got the error

json_decode() expects parameter 1 to be a string, Array given.

Incidentally , the Country DropDown List was not loaded.
#########################################################################################

This is an AJAX Country, State, City DropDown List

######################################################################################
######################################################################################

DROPDOWN.JS

$(document).ready(function(){
var COUNTRYCOD = 0;
$.ajax({
type : "POST",
url : "http://localhost/ajaxphpmysql/GETCOUNTRIES.PHP",
dataType : "json",
success : function(COUNTRY){
var CBOCONTROL = $("#COUNTRYCBO");
CBOCONTROL.empty();
for ( X = 0; X < COUNTRY.length;x++){
CBOCONTROL.append("<OPTION VALUE=" + COUNTRY[X].COUNTRYCOD + ">" + COUNTRY[X].COUNTRYNAM + "</OPTION>");
};
}
});

$("#COUNTRYCBO").click ( function(){
var COUNTRYCOD = $("#COUNTRYCBO").val();
POSTEDDATA = { "COUNTRYCOD" : COUNTRYCOD };

$.ajax({
type : "POST",
url : "http://localhost/ajaxphpmysql/GETSTATE.PHP",
data : { MYDATA: POSTEDDATA},
dataType: "json",
success : function(STATES){
var CBOCONTROL = $("#STATECBO");
CBOCONTROL.empty();
for ( N = 0; N < STATES.length; n++){
CBOCONTROL.Append($("<OPTION></OPTION>").attr("value",STATES[N].STATECOD).text(STATES[N].STATENAM));
};

}
});
});
$("#STATECBO").click ( function(){
$STATECOD = $("#STATECBO").val();
POSTEDDATA = {
"COUNTRYCOD" : COUNTRYCOD ,
"STATECOD" : STATECOD
}
$.ajax({
type : "POST",
url : "http://localhost/ajaxphpmysql/GETCITIES.PHP",
data : { MYDATA : POSTEDDATA },
dataType: "json",
success : function(CITIES){
var CBOCONTROL = $("#CITYCBO");
CBOCONTROL.empty();
for ( N = 0; N < CITIES.length; N++ ){
CBOCONTROL.Append($("<OPTION></OPTION>").attr("value",CITIES[N].CITYCOD).text(CITIES[N].CITYNAM));
};
}
});
});
});


##############################################################################################
##############################################################################################




<HTML>
<HEAD>
<STYLE>
#SRCHORDERS{
position : absolute;
top : 100px;
left : 20px;
width : 100px;


}

</STYLE>
</HEAD>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<SCRIPT src="DROPDOWN.JS"> </SCRIPT>

<DIV ID="SRCHORDERS">
<TABLE>
<TR>
<TD>
<SELECT NAME="DDCOUNTRY" ID="COUNTRYCBO">


</SELECT>
</TD>
</TR>
<TR>
<TD>
<SELECT NAME="DDSTATE" ID="STATECBO">


</SELECT>
</TD>
</TR>
<TR>
<TD>

<SELECT NAME="DDCITY" ID="CITYCBO">



</SELECT>
</TD>
</TR>
</TABLE>
</DIV>





#####################################################################
#####################################################################

GETCOUNTRIES.PHP

<?php
header('Content-Type : application/json');
$CON = mysql_connect("localhost","root","");
mysql_select_db ( "AJAXDBF", $CON );
$SQLCOUNTRY = " SELECT * FROM COUNTRY ";
$FCOUNTRY = mysql_query( $SQLCOUNTRY , $CON);
while ( $ROW = mysql_fetch_array( $FCOUNTRY )){
$COUNTRIES = array(
'COUNTRYCOD' => $ROW["COUNTRYCOD"],
'COUNTRYNAM' => $ROW["COUNTRYNAM"]);
}
echo ( json_encode( $COUNTRIES ));

?>

#############################################################################
#############################################################################

GETSTATE.PHP
<?php
header ('Content-Type : Application/json');
$DATA = $_POST['MYDATA'];
$DATADECODE = json_decode( $DATA );
$COUNTRYCOD = $DATADECODE["COUNTRYCOD"];
$CON = mysql_connect("localhost","root","");
mysql_select_db ( "AJAXDBF", $CON );
$SQLSTATE = " SELECT * FROM STATES WHERE COUNTRYCOD=" . $COUNTRYCOD ;
$FSTATE = mysql_query ( $SQLSTATE, $CON );
while ( $ROW = mysql_fetch_array($FSTATE)){
$STATES = array(
'STATECOD' => $ROW["STATECOD"],
'STATENAM' => $ROW["STATENAM"]);

}
echo(json_encode($STATES));
?>

#################################################################################
##################################################################################

GETCITIES.PHP

<?php
header ('Content-Type : Application/json');
$DATA = $_POST['MYDATA'];
$DATADECODE = json_decode( $DATA);
$COUNTRYCOD = $DATADECODE["COUNTRYCOD"];
$STATECOD = $DATADECODE["STATECOD"];
$CON = mysql_connect("localhost", "root","");
mysql_select_db( "AJAXDBF", $CON );
$SQLCITY = " SELECT * FROM CITY WHERE COUNTRYCOD =" . $COUNTRYCOD ;
$SQLCITY.= " WHERE STATECOD =" . $STATECOD ;
$FCITY = mysql_query( $SQLCITY, $CON ) ;
while ( $ROW = mysql_fetch_array( $FCITY )){
$CITIES = array(
'CITYCOD'=> $ROW["CITYCOD"],
'CITYNAM'=> $ROW["CITYNAM"])
}
echo(json_encode($CITIES));
?>

 

Fotiman




msg:4658044
 6:53 pm on Mar 27, 2014 (gmt 0)


while ( $ROW = mysql_fetch_array( $FCOUNTRY )){
$COUNTRIES = array(
'COUNTRYCOD' => $ROW["COUNTRYCOD"],
'COUNTRYNAM' => $ROW["COUNTRYNAM"]);
}
echo ( json_encode( $COUNTRIES ));

Each time through the loop, you're re-assigning an array containing the COUNTRYCOD and COUNTRYNAME to $COUNTRIES, so in the end $COUNTRIES is going to be an array containing the COUNTRYCOD and COUNTRYNAME for a single country (as opposed to an array of arrays). So I suspect that logic is wrong. I think you'll end up with something like this:
{"COUNTRYCOD":COD Value,"COUNTRYNAM":Country Name Value}

I'd start there.

calabar




msg:4658077
 10:40 pm on Mar 27, 2014 (gmt 0)

It is a 2 dimensional associate array.

Fotiman




msg:4658084
 10:55 pm on Mar 27, 2014 (gmt 0)

I know it's supposed to be a 2 dimensional associative array, but that's not what it is. You're only going to return the very last country.

calabar




msg:4658303
 4:58 pm on Mar 28, 2014 (gmt 0)

$COUNTRIES[] is causing only the first Country to load.

<?php
header('Content-Type : application/json');
$CON = mysql_connect("localhost","root","");
mysql_select_db ( "AJAXDBF", $CON );
$SQLCOUNTRY = " SELECT * FROM COUNTRY ";
$FCOUNTRY = mysql_query( $SQLCOUNTRY , $CON);
while ( $ROW = mysql_fetch_array( $FCOUNTRY )){
$COUNTRIES[] = array(
'COUNTRYCOD' => $ROW["COUNTRYCOD"],
'COUNTRYNAM' => $ROW["COUNTRYNAM"]);

}
echo ( json_encode( $COUNTRIES ));

?>

Fotiman




msg:4658304
 5:10 pm on Mar 28, 2014 (gmt 0)


while ( $ROW = mysql_fetch_array( $FCOUNTRY )){
$COUNTRIES[] = array(
'COUNTRYCOD' => $ROW["COUNTRYCOD"],
'COUNTRYNAM' => $ROW["COUNTRYNAM"]);
}

Needs to be something like this:

while ( $ROW = mysql_fetch_array( $FCOUNTRY )){
$ROW_ARRAY = array(
'COUNTRYCOD' => $ROW["COUNTRYCOD"],
'COUNTRYNAM' => $ROW["COUNTRYNAM"]
);
array_push($COUNTRIES, $ROW_ARRAY);
}

calabar




msg:4658311
 6:36 pm on Mar 28, 2014 (gmt 0)

I got just the first country loaded. However, when I clicked the country and GETSTATE.PHP appeared under console Network, when I clicked on the PHP i got the error
Undefined Index "MYDATA"

<?php
header('Content-Type : application/json');
$CON = mysql_connect("localhost","root","");
mysql_select_db ( "AJAXDBF", $CON );
$SQLCOUNTRY = " SELECT * FROM COUNTRY ";
$FCOUNTRY = mysql_query( $SQLCOUNTRY , $CON);
$COUNTRIES = Array();
while ( $ROW = mysql_fetch_array( $FCOUNTRY )){
$ROW_ARRAY = array(
'COUNTRYCOD' => $ROW["COUNTRYCOD"],
'COUNTRYNAM' => $ROW["COUNTRYNAM"]
);
array_push($COUNTRIES, $ROW_ARRAY);
}
echo ( json_encode( $COUNTRIES ));

?>

calabar




msg:4658594
 2:48 pm on Mar 30, 2014 (gmt 0)

I got warning that json_decode expects parameter 1, to be
a string, array given.

<?php
header ('Content-Type : Application/json');
$DATA = $_POST['MYDATA'];
$DATADECODE = json_decode( $DATA );
$COUNTRYCOD = $DATADECODE["COUNTRYCOD"];
$CON = mysql_connect("localhost","root","");
mysql_select_db ( "AJAXDBF", $CON );
$SQLSTATE = " SELECT * FROM STATES WHERE COUNTRYCOD=" . $COUNTRYCOD ;
$FSTATE = mysql_query ( $SQLSTATE, $CON );
$STATES = array();
while ( $ROW = mysql_fetch_array($FSTATE)){
$ROW_ARRAY= array(
'STATECOD' => $ROW["STATECOD"],
'STATENAM' => $ROW["STATENAM"]
);
array_push($STATES , $ROW_ARRAY );
}
echo(json_encode($STATES));
?>

Fotiman




msg:4658837
 2:03 pm on Mar 31, 2014 (gmt 0)

Maybe something like this:
echo(json_encode(implode(",", $STATES)));

calabar




msg:4658862
 3:24 pm on Mar 31, 2014 (gmt 0)


I was speaking of json_decode. Is this code below correct
in receiving JSON with PHP when sent from AJAX.


header ('Content-Type : Application/json');
$DATA = $_POST['MYDATA'];
$DATADECODE = json_decode( $DATA );
$COUNTRYCOD = $DATADECODE["COUNTRYCOD"];

Fotiman




msg:4658883
 4:03 pm on Mar 31, 2014 (gmt 0)

Ah. In that case, I'd start by examining the contents of $DATA. I suspect, though, that in your AJAX call, you might need to change this:
data : { MYDATA : POSTEDDATA },
to this:
data : { "MYDATA" : POSTEDDATA },

calabar




msg:4658917
 6:03 pm on Mar 31, 2014 (gmt 0)


I do not know how to examine the contents of $DATA since an
echo ( $data ) will send the value to AJAX

Fotiman




msg:4658924
 6:24 pm on Mar 31, 2014 (gmt 0)

You can use browser developer tools to examine the response, even in AJAX. For example, in Chrome open the developer tools (press F12), click on the Network tab and make sure "All" is the filter selected. When you see the request in the list, click on it and then click on the "Response" tab.

calabar




msg:4660180
 9:45 pm on Apr 3, 2014 (gmt 0)

I used the console and did not understand what I saw when
clicking Network , GETSTATE.PHP, AND Response. However I run
GETCOUNTRIES.PHP and it returned the two countries
U.S.A and CANADA, while the Country DropDown List shows only
U.S.A . I tried to make the JSON an object with
jQuery.parseJSON() AS SEEN IN CODE BELOW



$.ajax({
type : "POST",
url : "http://localhost/ajaxphpmysql/GETCOUNTRIES.PHP",
dataType : "json",
success : function(COUNTRY){
COUNTRY = jQuery.parseJSON(COUNTRY);
var CBOCONTROL = $("#COUNTRYCBO");
CBOCONTROL.empty();
for ( X = 0; X < COUNTRY.length;x++){
CBOCONTROL.append("<OPTION VALUE=" + COUNTRY[X].COUNTRYCOD + ">" + COUNTRY[X].COUNTRYNAM + "</OPTION>");
};
}
});

Fotiman




msg:4660426
 3:52 pm on Apr 4, 2014 (gmt 0)

jQuery.parseJSON will return an object. You're treating the result as if it's an array, but the array is probably a property of the object. If you do this, what does the output look like in Chrome's JavaScript console:

COUNTRY = jQuery.parseJSON(COUNTRY);
console.log(COUNTRY);

calabar




msg:4662342
 3:49 pm on Apr 11, 2014 (gmt 0)

I got error cannot use object as array

$COUNTRYCOD = $DATA["COUNTRYCOD"];



$("#COUNTRYCBO").click ( function(){
var COUNTRYCOD = $("#COUNTRYCBO").val();
POSTEDDATA = { "COUNTRYCOD" : COUNTRYCOD };

$.ajax({
type : "POST",
url : "http://localhost/ajaxphpmysql/GETSTATE.PHP",
data : { "MYDATA" : JSON.stringify(POSTEDDATA )},
dataType: "json",
success : function(STATES){
var CBOCONTROL = $("#STATECBO");
CBOCONTROL.empty();
for ( N = 0; N < STATES.length; n++){
CBOCONTROL.Append($("<OPTION></OPTION>").attr("value",STATES[N].STATECOD).text(STATES[N].STATENAM));
}

}
});
});




######################################################
######################################################
GETSTATE.PHP

<?php
header ('Content-Type : application/json');
$DATA = json_decode($_POST['MYDATA']);
$COUNTRYCOD = $DATA["COUNTRYCOD"];
$CON = mysql_connect("localhost","root","");
mysql_select_db ( "AJAXDBF", $CON );
$SQLSTATE = " SELECT * FROM STATES WHERE COUNTRYCOD=" . $COUNTRYCOD ;
$FSTATE = mysql_query ( $SQLSTATE, $CON );
$STATES = array();
while ( $ROW = mysql_fetch_array($FSTATE)){
$ROW_ARRAY= array(
'STATECOD' => $ROW["STATECOD"],
'STATENAM' => $ROW["STATENAM"]
);
array_push($STATES , $ROW_ARRAY );
}
echo(json_encode($STATES));
?>

Fotiman




msg:4662368
 5:32 pm on Apr 11, 2014 (gmt 0)

But what do you see in the console log?

calabar




msg:4662393
 8:16 pm on Apr 11, 2014 (gmt 0)

when GETSTATE.PHP was called , the console gave that message.
look at statement after json_decode() in GETSTATE.PHP .

Fotiman




msg:4662400
 8:25 pm on Apr 11, 2014 (gmt 0)

Ok, you're jumping ahead... you asked about only seeing 1 country in the dropdown... that's what I was trying to help you with. In other words, before you choose a country, what were you seeing if you added the console.log message in the GETCOUNTRIES.PHP request? Are you past that issue now?

calabar




msg:4662571
 12:58 pm on Apr 12, 2014 (gmt 0)

Yes the countries are loading in the Country DropDown
List.

Fotiman




msg:4662611
 6:12 pm on Apr 12, 2014 (gmt 0)

I *think* in your PHP code, you need to do this:


$DATA = json_decode($_POST['MYDATA'], true);


That "true" will cause the returned object to be converted to an associative array.

calabar




msg:4663268
 5:42 pm on Apr 15, 2014 (gmt 0)

got message, warning mysql_fetch_array() expects parameter 1
to be resource, boolean given.

GETSTATE.PHP

<?php
header ('Content-Type : application/json');
$DATA = json_decode($_POST['MYDATA'], true);
$COUNTRYCOD = $DATA["COUNTRYCOD"];
$CON = mysql_connect("localhost","root","");
mysql_select_db ( "AJAXDBF", $CON );
$SQLSTATE = " SELECT * FROM STATES WHERE COUNTRYCOD=" . $COUNTRYCOD ;
$FSTATE = mysql_query ( $SQLSTATE, $CON );
$STATES = array();
while ( $ROW = mysql_fetch_array($FSTATE)){
$ROW_ARRAY= array(
'STATECOD' => $ROW["STATECOD"],
'STATENAM' => $ROW["STATENAM"]
);
array_push($STATES , $ROW_ARRAY );
}
echo(json_encode($STATES));
?>

Fotiman




msg:4663275
 6:05 pm on Apr 15, 2014 (gmt 0)

[au1.php.net...]

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.


So you'll want to investigate what your $SQLSTATE looks like (do you need quotes around $COUNTRYCOD)?

Also note, from that same page:

mysql_query Send a MySQL query

Warning
This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:
- mysqli_query()
- PDO::query()

calabar




msg:4663469
 12:02 pm on Apr 16, 2014 (gmt 0)

I know about Mysqli and PDO , I JUST need help in
activating them in php.ini . I do not need quotes around
$COUNTRYCOD.

calabar




msg:4663477
 12:09 pm on Apr 16, 2014 (gmt 0)

how do I show what is in $DATA["COUNTRYCOD"]

calabar




msg:4663487
 12:34 pm on Apr 16, 2014 (gmt 0)

I GOT ONE STATE SHOWING IN STATE DROPDOWN LIST

Fotiman




msg:4663490
 12:59 pm on Apr 16, 2014 (gmt 0)

I can't tell if your ALL CAPS is meant to be shouting.

Does that mean you got your query working?

calabar




msg:4663492
 1:03 pm on Apr 16, 2014 (gmt 0)

I got all states loaded in state dropdown list;

calabar




msg:4663493
 1:08 pm on Apr 16, 2014 (gmt 0)


I got syntax error } at end of while loop

GETCITIES.PHP

<?php
header ('Content-Type : application/json');
$DATA = json_decode( $_POST['MYDATA'], true );
$COUNTRYCOD = $DATA["COUNTRYCOD"];
$STATECOD = $DATA["STATECOD"];
$CON = mysql_connect("localhost", "root","");
mysql_select_db( "AJAXDBF", $CON );
$SQLCITY = " SELECT * FROM CITY WHERE COUNTRYCOD =" . $COUNTRYCOD ;
$SQLCITY.= " AND STATECOD =" . $STATECOD ;
$FCITY = mysql_query( $SQLCITY, $CON ) ;
while ( $ROW = mysql_fetch_array( $FCITY )){
$CITIES[]= array(
'CITYCOD'=> $ROW["CITYCOD"],
'CITYNAM'=> $ROW["CITYNAM"]
)
}
echo(json_encode($CITIES));
?>

calabar




msg:4663494
 1:18 pm on Apr 16, 2014 (gmt 0)

Is this Correct since I am expecting $COUNTRYCOD to be
global variable.

var STATECOD = $("#STATECBO").val();
POSTEDDATA = {
"COUNTRYCOD" : COUNTRYCOD ,
"STATECOD" : STATECOD
}





$("#STATECBO").click ( function(){
var STATECOD = $("#STATECBO").val();
POSTEDDATA = {
"COUNTRYCOD" : COUNTRYCOD ,
"STATECOD" : STATECOD
}
$.ajax({
type : "POST",
url : "http://localhost/ajaxphpmysql/GETCITIES.PHP",
data : { "MYDATA" : JSON.stringify( POSTEDDATA ) },
dataType: "json",
success : function(CITIES){
var CBOCONTROL = $("#CITYCBO");
CBOCONTROL.empty();
var N = 0;
for ( N = 0; N < CITIES.length; N++ ){
CBOCONTROL.append($("<OPTION></OPTION>").attr("value",CITIES[N].CITYCOD).text(CITIES[N].CITYNAM));
};
}
});
});

This 35 message thread spans 2 pages: 35 ( [1] 2 > >
Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved