2

When I select values from two different select I get an error tmp is null

<form method="post" id="filterform">
       <select multiple name="category[]" id="category" class="select">
           <option value="10">Men Foreigner</option>
           <option value="11">Women Foreigner</option>
        </select>

        <select multiple name="hair[]" id="hair" class="select">
           <option value="Black">Black</option>
           <option value="Blond">Blond</option>
        </select>
</form>

jQuery part

   $("select.select").change( function() {
        var Thedata = $("#filterform").serialize();
            console.log(Thedata);

        $.ajax({
            data: Thedata,
            type: 'POST',
            url: 'autocomplete.php?cat=filtermodelscall',
            })
            .done(function( html ) {
                $("tbody").html( html );
            });
    });

Log shows Thedata values which is category%5B%5D=10&hair%5B%5D=Black I have no idea where come from this error. Also, when I select values from a single select, it is working. Thank you for your help

3
  • it comes from [] category[] why you don't use just name="category" Commented Nov 22, 2015 at 5:14
  • Because it is a multiple select. if name="category[]" is not an array and that I select multiple option in the same select, I am not able to retrieve all values in ajax Commented Nov 22, 2015 at 5:20
  • Where is "error tmp is null" error coming from in that code? Commented Nov 25, 2015 at 13:21

1 Answer 1

1

The serialize() method creates a text string in standard URL-encoded notation.

When a text convert to encoded URL:

[ convert to %5B

] convert to %5D

You need to decode encoded url using decodeURIComponent() JS function like this:

$("select.select").change( function() {
    var Thedata = $("#filterform").serialize();
    console.log(decodeURIComponent(Thedata));          
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post" id="filterform">
       <select multiple name="category[]" id="category" class="select">
           <option value="10">Men Foreigner</option>
           <option value="11">Women Foreigner</option>
        </select>

        <select multiple name="hair[]" id="hair" class="select">
           <option value="Black">Black</option>
           <option value="Blond">Blond</option>
        </select>
</form>

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.