0

I have javascript code that fires once I select from first drop down:

$(document).ready(function(){
    $("#first-choice").change(function() {
        $("#second-choice").load("getter.php?choice=" + $("select#first-choice").filter(":selected").val());
    });
});

I think the problem lies when I create the getter.php extension... Which I use choice in my sql. Choice is defined in getter.php as:

$choice = $_GET['choice'];
$sth = $db->prepare("SELECT name, code FROM sets WHERE name='$choice'");
$sth->execute();
$choicecode = $sth->fetchColumn(1);

When the javascript change fires I get an empty drop down. So I tested by setting Choice equal to one first-choice options, instead of $_GET, and the second list populated correctly. So I'm assuming my error comes from my js by defining the selected first choice.

2
  • Is that all of getter.php? Where do you render the new select? It looks like there is no output from this file at all. Commented Nov 22, 2013 at 21:48
  • @MatthewRapati No, there is more for rendering, but I am 100% sure the error does not lie in getter.php, like I said I changed the value of "choice" to a known value instead of $_get and it worked fine Commented Nov 22, 2013 at 21:50

1 Answer 1

1

Try this one in your change function

$("#first-choice").change(function() {
    $("#second-choice").load("getter.php?choice=" + $(this).val();
});

or

$("#first-choice").change(function() {
    $("#second-choice").load("getter.php?choice=" + $("#first-choice option:selected").val();
});

or use find

$("#first-choice").change(function() {
    $("#second-choice").load("getter.php?choice=" + $("#first-choice").find(":selected").val();
});

Make sure you have entered the value in option's value attribute

<option value="test">test</option>
Sign up to request clarification or add additional context in comments.

6 Comments

The first option worked. I have never seen that before. So basically "this" can be used for the change function. IS there a way I can later define the first selected and selected options so i could plug them into a database?
$(this) will have the object of current element triggered like if you select 3rd option it will have a object of third option
So I just found an error, where that if there is a spacing in-between the selection name, the the $_get doesn't seem to work either...any way of fixing this?
is value and option text is same then you can select $(this).text() match in database by the option text
what about the $_GET error I'm having, it must be adding in something to account for the spaces in some of my selection names
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.