Wes_Dunn77
Wes_Dunn77

Reputation: 61

Jquery OnChange fires but no data returned

I have posted this a few times looking for some help, I cant seem to work out why i dont get any data from my dynamic drop down but i do from the 2 static fields. I did get some answers but mainly saying that i need to sort out the security, which i hope to learn next before anything is live online, everything looks good in Firebug including the trace for the http request, i think its a problem with the query i am trying to run, i will post this again and see if anyone can help me out before i address the security flaws.

Thanks alot for helping me out.

first is html, the subcategory is the problem, all items are stored in a javascript array and works fine, just not the query

<select name="Category" id="Category" 
onchange="javascript: dropdownlist(this.options[this.selectedIndex].value);">
<option value="">Select Category</option>

<select name="subcategory" id="subcategory">
<option value="">Select Sub-Category</option>
</select>

<script>

$(function() {
  $('#subcategory').change(function() {
        $('#subcategory').load('results.php', {value: $(this).val()});
  });
});

</script>

 $category=$_POST['Category'];
 $subcategory=$_POST['Subcategory'];
 $destination=$_POST['Destination'];


 $result = mysql_query("SELECT * FROM travel WHERE Category='$category' 
 AND Subcategory='$subcategory' AND Destination='$destination'")
 or die(mysql_error());


 $row = mysql_fetch_assoc( $result ) ;

echo to table (not posted as is working ok)

Upvotes: 1

Views: 430

Answers (2)

Konstantin Pereiaslov
Konstantin Pereiaslov

Reputation: 1814

You only pass to php data from "#subcategory" form, not from "#Category", and with another id than you use in php code. And and you don't have a code for "destination", so I don't know where it should be taken from. Also it's better to put your code in $(document).ready when using jquery. It should be like that:

   $(document).ready(function() {
      $('#subcategory').change(function() {
            $('#subcategory').load('results.php', {Subcategory: $(this).val(), Category: $("#Category").val() });
      });
    });

Upvotes: 0

Chris Pratt
Chris Pratt

Reputation: 239420

Where to even start? Your field name is 'subcategory'. You pass 'value' to results.php and you're attempting to retrieve 'Subcategory' form the $_POST array. You need to line all these names up.

I'm not sure whether the PHP there is the code for results.php or for whatever script the form is posted to, or are they the same? Regardless, whatever results.php needs, you need to include in the data you pass to load. For example, since you currently use 'value', you'd retrieve that via $_POST['value'], not $_POST['Subcategory'].

Upvotes: 1

Related Questions