PDA

View Full Version : refreshing a page to add more form fields


misterx
01-20-2003, 08:13 AM
I'm totally vexed with writing a solution for this problem.

Basically I'm writing a poll script. On the page that creates the poll there are form fields for what the question should be and the available answers and so on. About half way through it occured to me that the user should be able to have as many or as little possible answers as they want. So I thought I would make a little field where they could set the number of possible answers, refresh(using some kind of handy refresh button), then have the page come back up with the appropriate number of fields to input answers into.

Is this making sense? I always have the hardest time trying to put these questions down.

bored
01-20-2003, 09:01 AM
You could use a for loop. For dynamic output.



$input_num = 5;

for($i = 0; $i <= $input_num - 1; $i++){

$a = $i + 1;

echo("Answer #$a: <input type=\"text\" name=\"answers[]\" value=\"\"><br>\r\n");

}


That would output as many input boxes as $input_num equaled to. In this case, 5 boxes.

The field names are set to answers[] to create an array when the data from the answers will be posted. You could also name the input fields like name=\"answer_$i\", which would make the text boxes named like "answer_0 answer_1" etc..

Hope that helped.

misterx
01-20-2003, 06:47 PM
That is somewhat helpful. Now can you tell me how to program a refresh button? I suppose I could use:

header("Location: script.php?input_num=x");


But will that keep any information they might have already entered as the poll question? I think I'll try it, but if anybody else has any suggestions they would be much appreciated.

bored
01-20-2003, 06:55 PM
You could use javascript ro refresh the page, like this:


<input type="text" name="input_num" size="5" value="">
<input type="button" value="Refresh" onClick="location.href='<?=$PHP_SELF?>?input_num=' + input_num.value">


As far as keeping the values, you would have to have someway to reload the data, thats a little more complicated, but can be done. Try setting it to where they user has to enter the number of answers first before actually filling in the answers. Just order of operations so you and the users can have a pattern of using the program.

misterx
01-20-2003, 07:06 PM
Clearly I'm going to have to play with it and see what I can come up with. Thanks for the help. :cool: