Solved

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

Posted on 2007-04-07
7
160 Views
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)) {
     $_REQUEST['$$var']
  }
}
0
Comment
Question by:Quetysis
  • 4
  • 2
7 Comments
 
LVL 20

Expert Comment

by:steelseth12
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];
  }
}
0
 
LVL 5

Expert Comment

by:NickVd
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.
0
 

Author Comment

by:Quetysis
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 ?

Thanks!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Quetysis
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?
0
 

Author Comment

by:Quetysis
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?
0
 
LVL 20

Accepted Solution

by:
steelseth12 earned 500 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 .
0
 

Author Comment

by:Quetysis
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.  :-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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 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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

932 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now