2

So I'm trying to store the selected <option></option> tags into an array. I've searched everywhere and can't find any "Good" way of doing it.

<form action='most-liked.php'>
            <select multiple name='choose-category' style='display: inline-block;'>
            <?php
                $allCategories = ["All", "Sport", "Automotive", "Comedy", "Misc"];
                $htmlOutput = "";
                $i=0;
                    for($i;$i<sizeof($allCategories);$i++)
                    {
                        $htmlOutput .= "<option value='".$allCategories[$i]."'>".$allCategories[$i]."</option>";
                    }   
                echo $htmlOutput;
            ?>
            </select>
</form>

I was wondering if there is a way of adding the selected options into the array on the fly without reloading the whole page...E.g when they choose one of the <option>'s it's value will stored. Im thinking there may be a way of doing this with jQuery?

I was thinking maybe using a $_GET[] or something. But not too sure how I'd do it.

1
  • Javascript/JQuery cannot directly interact with PHP. They can only do that by the GET or POST method, which can be done by either submitting a form, or using Ajax. Commented Jul 8, 2015 at 10:35

3 Answers 3

1

Change the name of the <select> to:

choose-category[]

So your code will now look like:

<select multiple name='choose-category[]' style='display: inline-block;'>

Also, it is better to avoid -s in names, so this would be the right one:

<select multiple name='choose_category[]' style='display: inline-block;'>

And you can access it using:

$_REQUEST["choose_category"]

Which gives you an array!

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

3 Comments

Using that $_REQUEST[] Doesn't return anything? Do I have to put the $_REQUEST[] inside an if(isset($_POST['button'])){ echo $_REQUEST['choose_category']]} for it to actually print?
No no,.. I just gave $_REQUEST in general. Use either $_GET or $_POST.
How would I print out the selected values with the form I have in my question and the changes that I've made with the <select name='choose_category'>? I can't seem to get it to actually print. Sorry Im not a huge expert in PHP.
0

change your select

from

 <select multiple name='choose-category' style='display: inline-block;'>

to

<select multiple name='choose_category[]' style='display: inline-block;'>

now your choose_category is a array and when you will submit it. you will get a post array with all those values that were selected

Comments

0

you can do like below using jQuery..

<form action='most-liked.php'>
            <select id="Category" multiple name='choose-category' style='display: inline-block;'>
            <?php
                $allCategories = ["All", "Sport", "Automotive", "Comedy", "Misc"];
                $htmlOutput = "";
                $i=0;
                    for($i;$i<sizeof($allCategories);$i++)
                    {
                        $htmlOutput .= "<option value='".$allCategories[$i]."'>".$allCategories[$i]."</option>";
                    }   
                echo $htmlOutput;
            ?>
            </select>
</form>
<script>
var foo = []; 
 $('#Category').change(function() {
     $('#Category :selected').each(function(i, selected){ 
          foo[i] = $(selected).text(); 
    });   alert(foo);
 }); 

</script>

2 Comments

I guess I could use this option if I was going to use AJAX?
yes to avoid reloading you should use Ajax or this type of js

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.