javascript - Getting radio button value from $.(ajax) and passing it in a SQL Query -


i have 2 sets of radio buttons: 1 car brands , other 1 car models. selecting "audi" car brands fetch of car models brand name "audi" in database through $(ajax).

function fetchmodels() {   $.ajax({     type: "post",     url: "script/rent/models.php",     data: {datefrom: selecteddatefrom,             dateto: selecteddateto,             destination: selecteddestination,             brand: selectedbrand},      success: function(data) {       $("#model-wrapper").html(data);     }     } );} // fetchmodels end   /* whenever selected car brand changed, change value of    selectedbrand , run function fetchmodels(). */      $('input[type=radio][name=brand]').on('change', function() {         switch($(this).val()) {           case 'audi':             selectedbrand = "audi";             break;           case 'toyota':             selectedbrand = "toyota";             break;           case 'volkswagen':             selectedbrand = "volkswagen";             break;           case 'volvo':             selectedbrand = "volvo";             break;         }          fetchmodels();      }); 

my issue models.php can't value selected car brand. it's caused fact car brands changed dynamically (the same way car models do) depending on location selected.

in other words, while loop run in brands.php fetch car brands:

while ($row = mysqli_fetch_array($result)) {   $brand = $row['brand'];   echo "<input type=\"radio\" name=\"brand\" value=\"$brand\">" . $brand . "</input>"; } 

if add <input type="radio" name="brand"/> manually php-file works fine, can't values generated ones.

a gif explaining issue:

a gif explaining issue

audi , volkswagen radio buttons generated brands.php while test isn't.

based on image of your generated html linked, problem arises case statement you're using. generated values not present in case statement, no case selected, naturally selectedbrand not set. has nothing static vs dynamically generated elements.

as pointed out in comment, case statement superfluous anyway. replace selectedbrand = $(this).val(). selected value no matter is.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -