0

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?

2
  • $("#userDomicilioProvincia")means select the element with attribute id="userDomicilioProvincia ". If you want to get your select element that have the name attribute set, use this selector : $('select[name="userDomicilioProvincia"]') Commented May 12, 2017 at 22:31
  • But it's better to set the id attribute to the select element rather than using the (select) tag to find the element. The id selector is MUCH faster. You can read something about selector performance here Commented May 13, 2017 at 9:06

1 Answer 1

3

You're almost there.

Put the id attribute on the select and instead of the option.

$(document).ready(function(){
    $('#userDomicilioProvincia').change(function(){
        $.ajax({
            type: "POST",
            url: "/ubicacion.php",
            data: { provincia: $(this).val() }
        });
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
    <label>Provincia</label>
    <select id="userDomicilioProvincia" class="form-control" name="userDomicilioProvincia">
        <option> </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>

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your reply!! How may I access that value in php? I'm trying with var_dump($_POST['provincia']); and it's null.
@Rosamunda sorry, there is an other mistake in the jQuery code. I corrected it. I changed provincia: {text:$(this).val()} to: data: { provincia: $(this).val() }. data is a javascript object which you send as POST to the server (PHP). Try again, var_dump($_POST['provincia']); should work now.

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.