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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display value from an array after form validation 3

Status
Not open for further replies.

milo3169

Programmer
May 2, 2007
42
0
0
I hope someone will be able to help me on this.

I am validating a form in PHP. If the validation fails, it includes the form to display the errors and the fields that passed the validation have the text that was input from before. However, I have an input field that is generated from an array and when the validation fails, the fields come back blank in the qty textbox.
Code:
<input class='qty-input-box' type='textbox' name='qty[]' size='1' value='"not sure what to put here"'/>

I am able to take the "qty[]" array to process it after form submission, but if the validation fails I don't know how to display the information back into the textbox like you would on a normal textbox ei... $_POST["var"]. I've been having a lot of trouble trying to figure this out. Does anybody have any insight on how to do this?
 
The idea is this.
[tt]
qty[0]:<input type="textbox" name="qty[]" value="<?php echo $_POST['qty'][0]; ?>" /><br />
qty[1]:<input type="textbox" name="qty[]" value="<?php echo $_POST['qty'][1]; ?>" /><br />
qty[2]:<input type="textbox" name="qty[]" value="<?php echo $_POST['qty'][2]; ?>" /><br />
[/tt]
The index has to be specifically taken care of during the authoring of the page, neither dynamic - inconvenient at time and error-prone.
 
To add to tsuji suggestion, your PHP could look like
Code:
$values=$_POST['qty'];
for($x=0; $x<sizeof($values); $x++) 
{

echo "<input class='qty-input-box' type='textbox' name='qty[]' size='1' value='".$value[$x]."' " />

}



--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Thanks tsuji & southbeach for all of your help. That worked!
 
Now that you're there, make this correction I overlooked in your input tag. Type is "text" not "textbox". "Textbox" is long gone(?) html2. But since that if the browser does not recognize a type attribute value or that it is backward compatible on this specific value - I have no idea on that point, - it flips it to the default "text" input element, hence, it works apparently without error.
 
@tsuji:
Type is "text" not "textbox". "Textbox" is long gone(?) html2.
Funny, I looked at that and stopped for a moment, I was not sure and had no time to research it so I left it alone.

Now, I do not need to bother with it sine tsuji brought it to our attention.

Thanks!


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top