Link to home
Start Free TrialLog in
Avatar of Quetysis
Quetysis

asked on

Is it possible to assign a variable dynamically from a form request?

Not sure if what I want to do is possible.  I want to loop through and set my variables from the form request for my check boxes.  I have my check box variable named stored in a variable.  Here's what I'm trying to do:

for ( $i = 1; $i <= 10, $i++ ) {
  $var = 'chkbox' . $i;
  if (isset($$var)) {
     $_REQUEST['$$var']
  }
}
Avatar of steelseth12
steelseth12
Flag of Cyprus image

If i understood correctly this is what you are trying to do

for ( $i = 1; $i <= 10; $i++ ) {
  $var = 'chkbox' . $i;

  if (isset($_REQUEST[$var])) {
     print $_REQUEST[$var];
  }
}
Avatar of NickVd
NickVd

Agreed, however it is best to use either $_POST or $_GET to retrieve the data, as that way you know where the data is coming from.

And remember... NEVER TRUST USER INPUT, EVER, EVER, EVER........... NEVER!

Always filter/sanitize your input and escape your output.
Avatar of Quetysis

ASKER

Hi steelseth12!  I will give that a shot and report back.  Thanks.  I'm new to PHP so I was wonder if you and NickVd could clarify what NickVd was saying about filtering and sanitizing the data.  Also, what is the difference between using $_REQUEST and $_POST or $_GET ?

Thanks!
Ok, I had a chance to try out the suggestion above and it is not going to work for what I am asking.  Any more ideas?
Instead of specifying the variable assignment like this:

print $_REQUEST['firstname'];

I want to do this:

print $_REQUEST['$$var']

Where $$var will earlier be assigned to be firstname.  Make sense?
ASKER CERTIFIED SOLUTION
Avatar of steelseth12
steelseth12
Flag of Cyprus image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Steelseth12!

I figured out my problem when I went back and re-read the PHP documentation on variable variables for the 5th time.  ug...  Anyway, thanks for your help and for the explaination.  It was much appreciated.  :-)