Retain text box value if form validation failed. PHP script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
totoylubacon
Forum Newbie
Posts: 7
Joined: Thu Mar 01, 2012 5:48 am

Retain text box value if form validation failed. PHP script

Post by totoylubacon »

Please help!

I want my form retain the value of text boxes if form validation failed. I am using php multiple rows update in one submit. I have a php form validation script that will prevent the user from submitting the form if one of the the text boxes is empty. But my form does not retains the user-input data into my form text boxes if form validation return errors. Below is my excerpt html and php script.

//form validation
while($i< $numrows ){
if(empty($_POST['SQ1'][$i])){
$error[] = 'Please fill-up the form completely';
}

if(empty($error)){
//update sql statement

}else{

echo ''Please fill-up the form completely. ";
}
?>
<form action="<?php $_SERVER['PHP_SELF'];?>" method="post" name="compute" id="compute" />
<table align="center" border="1" cellpadding="2" cellspacing="2" cols=""><font color="black">
<tr>
<td colspan="3" width="40%" align="center">NAME</td><td width="5%" align="center">SQ1</td><td width="5%" align="center">SQ2</td><td width="5%" align="center">SQ3</td><td width="5%" align="center">RC/BW</td>
<td width="5%" align="center">EXAM</td><td width="5%" align="center">ATTND</td><td width="5%" align="center">PRJ</td>
</tr>
<?php
$num=0;
$index=0;
while($row=mysql_fetch_array($sqlGetSub)){
$num++;


?>


<tr>
<td width="2%"><?php echo $num;?></td>
<td width="10%" align="left"><input type="text" name="lastname[]" id="lastname" readonly="readonly" size="20" value="<?php echo $row['LNAME'];?>">
</td>

<td width="5%" align="left"><input type="text" name="SQ1[]" id="SQ1" size="2" value="<?php if (isset($_POST['SQ1[]']))echo $_POST['SQ1'];?>">
</td>
<td width="5%" align="left"><input type="text" name="SQ2[]" id="SQ2" size="2" value="<?php if (isset($_POST['SQ2[]']))echo $_POST['SQ2[]'];?>">
<tr>
<td colspan="10" align="center"><input type="submit" name="compute" value="Submit" class ="buttonSubmit" /></td>

</tr>

</td>
</table>
</form>
<?php


?>
Please help..

Thanks.
User avatar
Robert07
Forum Contributor
Posts: 113
Joined: Tue Jun 17, 2008 1:41 pm

Re: Retain text box value if form validation failed. PHP scr

Post by Robert07 »

It appears that you are using the same array definition for each row (SQ1[] and SQ2[]). If you want to specify each one you'll need to add array indexes, like SQ1[$num] and SQ2[$num]. Try adding this at the top to see what you're posting and it might help you understand better how to set it up:
echo "POSTED:<br>";
if (isset($_POST)) print_r($_POST);
totoylubacon
Forum Newbie
Posts: 7
Joined: Thu Mar 01, 2012 5:48 am

Re: Retain text box value if form validation failed. PHP scr

Post by totoylubacon »

Thanks for the very nice suggestion robert07. I got it.

here is what i did.

<td width="5%" align="left"><input type="text" name="SQ1[]" id="SQ1" size="2" value="<?php if (isset($_POST['SQ1'][$index]))echo $_POST['SQ1'][$index];?>">
Post Reply