How to use $_GET to get multiple parameters using the same name in PHP

后端 未结 4 1216
长发绾君心
长发绾君心 2021-01-20 04:23

I\'m using checkboxes to search a mysql database I have created. As the checkboxes all use the same name, when I use $_GET, it only gets the last value in the URL.

相关标签:
4条回答
  • 2021-01-20 04:34

    I have never tried it with $_GET, but if you use $_POST you can do something like this:

    <input type="checkbox" name="car[]" value="honda" /> Honda
    <input type="checkbox" name="car[]" value="ford" /> Ford
    <input type="checkbox" name="car[]" value="toyota" /> Toyota
    // note the [] in the name
    

    so that $car = $_POST['car'] is an array

    You can try it with $_GET as well and see.

    0 讨论(0)
  • 2021-01-20 04:40

    PHP is a little odd here. Using its standard form data parser, you must end the name of the controls with [] in order to access more than one of them.

    <input type="checkbox" name="foo[]" value="bar">
    <input type="checkbox" name="foo[]" value="bar">
    <input type="checkbox" name="foo[]" value="bar">
    

    Will be available as an array in:

    $_GET['foo'][]
    

    If you don't want to rename the fields, then you will need to get access to the raw data ($_SERVER['REQUEST_URI']) and parse it yourself (not something I'd recommend).

    0 讨论(0)
  • 2021-01-20 04:40

    you need to make the NAME for that variable a HTML array'd variable so on the form, it would be

    <input name='features[]'/>
    
    0 讨论(0)
  • 2021-01-20 05:00

    Name your checkbox elements "features[]" in html. That way they will be passed as an array.

    0 讨论(0)
提交回复
热议问题