steveangelis Posted June 7, 2010 Share Posted June 7, 2010 Without going into the complete coding, I have a page that calls up data from a form using the $_POST command. This works just as it should, but I am trying to add a twist to it but it is not working. The page it loads from has a feature that automaticly generates a set number of fields to be entered based on the previous page like this: Page 1: Select number of fields Page 2: Display the number of fields based on page one. What I am working on is page 3, which is to get the $_POST of each field. I have each field set so it auto generated a number with each field like this: <input type="text" name="question<?PHP echo $num_q; ?>"> $num_q is set so that it is always one higher than the previous field, thus there will never be fields with the same name. Now when trying to call up the data, I tried this but if failed: $num_ans = $_POST['question'.num_q]; $num_q was previously defined and set so it would go up by one every time also. How would I get this to show up properly? Have I made any sense with this? Quote Link to comment https://forums.phpfreaks.com/topic/204119-post-and-variables/ Share on other sites More sharing options...
aeroswat Posted June 7, 2010 Share Posted June 7, 2010 Without going into the complete coding, I have a page that calls up data from a form using the $_POST command. This works just as it should, but I am trying to add a twist to it but it is not working. The page it loads from has a feature that automaticly generates a set number of fields to be entered based on the previous page like this: Page 1: Select number of fields Page 2: Display the number of fields based on page one. What I am working on is page 3, which is to get the $_POST of each field. I have each field set so it auto generated a number with each field like this: <input type="text" name="question<?PHP echo $num_q; ?>"> $num_q is set so that it is always one higher than the previous field, thus there will never be fields with the same name. Now when trying to call up the data, I tried this but if failed: $num_ans = $_POST['question'.num_q]; $num_q was previously defined and set so it would go up by one every time also. How would I get this to show up properly? Have I made any sense with this? Well obviously the num_q doesn't pass to the next page. The best thing would be to have a hidden field posted with the 2nd form that says the maximum num_q value. Then you can do a for loop on page 3 that will concat the enumerated value in your for loop to your POST variable something like this for($i = 0;$i<$_POST['num_q_max'];$i++) { echo $_POST['question' . $i]; } Quote Link to comment https://forums.phpfreaks.com/topic/204119-post-and-variables/#findComment-1069135 Share on other sites More sharing options...
steveangelis Posted June 7, 2010 Author Share Posted June 7, 2010 That is actually exactly what I have done. The problem lies with the code here not working: $num_ans = $_POST['question'.num_q]; All that does is show me the variable num_q. Quote Link to comment https://forums.phpfreaks.com/topic/204119-post-and-variables/#findComment-1069138 Share on other sites More sharing options...
mrMarcus Posted June 7, 2010 Share Posted June 7, 2010 what do you mean it "shows you the variable num_q"? Quote Link to comment https://forums.phpfreaks.com/topic/204119-post-and-variables/#findComment-1069143 Share on other sites More sharing options...
steveangelis Posted June 7, 2010 Author Share Posted June 7, 2010 I do an echo of the $_POST and all I get is the num_q showing up. Quote Link to comment https://forums.phpfreaks.com/topic/204119-post-and-variables/#findComment-1069144 Share on other sites More sharing options...
aeroswat Posted June 8, 2010 Share Posted June 8, 2010 That is actually exactly what I have done. The problem lies with the code here not working: $num_ans = $_POST['question'.num_q]; All that does is show me the variable num_q. If your code looks exactly the way you have posted it here then that's not what you are doing. You need to make a new enumerated variable. Unless num_q is a variable included in the action file then you are not doing this correctly. POST does not assume that you are trying to increment the variables like you did in your form. Without seeing your entire code I can do nothing but assume that you are doing what I have posted here and can not further help you. I don't believe anyone else can either. So till ya post your code... Good luck Quote Link to comment https://forums.phpfreaks.com/topic/204119-post-and-variables/#findComment-1069403 Share on other sites More sharing options...
litebearer Posted June 8, 2010 Share Posted June 8, 2010 Bored this Am, so I figured I'd give this a shot (not sure if is exactly what you are looking for). Form Page (example) <?PHP /* set the number of fields from whereever /* $num_fields_one = 7; /* set your incrementing variable */ $i = 0; ?> <form action="dynamic_fields_2.php" method="post"> <?PHP /* loop thru naming and displaying your input fields */ while($i<$num_fields_one) { $field_name = "my_field". $i; ?> <?PHP echo "this is field named " . $field_name . " "; ?><input type="text" name="<?PHP echo $field_name; ?>" size="10" maxlength="10" value=""><br> <?PHP $i ++; } ?> <input type="submit" value="submit"> </form> <?PHP ?> Processing Page <?PHP /* set your incrementing variable */ $i=0; /* loop thru the post variables puting the values into an array */ foreach($_POST as $key => $value){ $field[$i] = $value; $field_name[$i] = $key; $i++; } /* count the elements in the array (reduce by 1 as the last element contains the number of elements) */ $count = count($field) - 1; /* use the values as you choose - here we simply display them */ $i=0; while($i<$count) { echo "the field name is " . $field_name[$i] ." and its value is " . $field[$i] . "<br>"; $i++; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/204119-post-and-variables/#findComment-1069446 Share on other sites More sharing options...
steveangelis Posted June 10, 2010 Author Share Posted June 10, 2010 I was able to modify this enough with my current code to get it to work. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/204119-post-and-variables/#findComment-1070218 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.