4

i'm having a problem sending the option selected with ajax.

Code:

$("#editarConta").on('submit',(function(e) {

        e.preventDefault();

        $.ajax({
            url: "includes/php/class_conta.php",
            type: "POST",
            data: new FormData(this),
            contentType: false,
            cache: false,
            processData:false,
            success: function(resultado){
                $("#accountEdit_result").html(resultado);
            }
        });

    }));

Sending this data i can't see what option was selected. Is any way i can send this data, plus the selected option like:

data: { new FormData(this) , optionSelected: $( "#linguagem_favorita option:selected" ).val() },

My select HTML code:

<select name="linguagem_favorita" id="linguagem_favorita" class="form-control">

If i send with my code i get NULL when i var_dump the variable

var_dump($_POST["linguagem_favorita"]);
6
  • What does new FormData(this) do? Commented Oct 14, 2015 at 14:12
  • 1
    send the form values for PHP Commented Oct 14, 2015 at 14:13
  • I can deduct that it serialize form to JSON string, but I need to look at the code to answer your question. What is printed for this code: console.log( $(this).serialize() ); ? Commented Oct 14, 2015 at 14:16
  • 1
    U wanna see the ajax or php file? Commented Oct 14, 2015 at 14:18
  • 1
    I don't have a constructor in JS, FormData send ALL values inside the form to php. Commented Oct 14, 2015 at 14:21

1 Answer 1

3

Append to the form data:

$("#editarConta").on('submit',(function(e) {

    e.preventDefault();

    var formData = new FormData(this);
    formData.append("optionSelected", $("#linguagem_favorita option:selected" ).val() );

    $.ajax({
        url: "includes/php/class_conta.php",
        type: "POST",
        data: formData,
        contentType: false,
        cache: false,
        processData:false,
        success: function(resultado){
            $("#accountEdit_result").html(resultado);
        }
    });

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

Comments

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.