Jump to content

Form field names as variable names


scottwhittaker

Recommended Posts

Hi

Is it posible to use form field names as variable names like so...

 

<form name="test" method="GET"....>

<input name="surname">....

 

when submitted creates URL like  form_handler.php?surname=value_entered

 

Can I somehow set a variable with the name of each form input and set it to the value entered so that when I type "print $surname;" It prints the name name they entered?

 

I know this sounds strange but I want to create a script for use on many different forms which I wont know all of the input names for.

Hope that makes sense, kinda hard to explain though. I've had a look but nothing came up.

 

Cheers

Scott

 

 

Link to comment
Share on other sites

Hi thanks for the response but thats not what I was meaning although I see how u thought that.

 

What you replied with was how to put a POST or GET value into a variable BUT what if I do not know the name of the input field which would mean I cannot type the code you supplied because I do not know the name of the bit in red...here... ( $_GET['surname']; ).

 

I want the variable name to be the name of the input field (almost like a dynamically created variable name) and the value of the variable to be the value entered on the form.

 

I could swear I had read about this somewhere previously but its looking like I havent!

Link to comment
Share on other sites

register_globals does this, but it's depreciated and a huge security risk, so I do not suggest you use this.

 

As for the code you're after, I think you want something like this:

 

foreach($_GET as $var => $val)
{
    $$var = $val;
}

 

But again, this poses the same security flaw as register_globals, because anyone can inject their own variables into your script, so again I do not suggest you use this.

Link to comment
Share on other sites

The $_GET array is a perfectly usable set of variables. By making a series of named variables from it, you must now keep track of each of those variable names and know how many there were and write out code to reference each one. That does not make for general purpose coding.

 

script for use on many different forms I wont know all of the input names for
Since you are trying to write a generic script where you won't know the names before hand, it will be impossible for you to know how many and what names the variables will be. Just use array functions (i.e. foreach(){}) to process the $_GET array directly without going through an additional intermediate step of creating named variables (this will save processing time and memory as well.)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.