Viewed   48 times

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=on&30=on

I need to insert the variables in the database.

How do I get all variables sent with POST? As an array or values separated with comas or something?

 Answers

5

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.

<form action="myscript.php" method="post">
  <input type="checkbox" name="myCheckbox[]" value="A" />val1<br />
  <input type="checkbox" name="myCheckbox[]" value="B" />val2<br />
  <input type="checkbox" name="myCheckbox[]" value="C" />val3<br />
  <input type="checkbox" name="myCheckbox[]" value="D" />val4<br />
  <input type="checkbox" name="myCheckbox[]" value="E" />val5
  <input type="submit" name="Submit" value="Submit" />
</form>

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): <br>");
    for($j = 0; $j < $i; $j++)
    {
      echo $myboxes[$j] . "<br>";
    }
  }
Wednesday, August 17, 2022
3

You can convince PHP's curl backend to stop doing the 100-continue-thing by setting an explicit request header:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));

This way you can post a request however long you would ever want and curl will not do the dual phase post.

I've blogged about this nearly two years ago.

Monday, October 3, 2022
 
mivra
 
3

Change the order around:

header("Location: ..."); // The new resource URL
header('HTTP/1.1 201 Created');
header('Content-type: application/json; charset=utf-8');
echo $response;
exit;

Test:

# curl -i "http://localhost/projects/scratch/302.php"
HTTP/1.1 201 Created
Date: Sun, 29 Jan 2012 23:09:02 GMT
Server: Apache/2.2.17 (Win32) mod_ssl/2.2.17 OpenSSL/0.9.8o PHP/5.3.4 mod_perl/2.0.4 Perl/v5.10.1
X-Powered-By: PHP/5.3.5
Location: ...
Content-Length: 0
Content-Type: application/json; charset=utf-8

Another way

Keep the original order, but force a 201. According to the PHP docs:

it also returns a REDIRECT (302) status code to the browser unless the 201 or a 3xx status code has already been set.

header('HTTP/1.1 201 Created', true, 201);
header("Location: ..."); // The new resource URL
header('Content-type: application/json; charset=utf-8');
echo $response;
exit;
Thursday, December 15, 2022
 
5

The following uses strpos() to check that the POST string begins with book_

foreach($_POST as $key => $value) {
    if (strpos($key, 'book_') === 0) {
        // value starts with book_
    }
}
Saturday, December 10, 2022
 
2

FQL often provides more information than Graph API. You have to use the attachment parameter of the stream FQL table to get all the attached photos.

SELECT attachment FROM stream 
 WHERE source_id = me() 
   AND post_id="11882030_4952296803730"

Result:

{
  "data": [
    {
      "attachment": {
        "media": [
          {
            "href": "https://www.facebook.com/photo.php?fbid=471082296...", 
            "alt": "", 
            "type": "photo", 
            "src": "https://fbcdn-photos-a.akamaihd.net/hphotos-ak-ash3/5826...", 
            "photo": {
              "aid": "4391039135", 
              "pid": "439104482145", 
              "fbid": 471507, 
              "owner": 102832, 
              "index": 1, 
              "width": 485, 
              "height": 172, 
              "images": [
                {
                  "src": "https://fbcdn-photos-a.akamaihd.net/hph...", 
                  "width": 130, 
                  "height": 46
                }
              ]
            }
          }
        ], 
        "name": "", 
        "caption": "", 
        "description": "", 
        "properties": [
        ], 
        "icon": "https://fbstatic-a.akamaihd.net/rsrc.phpjk.gif", 
        "fb_object_type": "album", 
        "fb_object_id": "4391044992857139135"
      },
      { 
        ... //Photo 2
      }
    }
  ]
}
Sunday, November 27, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :