Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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

Posted on 2007-04-07
Medium Priority
Last Modified: 2007-04-09
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)) {
Question by:Quetysis
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
LVL 20

Expert Comment

ID: 18870122
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];

Expert Comment

ID: 18870594
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.

Author Comment

ID: 18871081
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 ?

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 18878049
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?

Author Comment

ID: 18878102
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?
LVL 20

Accepted Solution

steelseth12 earned 2000 total points
ID: 18878505
hey Quetysis would you mind posting what code you have so far cause im not sure i fully understand what your looking for ......

$myvar = $_REQUEST;
      foreach($myvar as $key=>$value) {
            $$key = $value;
            print $$key;

Now the difference between $_REQUEST with $_POST and $_GET is that $_REQUEST hold the values of $_POST, $_GET and $_COOKIE which all 3 of them are insecure as all 3 can be easy changed ... propably the only safe super global is $_SESSION .... now as for sanitising your code ... that just means in simple terms to chack and see that you are getting what you are looking for ... for example in the phone number field check that it is only numbers .

Author Comment

ID: 18878629
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.  :-)

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question