Solved

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

Posted on 2007-04-07
7
163 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
Technology Partners: 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

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
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 …

685 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