Jean Ouédraogo
Jean Ouédraogo

Reputation: 244

how to get a html form element with name is php variable

I want to get html form element by post with the name being a php variable

example :

<form method="post" action="action.php"><input type="submit" name="'.$name.'"></form>

action.php code:

$var=$_POST['What do i put here?'];

Thanks

Upvotes: 5

Views: 3989

Answers (5)

Jean Ou&#233;draogo
Jean Ou&#233;draogo

Reputation: 244

I figured out how to do it . <form method="post" action="action.php"><input type="hidden" name="name" value="'.$name.'">

action.php: $var=$_POST['name']; echo $var;

it displays the value of the input which is $name.

input can be whatever you want !!

Upvotes: 0

irfanengineer
irfanengineer

Reputation: 1300

Try This: print_r($_POST);

This is print all values with parameter.

Upvotes: 0

Edmar
Edmar

Reputation: 21

Try this.

$name = 'name';
$var  = $_POST[$name];

The result will display the value of the name variable.

Upvotes: 0

Lakshya
Lakshya

Reputation: 506

you can simply put it like
$_POST["{$name}"];
or
you can concatenate it like
$_POST['abc_'.$name];

To make it more clear See http://www.php.net/manual/en/language.types.string.php

Upvotes: 0

Dave
Dave

Reputation: 3091

try this, use $_POST array in foreach:

action.php

foreach ($_POST as $key => $value)
echo "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."<br>";

i hope it will be helpful.

Upvotes: 2

Related Questions