Jump to content

Recommended Posts

Hi,

Im writing a script for processing a form created by a csv file.

 

this form is created through a loop, where $num is the place in the loop.

<select id=menu$num name=menu$num><option value=default$num SELECTED>$desc</option>
<option value=option1_$num>Option 1</option>
<option value=option2_$num>Option 2</option>
<option value=option3_$num>Option 3</option>
<option value=option4_$num>Option 4</option>
<option value=option5_$num>Option 5</option>
<option value=option6_$num>Option 6</option>
<option value=option7_$num>Option 7</option>
<option value=option8_$num>Option 8</option>
</select>

now im trying to get the data out of the $_POST variable

 

now i need to run a loop to get the data out of $_POST.

something like

 

for ($i=0;$i<num;$i++){
$menu[$i] = $_POST['menu'[$i]]; // where $i is the place in the loop eg menu0 = option1_0 menu1 = option1_1
}

 

im not too sure if there is a way to do this or if its as simple as i have posted.

Thanks in advance for any help.

Link to comment
https://forums.phpfreaks.com/topic/71254-dynamic-form-processing/
Share on other sites

Your doing it the hard way.

Make the select statement a value, and just grab the value

(they can only select one option).

 

<select name="whatever[]">

<option value="value 1">yep</option>

<option value="value 2">Nope</option>

</select>

Then grab in the php

$var = $_POST['whatever'];

 

is that what you were wondering?

Sorry I left 2 fields off the form.

 

<select id=menu$num name=menu$num><option value=default$num SELECTED>$desc</option>
<option value=option1_$num>Option 1</option>
<option value=option2_$num>Option 2</option>
<option value=option3_$num>Option 3</option>
<option value=option4_$num>Option 4</option>
<option value=option5_$num>Option 5</option>
<option value=option6_$num>Option 6</option>
<option value=option7_$num>Option 7</option>
<option value=option8_$num>Option 8</option>
</select>
<input type=text name=deb$num value=\"$var1[$num]\" style=color:FF0000 readonly=yes>
<input type=text name=cred$num value=\"$var2[$num]\" readonly=yes>

 

That is kind of what i want to do.

but the <option value="value 1">yep</option> is generated by a csv file so there could be 5 or 500 values.

I need to be able to distinguish between each value$i in $_POST.

 

 

I wanted to add some more information.

 

I need to do something like

 

for ($i=0;$i<$num;$i++){
if ($menu[$i] = $option$[1]){
$total += $var[$i]
}
$i++;
}

If it's in the select then they can only choose one.

So if you are saving it in an array as i showed,then no matter WHAT is in it

it'll pass the right variable.

Actually wait, that won't work.

<select name="whatever">

// now put all your options

 

</select>

Whatever is selected is available via

$_POST['whatever'] no matter what.

Or am I understanding you wrong?

I think im not explaining it well enough.

There is a select drop down menu on each line of the loop.

so there could be 100 select menus which is why i have <selection name="whatever$num">

i need to get the value from each whatever$num as well as $var1[$i] and $var2[$i]

 

You are confusing me, so let me just offer what I see.

Just a few points is all I can offer until I understand better.

<select id=menu$num name=menu$num><option value=default$num SELECTED>$desc</option>

<option value=option1_$num>Option 1</option>

<option value=option2_$num>Option 2</option>

<option value=option3_$num>Option 3</option>

<option value=option4_$num>Option 4</option>

<option value=option5_$num>Option 5</option>

<option value=option6_$num>Option 6</option>

<option value=option7_$num>Option 7</option>

<option value=option8_$num>Option 8</option>

</select>

<input type=text name=deb$num value=\"$var1[$num]\" style=color:FF0000 readonly=yes>

<input type=text name=cred$num value=\"$var2[$num]\" readonly=yes>

The deb$num part it's happening.

You have to have the $num inside php tags, and the point is it has to be populates.

If this is coming from the db as you said then it's goign to be a paid with the $num all over the

place because you can't tell what it is, you have no way of accessing cred without knowing what

num is.

<select id=menu$num name=menu$num><option value=default$num SELECTED>$desc</option>

<option value=option1_$num>Option 1</option>

<option value=option2_$num>Option 2</option>

<option value=option3_$num>Option 3</option>

<option value=option4_$num>Option 4</option>

<option value=option5_$num>Option 5</option>

<option value=option6_$num>Option 6</option>

<option value=option7_$num>Option 7</option>

<option value=option8_$num>Option 8</option>

</select> and the rest of the form.

is all within a echo line so it is within php tags.

and there is a hidden field with the value of $num so after i press submit i know that $num=61 (just as an example)

there for i know that I need to get $var10 $var11 $var12 etc upto $var161 the same with $var20 $var21 and $menu0 $menu1.

 

and yes i have $num everwhere.

 

do you want me to post the output from $_POST

 

and Thank you for the speedy replys.

This code creates the form. $fp is the file

	echo "<form action=totals.php method=post>";
echo "<table border=1 cellspace=0 cellpadding=0 bordercolor=#000000 align=center width=1000>";
echo "<tr><td colspan=5 align=center>Westpac Banking Corporation</td></tr>";
$num = 0;
while (($line = fgetcsv($fp, 1024, ",")) !== FALSE){
	$debit = $line[3];
	$credit = $line[4];
	$deb1 += $line[3];
	$cred1 += $line[4];
	$deb[$num] = $debit;
	$cred[$num] = $credit;
	$date = $line[1];
	$desc = $line[2];
	echo "<tr><td> $date </td><td><select id=menu$num name=menu$num><option value=default$num SELECTED>$desc</option><option value=income$num>Personal income</option><option value=other$num>Other income</option><option value=household$num>Household Spending</option><option value=services$num>Services</option><option value=vehicle$num>Motor Vehicle</option><option value=rent_mort$num>Rent/Mortage</option><option value=business$num>Business Costs</option><option value=insurance$num>Insurances</option></select></td><td valign=center> <input type=text name=deb$num value=\"$deb[$num]\" style=color:FF0000 readonly=yes></td><td valign=center> <input type=text name=cred$num value=\"$cred[$num]\" readonly=yes></td><td valign=center><input id=annual$num type=checkbox name=annual$num></td></tr>\n";
	$num++;
}
balances($deb1, $cred1);
echo "<tr><td colspan=5 align=center><input type=hidden name=num value=\"$num\"><input type=submit value=\"Submit\" name=submit id=submit></form></td></tr>";
echo "</table>";

 

and here is the code to process the form.

 

$num = $_POST['num'];
for ($i=0; $i<$num; $i++){
$menu[$i] = $_POST[menu$i]; // this is where im having problems, pulling the value of menu0 menu1 menu2 etc out of the array $_POST same for $cred[$i] and $deb[$i]
      if ($menu[$i] = $income[$i]){
      $personal_income += $cred[$i];
}
elseif ($menu[$i] = $other[$i]) {
	$other_income += $cred[$i];
}
elseif ($menu[$i] = $household[$i]) {
	$household_exp += $deb[$i];
}
elseif ($menu[$i] = $services[$i]) {
	$services_exp += $deb[$i];
}
elseif ($menu[$i] = $vehicle[$i]) {
	$vehicle_exp += $deb[$i];
}
elseif ($menu[$i] = $rent_mort[$i]) {
	$rent_mortage += $deb[$i];
}
elseif ($menu[$i] = $business[$i]) {
	$business_exp += $deb[$i];
}
elseif ($menu[$i] = $insurance[$i]) {
	$insurance_exp += $deb[$i];
}
   }


echo "Number of entries $num <br>";
echo "Personal Income : \$$personal_income<br>";
code]

 

its supposed to loop through the $_POST array and seperate all the values and then add them togeather depending on the selection made from the form. using the values in $cred and $deb.

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.