Welcome to WebmasterWorld Guest from 54.145.120.69

Forum Moderators: open

Message Too Old, No Replies

Error with ajax (Country, State, City) DropDown List

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

New User

joined:Mar 27, 2014
posts: 32
votes: 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));
?>
6:53 pm on Mar 27, 2014 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts: 5007
votes: 21



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.
10:40 pm on Mar 27, 2014 (gmt 0)

New User

joined:Mar 27, 2014
posts: 32
votes: 0


It is a 2 dimensional associate array.
10:55 pm on Mar 27, 2014 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts: 5007
votes: 21


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.
4:58 pm on Mar 28, 2014 (gmt 0)

New User

joined:Mar 27, 2014
posts: 32
votes: 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 ));

?>
5:10 pm on Mar 28, 2014 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts: 5007
votes: 21



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);
}
6:36 pm on Mar 28, 2014 (gmt 0)

New User

joined:Mar 27, 2014
posts: 32
votes: 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 ));

?>
2:48 pm on Mar 30, 2014 (gmt 0)

New User

joined:Mar 27, 2014
posts: 32
votes: 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));
?>
2:03 pm on Mar 31, 2014 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts: 5007
votes: 21


Maybe something like this:
echo(json_encode(implode(",", $STATES)));
3:24 pm on Mar 31, 2014 (gmt 0)

New User

joined:Mar 27, 2014
posts: 32
votes: 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"];
4:03 pm on Mar 31, 2014 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts: 5007
votes: 21


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 },
6:03 pm on Mar 31, 2014 (gmt 0)

New User

joined:Mar 27, 2014
posts: 32
votes: 0



I do not know how to examine the contents of $DATA since an
echo ( $data ) will send the value to AJAX
6:24 pm on Mar 31, 2014 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts: 5007
votes: 21


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.
9:45 pm on Apr 3, 2014 (gmt 0)

New User

joined:Mar 27, 2014
posts: 32
votes: 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>");
};
}
});
3:52 pm on Apr 4, 2014 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts: 5007
votes: 21


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);
3:49 pm on Apr 11, 2014 (gmt 0)

New User

joined:Mar 27, 2014
posts: 32
votes: 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));
?>
5:32 pm on Apr 11, 2014 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts: 5007
votes: 21


But what do you see in the console log?
8:16 pm on Apr 11, 2014 (gmt 0)

New User

joined:Mar 27, 2014
posts: 32
votes: 0


when GETSTATE.PHP was called , the console gave that message.
look at statement after json_decode() in GETSTATE.PHP .
8:25 pm on Apr 11, 2014 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts: 5007
votes: 21


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?
12:58 pm on Apr 12, 2014 (gmt 0)

New User

joined:Mar 27, 2014
posts: 32
votes: 0


Yes the countries are loading in the Country DropDown
List.
6:12 pm on Apr 12, 2014 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts: 5007
votes: 21


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.
5:42 pm on Apr 15, 2014 (gmt 0)

New User

joined:Mar 27, 2014
posts: 32
votes: 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));
?>
6:05 pm on Apr 15, 2014 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts: 5007
votes: 21


[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()
12:02 pm on Apr 16, 2014 (gmt 0)

New User

joined:Mar 27, 2014
posts: 32
votes: 0


I know about Mysqli and PDO , I JUST need help in
activating them in php.ini . I do not need quotes around
$COUNTRYCOD.
12:09 pm on Apr 16, 2014 (gmt 0)

New User

joined:Mar 27, 2014
posts: 32
votes: 0


how do I show what is in $DATA["COUNTRYCOD"]
12:34 pm on Apr 16, 2014 (gmt 0)

New User

joined:Mar 27, 2014
posts: 32
votes: 0


I GOT ONE STATE SHOWING IN STATE DROPDOWN LIST
12:59 pm on Apr 16, 2014 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts: 5007
votes: 21


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

Does that mean you got your query working?
1:03 pm on Apr 16, 2014 (gmt 0)

New User

joined:Mar 27, 2014
posts: 32
votes: 0


I got all states loaded in state dropdown list;
1:08 pm on Apr 16, 2014 (gmt 0)

New User

joined:Mar 27, 2014
posts: 32
votes: 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));
?>
1:18 pm on Apr 16, 2014 (gmt 0)

New User

joined:Mar 27, 2014
posts: 32
votes: 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