Comdemned Posted September 30, 2007 Share Posted September 30, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/71254-dynamic-form-processing/ Share on other sites More sharing options...
Ninjakreborn Posted September 30, 2007 Share Posted September 30, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/71254-dynamic-form-processing/#findComment-358377 Share on other sites More sharing options...
Comdemned Posted September 30, 2007 Author Share Posted September 30, 2007 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++; } Quote Link to comment https://forums.phpfreaks.com/topic/71254-dynamic-form-processing/#findComment-358392 Share on other sites More sharing options...
Ninjakreborn Posted September 30, 2007 Share Posted September 30, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/71254-dynamic-form-processing/#findComment-358397 Share on other sites More sharing options...
Comdemned Posted September 30, 2007 Author Share Posted September 30, 2007 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] Quote Link to comment https://forums.phpfreaks.com/topic/71254-dynamic-form-processing/#findComment-358399 Share on other sites More sharing options...
Ninjakreborn Posted September 30, 2007 Share Posted September 30, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/71254-dynamic-form-processing/#findComment-358400 Share on other sites More sharing options...
Comdemned Posted September 30, 2007 Author Share Posted September 30, 2007 <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. Quote Link to comment https://forums.phpfreaks.com/topic/71254-dynamic-form-processing/#findComment-358403 Share on other sites More sharing options...
Ninjakreborn Posted September 30, 2007 Share Posted September 30, 2007 Post all relevant code real quick, and walk me through what it's suppose to do and what it's not doing (as opposed to what it should be) so I can get a better idea of what is going on. Then let me see if I can rewrite it to work for you. Quote Link to comment https://forums.phpfreaks.com/topic/71254-dynamic-form-processing/#findComment-358405 Share on other sites More sharing options...
Comdemned Posted September 30, 2007 Author Share Posted September 30, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/71254-dynamic-form-processing/#findComment-358412 Share on other sites More sharing options...
Comdemned Posted October 1, 2007 Author Share Posted October 1, 2007 Bump Quote Link to comment https://forums.phpfreaks.com/topic/71254-dynamic-form-processing/#findComment-358740 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.