一千萬個為什麽

搜索

發送多個復選框選項

I'm creating a used cars website (written in PHP), and I'm stuck on sending advanced search options from form. I have more than 30 of them and I wonder if it's possible, and how, to send them in one variable (for example &options=1,2,3,5,6,10 or some other way..).

我也聽說這可能是“按位”,但我不知道如何做到這一點。或者如果有人有更好的想法,請告訴我。

謝謝。

最佳答案

您可以將它們發送到陣列中。

<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')

您甚至可以使用多維數組:

<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')

轉載註明原文: 發送多個復選框選項