How do I output several lines of the $_POST variable ?
When I keep outputting the result I only get the last $_POST variable
Thanks for helping
If you want more detailed information about what's being stored in $_POST you can use
var_dump($_POST);
This will return the key, contents and type of each entry
print_r($_POST);
This will display the key and contents of each entry.
If you want to cycle through the contents of $_POST and format the output you can use.
foreach($_POST as $key => $value){
print $key.' is '.$value.'<br />';
}
What do you mean by "the last $_POST variable"? Please provide the code snipped and the desired output format.
echo $_POST; -> "Array"
print_r ($_POST); -> detailed output of the array's contents.
You mean something like this?
foreach ($_POST as $key => $value)
{
echo $key . ': ' . $value . '<br />';
}
Related
I have link to a website that runs a php code with the following variable:
http://www.example.com/run.php?test=abc
There are other $_GET variables usable in the link other than test, say id and title, which I don't know about. Is it possible to get the missing variables (If there are any)? In my example case it would be the id and title variables.
$_GET is an associative array of variables passed to the current script via the URL PHP Docs
So it will hold all the url parameters. You can see this by doing:
var_dump($_GET);
Then you can do something with your $_GET parameters like so:
foreach ($_GET as $getParam => $value) {
echo $getParam . ' = ' . $value . PHP_EOL;
}
Yes...
$_GET is an array.
So $_GET[0] could be the value of test
$_GET[1] could be the value of title
So what you need to to is find out how many values are held in the $_GET array or loop through the array:
foreach ($_GET as $getParam => $value) {
echo $getParam . ' = ' . $value . PHP_EOL;
}
Also, if your do print_r($_GET); you can see how all the different entries in the array.
Yes, array_keys($_GET) will give you the keys, just iterate through them
I have a.php file that send some elements data to b.php with GET method.
I b.php we don`t know id of elements in a.php file.
But we need access them.
Are there any way to read all submited data in b.php?
Thanks.
you can use the foreach structure:
foreach ($_GET as $key => $value)
{
// Do something
}
$_GET is a superglobal array. Here is the doc about it.
Like for all variables, you can dump its content and structured information about it to the output with the var_dump() function. This will help you to understand how it work.
$_GET only returns parameters passed by HTTP GET. It's commonly the part after the question mark in the URI, e.g. ?key1=value1&key2=value2
You can also use $_REQUEST (documentation here) to retrieve all the values passed in the request by GET, POST, and COOKIE.
Some people think using $_REQUEST is unsafe, but IMHO, check the HTTP method have a very poor interest (well, not interest at all), because it depend of the user. And the user can trick this very easily.
Use the following to list all arguments.
foreach ($_GET as $key => $value) {
echo $key . ' => ' . $value . '<br />';
}
To access all the data in the $_GET global array, you can do something like this:
foreach($_GET as $key => $value)
echo "$key: $value";
How would I get all the data from fields that have been posted or requested using get in PHP?
e.g
echo $_GET[*];
var_dump ($_GET);
or
print_r ($_GET);
or
echo (json_encode ($_GET));
or
foreach ($_GET as $key => $val)
{
echo ($key . ', ' . $val);
}
or any number of other methods
You can use print_r:
<?php
...
print_r($_GET)
...
?>
If you want to display all the values of an array or an object, you can use print_r. You'll want to use it inside <pre> tags to get all the indentation and new lines.
<pre>
<?php print_r($_GET); ?>
</pre>
extract($_GET);
fun with extract!
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP $_POST print variable name along with value
I have a form (whatever number of fields).. this form will send $_POST data.. I want to return every $_POST variable value and name.
I wanna do soemthing like this :
foreach($_POST as $field){
//****some code*****//
}
in a way that will display the fields and values like this:
name : Simo Taqi email : example#ymail.com
in other words :
if I have a post variable : $_POST['city']='las vegas'
I want to know how to get the name of the variable : 'city'.
$_POST is populated as an associative array, so you can just do this:
foreach ($_POST as $name => $val)
{
echo htmlspecialchars($name . ': ' . $val) . "\n";
}
Additionally, if you're just interested in the field names, you can use array_keys($_POST); to return an array of all the keys used in $_POST. You can use those keys to reference values in $_POST.
foreach (array_keys($_POST) as $field)
{
echo $_POST[$field];
}
foreach documentation
array_keys documentation
Use the extended foreach syntax:
foreach ($_POST as $key => $value)
{
echo $key . ": ". $value . "\n";
}
I disagree with this post, since it assumes that output is always intended for a browser. One should not get into the habit of this. \n is the correct usage and can be easily converted before output ( to a browser ) using the nl2br() function.
I want to save the name of all the $_GET variables in a url, but im not sure where to start, or finish for that matter.
For example:
if i have:
url: test.com/forums.php?topic=blog&discussion_id=12
can i use php to get the name, i.e. "topic" and "discussion_id from the $_GET variables and can i then store the values: "topic" and "discussion_id" in an array?
You can get this by calling array_keys on $_GET:
$getVars = array_keys($_GET);
If this isn't about the current URL, but just some $url string you want to extract the parameters from then:
parse_str(parse_url($url, PHP_URL_QUERY), $params);
will populate $params with:
[topic] => blog
[discussion_id] => 12
Use the following code to grab data from the URL using GET. Change it to $_POST will work for post.
<?php
foreach ( $_GET as $key => $value )
{
//other code go here
echo 'Index : ' . $key . ' & Value : ' . $value;
echo '<br/>';
}
?>
$_GET is usual php-array. you may use it in foreach loop:
foreach ($_GET as $k => $v)
echo ($k . '=' . $v);
It's an array:
print_r($_GET);
Fetch the elements as you would with any other array.