Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2010
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    AJAX (Country, State, City ) DropDown List problem when tested in Console

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

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 Posts
    You're making your POST data complicated. Try changing this:

    Code:
    data : { MYDATA: POSTEDDATA},
    to:

    Code:
    data : POSTEDDATA,
    Then in PHP, change this:

    PHP Code:
    $DATA $_POST['MYDATA'];
    $DATADECODE json_decode$DATA );
    $COUNTRYCOD $DATADECODE["COUNTRYCOD"]; 
    to:

    PHP Code:
    $COUNTRYCOD $_POST["COUNTRYCOD"]; 
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    New Coder
    Join Date
    Jul 2010
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you for your response, but first the Country DropDown List is not even Loaded.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •