0

I am trying to populate an HTML form with values from a previous form being posted. The first form is created dynamically and is a list of t-shirt styles with 5 different sizes available.

I'm having trouble figuring out a way to populate the second form.

The HTML is this:

echo '<input name="'.$filename.'-s" type="text" size="3" value="'.$quantity.'"'>;

What I'd like to do for $quantity is something like this:

$quantity = $_POST['{$filename.$size}'];

Is it possible to use a variable with $_POST?

5
  • Why shouldn't it be possible? You don't even need quotes, just do: $_POST[$filename . $size] and try it Commented Jul 11, 2015 at 18:21
  • Single quoted strings are not parsed. Either use double quotes or no quotes as @Rizier123 advised Commented Jul 11, 2015 at 18:22
  • Please read this post, it is only a few days older than yours and the question is almost identical: stackoverflow.com/questions/31329499/… (edit, this may not be an exact duplicate, but does solve the issue of parsing unknown array keys into $_POST ) Commented Jul 11, 2015 at 18:52
  • where is the value $size coming from? Commented Jul 11, 2015 at 18:54
  • @Rizier123 Thanks. I had tried that first and it didn't seem to work. Must have been something else. Got interrupted by a fire call in the middle of working on it and lost my place. So then I started trying all kinds of different variations. Got it working now. Commented Jul 11, 2015 at 19:02

2 Answers 2

1

I think you're looking for this:

$quantity = htmlentities($_POST["$filename$size"]);

htmlentities() is necessary in case you have " or other junk in the input

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

1 Comment

I would add a small qualifier for the OP and recommend that htmlentities is used with as many of the variables filled in as possible, for example htmlentities($var, ENT_QUOTES, "UTF-8", FALSE); rather than relay on defaults.
0

$_POST[$var1.$var2] worked. Nothing came up in my initial search of previous answers, although I see several applicable questions in the related questions on this page.

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.