Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remembering Array Values within Form 1

Status
Not open for further replies.

FAM

Technical User
Jan 13, 2003
345
GB
I have a form which when submitted checks to make sure data has been added to the fields, if not it requests the user to enter some valid data. When the user goes back i would like to retain the values which would of been entered within the forms array element.

$NumberAnswers ranges between 2 to 9.

Code:
for ($i=1;$i<=$NumberAnswers;$i++)   //Displays the number of answer boxes
{
   echo "<tr align='center'><td>$i\n</td>";
   echo "<td><input name='Value[]' type='text' size='60' maxlength='60' /><br /></td>";
   echo "<td><input type='radio' name='CorrectAnswer'value='$i'></td></tr>";
}

How could i get it to remember the values which would of been previously entered within the text boxes?

Thanks
 
include the value within the echo'd field elements.

eg:
Code:
$textfield1 = empty($_POST['textfield1'])?"":$_POST['textfield1'];
echo "<input name=\"textfield1\" type=\"text\" value=\"$textfield1\"/>";
 
I would set the values into session variables and then output those session variables inside the value attribute in your textboxes.
Code:
<input type="text" value="<? if(isset($_SESSION['one'])) echo $_SESSION['one']; ?>" />
If the session variable is not set, then they get an empty value attribute (which is perfectly valid).

At least that's one way you could do it [smile]

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
If the session variable is not set, then they get an empty value attribute (which is perfectly valid).

Correct me if I'm wrong, but if you have debugging dialed all the way up, you're going to produce warnings with this type of behavior. Hence jpadie's ternary logic line... however, I'm a bigger fan of isset() than empty() in case you're using things like 0 or "0".
 
Are you saying that my code block would throw an error if the session variable was not set? I don't think it will... my understanding is that it will not output anything (not even a warning) because of the test using isset(). And if it outputs nothing, the resulting HTML will contain the value attribute with an empty value - which is valid markup.

Please correct me if I'm wrong... I've been using this method for some time and I was under the impression it's totally valid and an acceptable way to do it. I've never learnt from a book - so I could easily be wrong on this.

Cheers,
jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Please correct me if I'm wrong... I've been using this method for some time and I was under the impression it's totally valid and an acceptable way to do it. I've never learnt from a book - so I could easily be wrong on this.

Woops, you are completely correct... I totally missed your use of ISSET() and then responded to your commentary (which I mis-understood)... hence the reason I suggested exactly the method you used...

My apologies.

 
Thanks for the quick responses, but as im not an expert im not sure if i am properly understanding either proposed method,

jpadie, are you referring 'textfield1' as the Value[] array?

BabyJeffy, are you referring 'one' as the Value[] array?

Thanks
 
I will try to explain as best as i can, (for testing purposes ive submitted the form back on itself to check that boxes have been populated) i.e.

$NumberAnswers = 3

this would then display a form showing,
1 [TextBox] [Radio Button]
2 [TextBox] [Radio Button]
3 [TextBox] [Radio Button]

when submitted the 3 values that were entered into the text box are put into an array variable,

$Value = $_POST['Value'];

i am trying to get it so as the text boxs are populated with what the had when the form was submitted.

Hope this explains what i am after a bit better...

Cheers
 
This isn't hard, but I'm drawing a blank right now... I can however give you a suggestion which should help you help yourself...

Code:
echo '<pre>';
print_r($_POST);
echo '</pre>';

That will dump the entire $_POST array to your screen, and you can see how everything from your form as been stored, be it in arrays, as simple variables, or mysteriously discarded (as sometimes happens if you name two text fields the same and don't use []'s)

... If that's not what you're looking for, then you're actually asking an HTML question, how to I pre-populate a text box, and the answer to that is illustrated above in both jpadie & Jeff's answers by using the value attribute.
 
Thanks but the form works fine when every field is populated and the data is submitted but im just trying to implement error checking to make sure every required field has values. Above the array i have a text box which populates depending on the variable i.e.
Code:
<input name="Question" type="text" value="<? echo $Question;?>" />

All i would like is for 'X' amount of textboxes to retain there values.

Any suggestions?
 
Right... so it's come down to if you can store them in an array or not... do a print_r($_POST); and you'll find out if you have
Code:
$_POST['Value'][0] = "First text box";
$_POST['Value'][1] = "Second text box";

Or if you your setup is invalid and you just have
Code:
$_POST['Value[]'] = "Last text box";

If it's the former, then I've just shown you the syntax you need... if it's the later, then you'll have to change
Code:
 echo "<td><input name='Value[]' type='text' size='60' maxlength='60' /><br /></td>";

to something more like

 echo "<td><input name='Value_.$i' type='text' size='60' maxlength='60' /><br /></td>";

and parse out the $i
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top