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 12 of 12
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cascading form dropdown

    I trying to make a cascading dropdowns for a form

    this is the JS I have

    Code:
    function changeSecond(first){
    	var xmlhttp;
    	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
    		xmlhttp=new XMLHttpRequest();
    	} else {// code for IE6, IE5
    		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    	}
    	xmlhttp.onreadystatechange=function(){
    		if (xmlhttp.readyState==4 && xmlhttp.status==200){
    			var res=xmlhttp.responseText;
    			document.getElementById("second").innerHTML=res;
    		}
    	}
    	xmlhttp.open("GET","reservations-plane.php?first="+first,true);
    	xmlhttp.send();
    }
    When some one makes a selection it send the value to this code. then the code (from what I understand) is supposed to populate a dropdown with the php code from reservations-plane.php passing the value to it(var first).

    The value us being passed to the JS. I tested it with an alert, but it's passing the value to the code in reservations-plane.php and not populating the select on the form.
    Last edited by jawittdesigns; 10-12-2012 at 09:06 PM.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,225
    Thanks
    23
    Thanked 606 Times in 605 Posts
    What you have posted for code is just the calling part of an ajax request. All we can find out from this is that you are passing the variable 'first' to reservations-plane.php and that the return from this will display in a div(maybe a div???) with an ID of "second".

    What we really need to see is the file "reservations-plane.php" and maybe the div "second".

  • #3
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    The code ( if workng ) seems intended to 'populate' the element with ID "second", presumably with a string of the form

    "<option value='val1'><option 1</option><option value='val2'..."

    which is a hideous way populating a <select>, but should work.

    There are better ways to do this. Are you responsible for the PHP and the data it generates?

  • #4
    New Coder
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sunfighter View Post
    What we really need to see is the file "reservations-plane.php" and maybe the div "second".
    OK Here's the code.
    PHP Code:
    global $wpdb$page_url;
    $location$_GET["first"];
    echo 
    $location;
    $flight_sql "SELECT ".$location." FROM wp_flr_flights GROUP BY ".$location;
    $flight_result mysql_query($flight_sql);
    echo 
    '<select name="plane_type_choose">';
    while (
    $flt_plane_rows mysql_fetch_row($flight_result)) {
        
    $plain_id $flt_plane_rows["plane_id"];
        
    $plane_type $flt_plane_rows["plane_type"];
        echo 
    '<option value="'.$flt_plane_rows[0].'">'.$flt_plane_rows[0].'</option>';
    }
    echo 
    '</select>'
    and the second div
    Code:
    <div id="second"><select><option value=""></option></select></div>

  • #5
    New Coder
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Logic Ali View Post
    The code ( if workng ) seems intended to 'populate' the element with ID "second", presumably with a string of the form

    "<option value='val1'><option 1</option><option value='val2'..."

    which is a hideous way populating a <select>, but should work.

    There are better ways to do this. Are you responsible for the PHP and the data it generates?
    I agree it probably is a bad way of doing it, but this is for my work and I just need it to work, not to be pretty.

    If I had time i'd learn to do it the right way, but I have a deadline to make.

    I don't understand what you mean by "responsible" for the php.

  • #6
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by jawittdesigns View Post
    I agree it probably is a bad way of doing it, but this is for my work and I just need it to work, not to be pretty.

    If I had time i'd learn to do it the right way, but I have a deadline to make.

    I don't understand what you mean by "responsible" for the php.
    I just meant do you know how to modify it? You need to filter that GET parameter before you're site is attacked.

    You can test the PHP simply by loading the document with the right querystring parameter, so presumably it's OK.

    Do you get any error messages in the console?

  • #7
    New Coder
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Logic Ali View Post
    I just meant do you know how to modify it? You need to filter that GET parameter before you're site is attacked.

    You can test the PHP simply by loading the document with the right querystring parameter, so presumably it's OK.

    Do you get any error messages in the console?
    Oh, I haven added any sanitation yet. Right now I just want to get it working. Details come later.

    No, no errors.

  • #8
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by jawittdesigns View Post
    Oh, I haven added any sanitation yet. Right now I just want to get it working. Details come later.

    No, no errors.
    How is the code being triggered? I don't see any event handler.

  • #9
    New Coder
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Logic Ali View Post
    How is the code being triggered? I don't see any event handler.

    PHP Code:
    $flight_sql "SELECT DISTINCT * FROM wp_flr_flights";
    $flight_result mysql_query($flight_sql);
    echo 
    '<select name="location" id="select-location" onChange="changeSecond(this.value)">';
    echo 
    '<option value="">Select</option>';
    while (
    $flt_loc_rows mysql_fetch_assoc($flight_result)) {
        
    $loc_id $flt_loc_rows["location_id"];
        
    $loc_name $flt_loc_rows["location_name"];
        echo 
    '<option value="'.$loc_id.'">'.$loc_name.'</option>';
    }
    echo 
    '</select>'

  • #10
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    0
    Thanked 4 Times in 4 Posts
    On a side note, this code is vulnerable to sql injection. You need to escape the $location variable or use some type of parameterized query.

    http://en.wikipedia.org/wiki/SQL_injection

    Quote Originally Posted by jawittdesigns View Post
    OK Here's the code.
    PHP Code:
    global $wpdb$page_url;
    $location$_GET["first"];
    echo 
    $location;
    $flight_sql "SELECT ".$location." FROM wp_flr_flights GROUP BY ".$location;
    $flight_result mysql_query($flight_sql);
    echo 
    '<select name="plane_type_choose">';
    while (
    $flt_plane_rows mysql_fetch_row($flight_result)) {
        
    $plain_id $flt_plane_rows["plane_id"];
        
    $plane_type $flt_plane_rows["plane_type"];
        echo 
    '<option value="'.$flt_plane_rows[0].'">'.$flt_plane_rows[0].'</option>';
    }
    echo 
    '</select>'

  • #11
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Presumably you have checked that the PHP is responding as expected.

    If so I'd be doing some basic debugging like this:

    Code:
    function changeSecond(first){
            alert( "CALLED" );
    	var xmlhttp;
    	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
    		xmlhttp=new XMLHttpRequest();
    	} else {// code for IE6, IE5
    		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    	}
    	xmlhttp.onreadystatechange=function(){
    		if (xmlhttp.readyState==4 && xmlhttp.status==200){
                            alert( "DATA RETURNED" );
    			var res=xmlhttp.responseText;
    			document.getElementById("second").innerHTML=res;
    		}
    	}
    	xmlhttp.open("GET","reservations-plane.php?first="+first,true);
    	xmlhttp.send();
    }

  • #12
    New Coder
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I got this working now.

    It's something really stupid. This is a wordpress plugin and I forgot to add the extra wordpress stuff to the URL.

    Code:
    xmlhttp.open("GET","reservations-plane.php?first="+first,true);
    should have been

    Code:
    xmlhttp.open("GET","<?php echo WP_PLUGIN_URL .'/'. $plugin_folder ?>/admin/reservations-plane.php?first="+first,true);


  •  

    Tags for this Thread

    Posting Permissions

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