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?
    – MaxZoom
    Commented Oct 14, 2015 at 14:12
  • 1
    send the form values for PHP
    – mmarques
    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() ); ?
    – MaxZoom
    Commented Oct 14, 2015 at 14:16
  • 1
    U wanna see the ajax or php file?
    – mmarques
    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.
    – mmarques
    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);
        }
    });

}));
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.