Jump to content

Creating variable names from form variables


feck

Recommended Posts

When using long forms you have to go about creating variables for all of the input fields of a form.What I am trying to to is create some variable names from the input names/id's.Heres a simple form:

<form action="test.php" method="post">	<input type="text" id="box1" name="box1" size="30" />	<input type="text" id="box2" name="box2" size="30" />	<input type="submit" id="sub" name="sub" value="Submit"/></form>

And heres a simple for each loop that I'm trying to use to create the variable names, bear in mind that the echo statement is just for testing purposes, and if actually worked would only show the id/name of the first input box:

foreach($_POST as $env => $val){		$$env;		echo "<p>Hello this is the name of the input box: $box1 <br /></p>";	}

Where am I going wrong?Here's someone who's done something similar:http://www.dreamincode.net/forums/showtopic9531.htm

Link to comment
Share on other sites

Sorry people solved it.Well actually sorted out my own stupidity again.the $box1 variable had been named by using $$env, but there was still nothing in it. So although I was trying to test if it had worked, it never had a value in it, it had just been named.

foreach($_POST as $env => $val){				if($env != "sub"){ //does'nt include the submit button			$$env = $val;			echo "<p>This box is named: $env<br />with value: $box1 <br /></p>";		}			}

As i stated in the first post the echo is just for testing.I can now create variables on the fly, without having to create a long list of variable names and associating them with values, just use a simple foreach loop. This will name the variables according to the names of the input boxes.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...