Jump to content

Dynamic Variable problem


croakingtoad

Recommended Posts

I have a form that is passing $_POST values to itself on submit.  The values are predictable based on the data in an array.  So what I'm trying to do is dynamically build the POST variables which I have done like this--

 

//removes characters in $chars from $array items
$chars = array(" ", "&", "(", ")", "/");
$trim = str_replace ($chars, "", $array[$i]);

//Build $_POST variable from array
$one = "$" . "_POST['" . "$trim" . "-1']";
$two = "$" . "_POST['" . "$trim" . "-2']";
$three = "$" . "_POST['" . "$trim" . "-3']";

 

The above works fine and creates a text string representing the var.  Where I run into the problem is executing that string as PHP.  Here's what I have that isn't working--

 

$uno = ${$one};
$dos = ${$two};
$tres = ${$three};

$message .= "$array[$i]";
$message .= "1st- $uno";
$message .= "2nd- $dos";
$message .= "3rd- $tres";

 

When I echo out $message none of the $uno, $dos or $tres vars show up.  Just blank.  When I echo out $one, $two or $three from the first set of code though I am getting a var like--  $_POST['allergy-1']

 

Any ideas what I'm doing wrong?

Link to comment
Share on other sites

First, I think - is not a legal variable character. Correct me if I'm wrong.  I don't understand why you'd want to build the $_POST[] string.. why not just do this:

 

$one = $_POST[$trim . "_1"]; // and so on?

 

but if you must, try leaving the $ off the _POST .. then

 

$uno = ${$one}; // might work if $one = '_POST["x_1"]';

 

 

Link to comment
Share on other sites

Okay, I implemented your suggestions and here's what i have now-

for ($i = 0; $i < count($array); $i++) {

//removes characters in $chars from $array items
$chars = array(" ", "&", "(", ")", "/");
$trim = str_replace ($chars, "", $array[$i]);

//Build $_POST variable from array
$one = $_POST[$trim . "_1"];
$two = $_POST[$trim . "_2"];
$three = $_POST[$trim . "_3"];

//troubleshooting..
if ($i == 0) {
echo $_POST['AllergyImmunology-1'];
echo "1 = $one";
echo "2 = $two";
echo "3 = $three"; }

$uno = ${$one};
$dos = ${$two};
$tres = ${$three};

$message .= "$array[$i]";
$message .= "1st- $uno";
$message .= "2nd- $dos";
$message .= "3rd- $tres";

}

 

I still am not getting the values of $uno, etc. nor $one, etc. when I echo them out...?

Link to comment
Share on other sites

Now that you're setting $one/etc to $_POST[$trim . "_X"] they will contain the value of that post variable, and not the string "$_POST[..]"

 

So, $uno = ${$one}; should give you an error along the lines of an invalid variable, unless say $_POST[] contained the name of a variable. heh. I don't really get what you're trying to accomplish.

 

One or the other I said.

 

Either:

$one =  "_POST['" . $trim . "_1']";
//...
$uno = ${$one};

 

OR just $one, and NO $uno.

 

$one = $_POST[$trim . "_1"];

 

I guess you'd have to give more information on your objective...

Link to comment
Share on other sites

What I'm trying to do is avoid having to write the name of every $_POST returned by the form to be processed (or in this case emailed with phpmail).  I know what the html input names are going to be because I dynamically create them using an array.  I then want to use the same array to create dynamic variables for the part of the script that will process the form submission.

 

Here is my array-

$array = array("Allergy & Immunology", "Anesthesiology", "Cardiology", "Chiropractic", "Dentistry", "Dermatology", "Emergency Medicine", "Endocrinology", "Family Practice", "Gastroenterology", "Geriatrics", "Hematology/Oncology", "Internal Medicine", "Neurology", "Nephrology", "Nurse Practitioner", "Obstetrics & Gynecology", "Ophthalmology", "Optometry", "Osteopathy", "Orthopaedic", "Surgery", "Otolaryngology (ENT)", "Pain Management", "Pediatrics", "Physician Assistant (PA)", "Plastic Surgery", "Physical Medicine", "Podiatry", "Pulmonary", "Psychiatry", "Rheumatology", "Surgery - Cardiac", "Surgery - General", "Surgery - Neurosurgery", "Surgery - Vascular", "Urology");

 

So here is what I'm using to iterate through the array and create the form inputs-

<form action="bedside-manner.php" name="form1" method="post">

<? 

$ip = $_SERVER["REMOTE_ADDR"];

for ($i = 0; $i < count($array); $i++) {

//removes characters in $chars from $array items
$chars = array(" ", "&", "(", ")", "/");
$trim = str_replace ($chars, "", $array[$i]);

if ($i & 2 != 0) { 
echo "<fieldset>"; } else {
echo "
<fieldset style=\"float: left;\">";	}
echo "
	<legend>$array[$i]</legend>
	<input type=\"text\" name=\"$trim_1\" size=\"30\" /> <label>1st</label> <br />
	<input type=\"text\" name=\"$trim_2\" size=\"30\" /> <label>2nd</label> <br />
	<input type=\"text\" name=\"$trim_3\" size=\"30\" /> <label>3rd</label>
</fieldset>
";
}

?>
<div style="clear: both;"> </div>
<input type="hidden" value="<? echo $ip; ?>" name="ip" />
<input type="submit" value="Submit" name="submit" />

</form>

<? } ?>

 

Then when the user submits the form, I am trying to do the below to capture the $_POST values without having to explicitly define each of them.  I'd rather create dynamic variables to do the job for me like below (which still isn't working :)-

<?

if ( isset($_POST['submit']) ) {
$to = "foo@gmail.com";
$subject = "Best in Bedside Awards Submission";
$message = "
	From -

	$name
	$ip
	$email
	";

for ($i = 0; $i < count($array); $i++) {

//removes characters in $chars from $array items
$chars = array(" ", "&", "(", ")", "/");
$trim = str_replace ($chars, "", $array[$i]);

//Dynamically build $_POST variables from array
$one = $_POST[$trim . "_1"];
$two = $_POST[$trim . "_2"];
$three = $_POST[$trim . "_3"];

//troubleshooting..
if ($i == 0) {
echo $_POST['AllergyImmunology_1'];
echo "1 = $one";
echo "2 = $two";
echo "3 = $three"; }

$message .= "$array[$i]";
$message .= "1st- $one";
$message .= "2nd- $two";
$message .= "3rd- $three";

}

$headers .= "To: $to" . "\r\n";
$headers .= "From: $name <$email>" . "\r\n";
//mail($to, $subject, $message, $headers);*/
$thanks = "Thank you for your submission.  Be sure to watch for our Fall issue with The Best in Bedside Manners awards!";

echo "$message";

} else {

?>

 

Right now the output of the above bit of code when the form is submitted is simply--

1 = 2 = 3 = From - 70.188.0.64 Allergy & Immunology1st- 2nd- 3rd- Anesthesiology1st- 2nd- 3rd- Cardiology1st- 2nd- 3rd- Chiropractic1st- 2nd- 3rd- Dentistry1st- 2nd- 3rd- Dermatology1st- 2nd- 3rd- Emergency Medicine1st- 2nd- 3rd- Endocrinology1st- 2nd- 3rd- Family Practice1st- 2nd- 3rd- Gastroenterology1st- 2nd- 3rd- Geriatrics1st- 2nd- 3rd- Hematology/Oncology1st- 2nd- 3rd- Internal Medicine1st- 2nd- 3rd- Neurology1st- 2nd- 3rd- Nephrology1st- 2nd- 3rd- Nurse Practitioner1st- 2nd- 3rd- Obstetrics & Gynecology1st- 2nd- 3rd- Ophthalmology1st- 2nd- 3rd- Optometry1st- 2nd- 3rd- Osteopathy1st- 2nd- 3rd- Orthopaedic1st- 2nd- 3rd- Surgery1st- 2nd- 3rd- Otolaryngology (ENT)1st- 2nd- 3rd- Pain Management1st- 2nd- 3rd- Pediatrics1st- 2nd- 3rd- Physician Assistant (PA)1st- 2nd- 3rd- Plastic Surgery1st- 2nd- 3rd- Physical Medicine1st- 2nd- 3rd- Podiatry1st- 2nd- 3rd- Pulmonary1st- 2nd- 3rd- Psychiatry1st- 2nd- 3rd- Rheumatology1st- 2nd- 3rd- Surgery - Cardiac1st- 2nd- 3rd- Surgery - General1st- 2nd- 3rd- Surgery - Neurosurgery1st- 2nd- 3rd- Surgery - Vascular1st- 2nd- 3rd- Urology1st- 2nd- 3rd-

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.