Solved

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

Posted on 2007-04-07
7
159 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

757 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

22 Experts available now in Live!

Get 1:1 Help Now