Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all variables sent with POST?

Tags:

php

http-post

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?

like image 435
lisovaccaro Avatar asked Nov 21 '11 05:11

lisovaccaro


People also ask

How do I get variables from a POST?

The variable $_POST is automatically populated. Try var_dump($_POST); to see the contents. 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');

How display all POST values in PHP?

Simply: <? php print_r($_POST); //Or: foreach ($_POST as $key => $value) echo $key.

What is $_ GET?

PHP $_GET is a PHP super global variable which is used to collect form data after submitting an HTML form with method="get". $_GET can also collect data sent in the URL. Assume we have an HTML page that contains a hyperlink with parameters: <html> <body>

What is the difference between $post and $_ POST?

$_POST is a superglobal whereas $POST appears to be somebody forgetting the underscore. It could also be a standard variable but more than likely it's a mistake.

What is $_get and $_post variable in PHP?

Like $_GET, PHP gives another superglobal variable $_POST to get to all the data sent by means of post strategy or submitted through an HTML structure utilizing the method=”post”.

How to access get and post data in PHP?

GET method data can be accessed using PHP QUERY_STRING environment variable. PHP $_GET associative array is used to access all the sent information by GET method. In PHP, the $_POST variable is used to collect values from HTML forms using method post.

How to collect values from HTML forms using PHP post method?

In PHP, the $_POST variable is used to collect values from HTML forms using method post. Information sent from a form with the POST method is invisible and has no limits on the amount of information to send.

How to access the information sent by GET method in PHP?

PHP $_GET associative array is used to access all the sent information by GET method. In PHP, the $_POST variable is used to collect values from HTML forms using method post. Information sent from a form with the POST method is invisible and has no limits on the amount of information to send.


1 Answers

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>";     }   } 
like image 199
Code Magician Avatar answered Sep 19 '22 06:09

Code Magician