0

I am working on using jQuery to submit a form without having to refresh a page which I am new at. So far i have everything working except i have a dynamic amount of dropdown menus which would be named loot[0], loot[1], etc. however i can not get the loot[] variable to pass through the jQuery.

Here is the dropdown

<select class="search" name="loot[]" id="loot[]">
    <option class="search" value="">--</option>';
        $stmt = $conn->query("SELECT * FROM re_tbllootlist WHERE active = 1 ORDER BY LootName");
            while($row = $stmt->fetch()){
                echo "<option class='search' value='".$row["LootKey"]."'>".$row["LootName"]."</option>";
            }
</select>

More dropdowns are added with jQuery which would create the loot[0], loot[1], etc.

Which then would be added to the post data in jQuery with this:

post_data = {
    'info_planet'       : $('select[name=planet]').val(), 
    'info_mob'      : $('input[name=mob]').val(),
    'info_x_axis'       : $('input[name=x_axis]').val(),
    'info_y_axis'       : $('input[name=y_axis]').val(),
    'info_description'      : $('textarea[name=description]').val(),
    'info_loot'     : $('select[name=loot[]]').val()
};

I have tried several things to get this to work however i can not get this to work.

4
  • Is that give some relief. <?php $stmt = $conn->query("SELECT * FROM re_tbllootlist WHERE active = 1 ORDER BY LootName"); while($row = $stmt->fetch()){ echo "<option class='search' value='".$row["LootKey"]."'>".$row["LootName"]."</option>"; } ?> Commented Nov 11, 2015 at 4:04
  • the whole file is surrounded by a php tag that is the the snippet that has the issue is all Commented Nov 11, 2015 at 4:07
  • Then the code must be. echo '<select class="search" name="loot[]" id="loot[]"> <option class="search" value="">--</option>'; $stmt = $conn->query("SELECT * FROM re_tbllootlist WHERE active = 1 ORDER BY LootName"); while($row = $stmt->fetch()){ echo "<option class='search' value='".$row["LootKey"]."'>".$row["LootName"]."</option>"; } echo '</select>'; Commented Nov 11, 2015 at 6:22
  • 1
    I know it is however i was trying to just include the problematic snippets because there is a lot of code to be included if i included everything. Commented Nov 11, 2015 at 18:37

1 Answer 1

1

jQuery cannot recognize this selector select[name=loot[]] because of [].
If you look in your developer console, you will see the following error:

Uncaught Error: Syntax error, unrecognized expression: select[name=loot[]]

Just enclose the name in quotes like this

$('select[name="loot[]"]').val()
Sign up to request clarification or add additional context in comments.

4 Comments

That seems to have fixed the issue with it not passing the variable through however it doesnt seem to have anything except loot[0]
@Adonai How do you use it?
@adonai If you have multiple select elements, you'll have to loop through each one and push its value onto an array that you'll post. If you want to select multiple options, just add multiple as an attribute to the select. Like this: <select multiple>
Oh i think whats needed is what @Terminus said. I have multiple select elements so id need to loop through them to make the array.

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.