I want to pass the value of a form select input from an HTML form to a php file without submitting it.
How may I do that?
This is my form:
<div class="form-group">
<label>Provincia</label>
<select class="form-control" name="userDomicilioProvincia">
<option id ="userDomicilioProvincia" name="userDomicilioProvincia"> </option>
<option value="Capital Federal">Capital Federal</option>
<option value="Gran Buenos Aires">Gran Buenos Aires</option>
<option value="Buenos Aires">Provincia de Buenos Aires</option>
</select>
</div>
And here's my ajax call:
$('#userDomicilioProvincia').change(function(){
$.ajax({
type: "POST",
url: "/ubicacion.php",
provincia: {text:$(this).val()}
});
});
I'm trying to test this using the browser's console, I got into the form, select one option and in the console I type var Provincia and it returns undefined.
How may I do that?
$("#userDomicilioProvincia")means select the element with attributeid="userDomicilioProvincia ". If you want to get your select element that have the name attribute set, use this selector :$('select[name="userDomicilioProvincia"]')idattribute to theselectelement rather than using the (select) tag to find the element. Theidselector is MUCH faster. You can read something about selector performance here