get name of a post variable [duplicate] - php

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.

Related

Get all possible $_GET parameters from URL

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

HTML PHP array index lenght error

I have an html form where I am grouping inputs using name="array[]" then just looping through the array with PHP when submitted. Well I am using array[] to store the question, but when the question (array index) is longer than 64 characters then It will not pass that array key to my PHP.
HTML
<textarea name="corporate[CAN YOU SHOW US SIMILAR PROJECTS WITH THE SAME TARGET AUDIENCE? COMPETITORS?]"></textarea>
When I do:
var_dump($_POST['array']);
I get array(0)
But when I use a shorter index, it works.
Now if I manually create an associative array it works fine:
$array = array("CAN YOU SHOW US SIMILAR PROJECTS WITH THE SAME TARGET AUDIENCE? COMPETITORS?"=>"0");
What am I doing wrong?
I think it has to be a problem with going from the html form to the PHP. I am trying to loop through the inputs with my PHP so that I can loop through and display each question and corresponding answer with:
foreach ($array as $key=>$value) {
if ($value != NULL) {
echo '<strong>' . $key . '</strong><br/>';
echo $value . '<br/><br/>';
}
}
Which gives me:
Question
Answer
Question
Answer
etc.
How else could I do this without giving each input their own name to pass the question, or hard coding the question in my HTML?
It might be because you're doing this
var_dump($_POST['array']);
when you named the textarea "corporate". Try just doing
var_dump($_POST);
You can use a hidden input with the question and match up the indexes with the answer:
<input type="hidden" name="question[1]" value="CAN YOU SHOW US BLAH?">
<textarea name="answer[1]"></textarea>
I assume you are dynamically adding the question text? If so you should probably use htmlentities on it to avoid issues.
Then just loop one and access the other:
foreach ($_POST['answer'] as $key => $value) {
if (!empty($value)) {
echo '<strong>' . $_POST['question'][$key] . '</strong><br/>';
echo $value . '<br/><br/>';
}
}

PHP post all form inputs without creating variables for each

Usually when I use PHP I create a variable and post statement for each form input like so:
$myVar1 = $_POST["formItem1"];
$myVar2 = $_POST["formItem2"];
ect......
I know if i use:
echo $_POST;
I can get all the key and values from the form inputs but I don't know how to use them in a script.
So I guess I have 2 questions:
How do I quickly post all form inputs without creating a variable for each input?
How do I use a posted in a script without having it assigned to a specific variable?
To simply echo all the inputs, you can use a foreach loop:
foreach ($_POST as $key => $value) {
echo $value.'<br/>';
}
If you don't want to store them in variables, use arrays:
$data = array();
foreach ($_POST as $key => $value) {
$data[] = $value;
}
Now, $data is an array containing all the data in $_POST array. You can use it however you wish.
You don't have to assign to a variable. You can use directly $_POST['input_name']
If you want to deal with each sended params, you can use foreach loop:
foreach ($_POST as $key => $val)
{
echo "$key : $val <br/>";
}
for this instance of just quickly checking and testing i typically just use the print_r() function: documentation here
as quoted from the docs:
print_r() displays information about a variable in a way that's readable by humans.
one line easy to toggle on and off (with comments)- no need to use any form of variables
print_r($_POST);
if i need my output nice and readable i like to expand it as follows:
function print_r2($x){
echo '<pre>';
print_r($x);
echo '</pre>';
}
and then you can call with: print_r2($_POST);
this way you get the pre-formatted text block on your html page and can see the line breaks and tabbed spacing provided from the $_POST object printout
Another way to extract (besides the extract function) variables is;
$array = array('foo'); // Which POST variables do you want to get
foreach($array as $key) {
if(!isset(${$key})) { // Check if variable hasn't been assigned already
${$key} = $_POST[$key];
}
}
echo $foo;
I would not recommend it because it can get quite messy to keep up.
View everything in $_POST is useful for debugging
echo '<pre>'.print_r($_POST, true).'</pre>';
Access a specific checkbox
HTML
<input type="checkbox" value="something" name="ckbox[]" checked>
<input type="checkbox" value="anotherthing" name="ckbox[]" checked>
PHP
echo $_POST['ckbox'][0]; // something
echo $_POST['ckbox'][1]; // anotherthing
// isolate checkbox array
$ckbox_array = $_POST['ckbox'];
Define a function that allows you to access a specific $_POST item by name (key):
function get_post_value($name, $default)
{
if ( isset($_POST[$name]) ) {
return $_POST[$name];
} else if ( $default ) {
return $default;
}
return null;
}
$default allows you to pass a value that can be used as a fallback if the key you specify isn't present in the $_POST array.
Now you can reference $_POST items without assigning them to a variable, and without worrying if they are set. For example:
if ( get_post_value('user-login-submit', false) ) {
// attempt to log in user
}
You can use extract($_POST), it will create variables for you.
For example, you can have for the code you posted :
<?php
$_POST["formItem1"] = "foo";
extract($_POST);
echo $formItem1; // will display "foo"
?>
EDIT : it's not PHP explode, it's extract.

How Can I Pass Array Values through Form Submission?

I am trying to pass an array of values through a form submission. As an example:
example.com?value1=178&value2=345&value3=2356
The easy solution would be for me to do the following to get the values on the new page:
$value1=$_GET['value1'];
$value2=$_GET['value2'];
$value3=$_GET['value3'];
The difficulty that I am having is that the variable after the word 'value' passed through the form will change with each submission. So I have amended the code to pass as:
example.com?value14=178&variable=14&value23=345&variable=23&value63=2356&variable=63
As you can see here, I have now passed the variable that comes in from of the value as a GET parameter. My attempt then GET these values to display individually on the submitted page is as follows:
$variable=$_GET['variable'];
$value=$_GET['value'.$variable];
echo $value . '<br>';
This code almost works. I am able to get the last array which is passed through to display. How can I fix this code to get all of the passed values to display on the submitted page?
Use PHP's array notation for form fields:
val[]=178&val[]=14&val[]=345&etc...
This will cause $_GET['val'] to be an array:
$_GET = array(
'val' => array(178, 14, 345, etc...)
)
If you can't rearrange the URL like that, you can try using preg_grep:
$matches = preg_grep('/^variable\d+$/', array_keys($_GET));
which'll return :
$matches= array('variable1', 'variable2', 'variable3', etc...);
Use an array, for example like this without the need for variable $variable.
example.com?value[14]=178&value[23]=345&value[63]=2356
foreach ($_GET['value'] as $key => value) {
echo $key . " => " . $value . "<br/>";
}
EDIT: Another way for getting the values would be looping the whole $_GET -array and parsing values from there like this (variables are always in the form of "value" followed by X numbers):
example.com?value14=178&value23=345&value63=2356
$values = array();
foreach ($_GET as $key => $value) {
if (preg_match('/^value[\d]+$/', $key)) {
// remove "value" from the beginning of the key
$key = str_replace('value', '', $key);
// save result to array
$values[$key] = $value;
}
}
See http_build_query()

How do you output several lines of the $_POST variable

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 />';
}

Categories