WorksDone
WorksDone

Reputation: 3

How do I properly get the selected index of my select box

<script src="http://code.jquery.com/jquery-1.5.min.js"></script>
<script>
$(document).ready(function () {
var $article = null;

$('#category').change(function () {
var $categoryName = $("#category").attr('value');

 if ($article == null) {
 $article = $('<h4>Select a business you wish to view.</h4><select id="business" name="business" class="business"></select>').appendTo('.query');
 $("#business").load("php.php", {categoryName:$categoryName});
 }

 });

});
</script>

category is the id/class/name given to my select box, Each option is populated through php and is given a value="$row['colName']", I am trying to pass that selected value to a php document on change. So I can pass that value to the next queries WHERE clause.

Can you help me correct the

  var $categoryName = $("#category").attr('value');

line to get the selected index of #category and the value for w.e option is selected?

here is php.php

 <?php
    $con = mysqli_connect(,,,,);
    // Check connection
    $myVar = $_GET["categoryName"];
    if (mysqli_connect_errno())
       {
       echo "<option>Failed to connect to the Database</option>" ;
       }
     echo "<option>Select A Business To View Listing</option>";
     $result = mysqli_query($con,"SELECT BName, Category FROM Business WHERE Category=$myVar");
     while($row = mysqli_fetch_array($result)) {
      echo "<option class='".$row['BName']."'>".$row['BName']."</option>";
      }
     // Free result set
     mysqli_free_result($result);
     mysqli_close($con);
 ?>

Upvotes: 0

Views: 80

Answers (1)

slightlyfaulty
slightlyfaulty

Reputation: 1411

var $categoryName = $(this).val();

This is the correct way to get the current value of an input element with jQuery.

Upvotes: 1

Related Questions