Get all variables sent with POST?

后端 未结 6 1959
孤城傲影
孤城傲影 2020-11-27 02:45

I need to insert all variables sent with post, they were checkboxes each representing an user.

If I use GET I get something like this:

?19=on&25=         


        
6条回答
  •  轮回少年
    2020-11-27 03:13

    The variable $_POST is automatically populated.

    Try var_dump($_POST); to see the contents.

    You can access individual values like this: echo $_POST["name"];

    This, of course, assumes your form is using the typical form encoding (i.e. enctype=”multipart/form-data”

    If your post data is in another format (e.g. JSON or XML, you can do something like this:

    $post = file_get_contents('php://input');
    

    and $post will contain the raw data.

    Assuming you're using the standard $_POST variable, you can test if a checkbox is checked like this:

    if(isset($_POST['myCheckbox']) && $_POST['myCheckbox'] == 'Yes')
    {
         ...
    }
    

    If you have an array of checkboxes (e.g.

    val1
    val2
    val3
    val4
    val5

    Using [ ] in the checkbox name indicates that the selected values will be accessed by PHP script as an array. In this case $_POST['myCheckbox'] won't return a single string but will return an array consisting of all the values of the checkboxes that were checked.

    For instance, if I checked all the boxes, $_POST['myCheckbox'] would be an array consisting of: {A, B, C, D, E}. Here's an example of how to retrieve the array of values and display them:

      $myboxes = $_POST['myCheckbox'];
      if(empty($myboxes))
      {
        echo("You didn't select any boxes.");
      }
      else
      {
        $i = count($myboxes);
        echo("You selected $i box(es): 
    "); for($j = 0; $j < $i; $j++) { echo $myboxes[$j] . "
    "; } }

提交回复
热议问题