Enjoy an ad free experience by logging in. Not a member yet?
Register .
10-12-2012, 01:23 PM
PM User |
#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 ..
10-12-2012, 03:35 PM
PM User |
#2
Senior Coder
Join Date: Jan 2011
Location: Missouri
Posts: 2,392
Thanks: 18
Thanked 351 Times in 350 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".
10-12-2012, 04:32 PM
PM User |
#3
Regular Coder
Join Date: Sep 2010
Location: London
Posts: 961
Thanks: 0
Thanked 198 Times in 193 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?
10-12-2012, 05:31 PM
PM User |
#4
New Coder
Join Date: Sep 2012
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by
sunfighter
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>
10-12-2012, 05:34 PM
PM User |
#5
New Coder
Join Date: Sep 2012
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by
Logic Ali
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.
10-12-2012, 06:13 PM
PM User |
#6
Regular Coder
Join Date: Sep 2010
Location: London
Posts: 961
Thanks: 0
Thanked 198 Times in 193 Posts
Quote:
Originally Posted by
jawittdesigns
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?
10-12-2012, 06:18 PM
PM User |
#7
New Coder
Join Date: Sep 2012
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by
Logic Ali
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.
10-12-2012, 06:25 PM
PM User |
#8
Regular Coder
Join Date: Sep 2010
Location: London
Posts: 961
Thanks: 0
Thanked 198 Times in 193 Posts
Quote:
Originally Posted by
jawittdesigns
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.
10-12-2012, 06:31 PM
PM User |
#9
New Coder
Join Date: Sep 2012
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by
Logic Ali
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-12-2012, 06:41 PM
PM User |
#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
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>' ;
10-12-2012, 08:00 PM
PM User |
#11
Regular Coder
Join Date: Sep 2010
Location: London
Posts: 961
Thanks: 0
Thanked 198 Times in 193 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();
}
10-12-2012, 09:06 PM
PM User |
#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);
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 01:57 AM .
Advertisement
Log in to turn off these ads.