Forum Moderators: open

Message Too Old, No Replies

I cannot load Country DropDown List with countries

         

calabar

5:53 pm on Apr 10, 2014 (gmt 0)

10+ Year Member



$(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 ( N = 0; N < COUNTRY.length;N++){
CBOCONTROL.Append("<OPTION VALUE=" + COUNTRY[N].COUNTRYCOD + ">" + COUNTRY[N].COUNTRYNAM + "</OPTION>");
}
}
});
});

########################################################
########################################################
GETCOUNTRIES.PHP

<?php

$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 ));

?>

Fotiman

6:02 pm on Apr 10, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



jQuery.ajax, when the dataType is set to json, is expecting a JSON string that it will convert to an object and pass that object to your success callback. If you do this, what does it show in the console log?


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

calabar

7:25 pm on Apr 10, 2014 (gmt 0)

10+ Year Member



I got the Countries loaded in the dropdown list.
but in GETSTATES.PHP an error says $SQLSTATE could not be
converted to string.

<?php
header ('Content-Type : application/json');
$COUNTRYCOD = json_decode($_POST['MYDATA']);
$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

7:51 pm on Apr 10, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



$COUNTRYCOD = json_decode($_POST['MYDATA']);

json_decode takes a string and converts it to an object.

$SQLSTATE = " SELECT * FROM STATES WHERE COUNTRYCOD=" . $COUNTRYCOD ;

You're passing an object where you need a string.