Solved

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

Posted on 2007-04-07
7
164 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
[X]
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
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
Industry Leaders: 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Session time out quickly. 6 56
PHP URL Parameter to simple URL for single folder 16 48
PHP and accessing Array Elements 3 34
Joomla Editing 3 23
This article discusses four methods for overlaying images in a container on a web page
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

751 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