Sending multiple flags

I am creating a used car website (written in PHP) and I am late in submitting advanced search options from a form. I have more than 30 of them, and I wonder if it is possible and how to send them to one variable (for example, &options=1,2,3,5,6,10or in some other way ..).

I also heard that this is possible with "bitwise", but I do not know how to do this. Or, if anyone has a better idea, please let me know.

Thank.

+5
source share
5 answers

You can send them to an array.

<form method="post">
    <input type="checkbox" name="param[]" value="blue" />
    <input type="checkbox" name="param[]" value="red" />
    <input type="checkbox" name="param[]" value="orange" />
    <input type="checkbox" name="param[]" value="green" />
    <input type="checkbox" name="param[]" value="black" />
    <input type="submit" value="Submit" />
</form>

>> $_POST['param']
array('blue', 'orange')

You can even use multidimensional arrays:

<form method="post">
    <input type="checkbox" name="param[color][]" value="blue" />
    <input type="checkbox" name="param[color][]" value="red" />
    <input type="checkbox" name="param[color][]" value="orange" />
    <input type="checkbox" name="param[color][]" value="green" />
    <input type="checkbox" name="param[color][]" value="black" />
    <input type="checkbox" name="param[year][]" value="1999" />
    <input type="checkbox" name="param[year][]" value="2000" />
    <input type="checkbox" name="param[year][]" value="2001" />
    <input type="checkbox" name="param[year][]" value="2002" />
    <input type="checkbox" name="param[year][]" value="2003" />
    <input type="submit" value="Submit" />
</form>

>> $_POST['param']['color']
array('blue', 'green')

>> $_POST['param']['year']
array('2001', '2004')
+5
source

script, .

<form action="yourscript.php" method="POST">
     <label for="option1">Option 1</label>
     <input id="option1" type="checkbox" name="option[]" value="option1" />
     <label for="option2">Option 2</label>
     <input id="option2" type="checkbox" name="option[]" value="option2" />
     <label for="option3">Option 3</label>
     <input id="option3" type="checkbox" name="option[]" value="option3" />
     <input type="submit" name="submit" value=Submit />
</form>

. option[]

script .

if(!empty($_POST['submit']) //process the form

     if(!empty($_POST['option']) //check to see if any checkboxes were selected
     {
          $options = $_POST['option'];
          foreach($options as $option) //loop through the checkboxes
          {
               //do what you need to do with an option
          }
     }

}
+2

, , . jQuery. . , .

javascript:

$('.options').click(function() {
var selectedItems = new Array();
$("input:checkbox[name='opt[]']:checked").each(function() {selectedItems.push($(this).val());});
var data = selectedItems.join('|');
$("#opts").val(data);
});

:

<form name="search" method="get" action="blabla.php">
.....
<input type="hidden" name="options" id="opts" value=""/>
....
</form>

...
<input type="checkbox" name="opt[]" class="options" value="1"/>
<input type="checkbox" name="opt[]" class="options" value="2"/>
<input type="checkbox" name="opt[]" class="options" value="3"/>
...
+2

script, (.. ), , (1,3,5 1 3 5, 2 4 ).

. , 10101 == 17 (.. 1 3 5 "on", 2 4 ). , . [ , send 1000 == 8 == 1 + 3 + 5 ---- ]

0

GET URL- .

POST URL-, "" .

URL- GET, script, - , JavaScript URL- .

. , 32- 8 4 :

01010010010001001010101010101001

:

0101 0010 0100 0100 1010 1010 1010 1001
  #8   #7   #6   #5   #4   #3   #2   #1

, 16 4- . , , , - SO , .:)

0

All Articles