5

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

2
  • Why do you need it to be a php variable? Commented Jul 25, 2016 at 6:27
  • whatever is the value of $name variale will come Commented Jul 25, 2016 at 6:52

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

what if i have in my form several input that i wish to get elements form by post like : <form method="post" action="action.php"><input type="submit" name="'.$name.'"> <input type="text" name="'.$course.'"</form> ?
0

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

Comments

0

Try this.

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

The result will display the value of the name variable.

Comments

0

Try This: print_r($_POST);

This is print all values with parameter.

2 Comments

Here is now what i want to do , i want to retrieve the element of the post one by one . i tried all the solution given above but it does not work .Example :echo $POST[$name];
This is just print all parameters.
0

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 !!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.