homepage Welcome to WebmasterWorld Guest from 54.211.219.178
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
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));
?>

 

calabar




msg:4663507
 1:35 pm on Apr 16, 2014 (gmt 0)

Is this mysqli used below correct


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

?>

Fotiman




msg:4663508
 1:37 pm on Apr 16, 2014 (gmt 0)


I got syntax error } at end of while loop

I take it you fixed that.

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

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

As long as you have COUNTRYCOD defined.

calabar




msg:4663512
 1:40 pm on Apr 16, 2014 (gmt 0)

Is this mysqli used below correct


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

?>

Fotiman




msg:4663517
 1:48 pm on Apr 16, 2014 (gmt 0)


Is this mysqli used below correct

I think so, but you probably also want to be doing this after your while loop:

$FCOUNTRY->free();
$mysqli->close();

calabar




msg:4664154
 1:44 pm on Apr 18, 2014 (gmt 0)

I went to test AJAX CHATTING Routine because I put
PDO and mysqli in GETCOUNTIRES.PHP AND GETSTATE.PHP
and now it is not loading DropDown List. I do not think
PDO and Mysqli is activated in php.ini.

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