Jump to content

creating variable names from $_POST array


litebearer

Recommended Posts

Regarding the $_POST array. I know how to list all the $_POST array variables; however, I have a couple of questions regarding the output.

 

1. Are the variables ALWAYS in the same order? (ie does it list the form variables in sequential order - 1st field in form will ALWAYS be 1st $_POST variable, 2nd will be 2nd etc).

 

2. Is there a way to create a variable named the same as the $_POST variable? (ie take $_POST['address'] and create a variable named $address).

 

Thanks,

Interesting (and very possibly part of what I am looking to achieve).

 

Perhaps I need to explaon better what I am trying to do...

 

1. I am slow poor typist (I type with two fingers while the other 8 sit and watch).

 

2. I am lazy.

 

3. The real objective:

 

after creating a form with MANY variables I hate having to retype all the variable names in my form procesing page. I am seeking a way to automatically grab the names and write them to a simpl text file. I can then open the file with all the variable names listed

 

example output (what the file would look like):

 

$address = $_POST['address'];

$phone = $_POST['phone'];

 

etc etc

 

Not sure I understand where you're going with this  :confused: but try:

 

$str = '';
foreach ($_POST as $key => $value) {
  $str .= '$'. $key .' = $_POST[\''. $key .'\'];' . "\n";
}

// write $str to a txt file
file_put_contents('file.txt', $str);

$_POST is a perfectly fine array variable. In a lot of cases, making other named variables out of every $_POST entry is a waste of processing time, program memory, and typing (both in making the variables and then later referencing all the variables.)

 

If you want to simply store all the form data, you can use a foreach() loop to iterate over the $_POST array. Using the foreach($array as $key => $value) syntax, you can get both the key (index name) and the value.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.