Help with holding a value during validation

I've only been working with PHP for five months during this time I'm trying
to improve a script written by someone by adding validation to the form. The
form contains seven step functions with it's on validation for each page. My
problem comes when the page is validated and the user has put a check in the
radio button or a drop down list I can't get the script to apply that check
back into the form when the user has to correct something else. Below is
step3 with calls the function in this case I'm trying to get AplyMonths to
remember that either yes or no was checked but again once the form is
validated with an error the check comes out and the user has to select it
again. Any help how to make this work please let me know.

Thanks,
Rob

function displayStep3 ()
{
/*
* [at] author RFM
* [at] version 1.0
* [at] date 4/20/2005
* [at] revhist
*
*/


// Get State List
$this->stateList();

$this->mainContent = "
<table width=\"400\" border=\"0\" cellspacing=\"0\" cellpadding=\"1\">

<input type=\"hidden\" name=\"action\" value=\"3\">
<input type=\"hidden\" name=\"job\" value=\"$this->job\">
<input type=\"hidden\" name=\"jobtype\" value=\"$this->jobtype\">
<input type=\"hidden\" name=\"OptOutEEOC\" value=\"$this->OptOutEEOC\">
<input type=\"hidden\" name=\"sex\" value=\"$this->sex\">
<input type=\"hidden\" name=\"birthday\" value=\"$this->birthday\">
<input type=\"hidden\" name=\"origin\" value=\"$this->origin\">
<input type=\"hidden\" name=\"Citizenship\" value=\"$this->Citizenship\">

<input type=\"hidden\" name=\"handicap_blind\"
value=\"$this->handicap_blind\">
<input type=\"hidden\" name=\"handicap_deaf\"
value=\"$this->handicap_deaf\">
<input type=\"hidden\" name=\"handicap_communicative\"
value=\"$this->handicap_communicative\">
<input type=\"hidden\" name=\"handicap_orthopedic\"
value=\"$this->handicap_orthopedic\">
<input type=\"hidden\" name=\"handicap_Weight_lifting_restriction\"
value=\"$this->handicap_Weight_lifting_restriction\">
<input type=\"hidden\" name=\"handicap_drugs_alcohol\"
value=\"$this->handicap_drugs_alcohol\">
<input type=\"hidden\" name=\"handicap_other\"
value=\"$this->handicap_other\">

<input type=\"hidden\" name=\"Disabled_Veteran\"
value=\"$this->Disabled_Veteran\">
<input type=\"hidden\" name=\"Vietnamera_Veteran\"
value=\"$this->Vietnamera_Veteran\">

<input type=\"hidden\" name=\"state\" value=\"$this->state\">
<input type=\"hidden\" name=\"perm_state\" value=\"$this->perm_state\">
<tr>
<td valign=\"top\" colspan=\"2\"><font color=\"red\">*</font><small>
Indicates Required</small></td>
</tr>
<tr>
<td valign=\"top\" colspan=\"2\"><img src=\"/images/spacer.gif\"
width=\"1\" height=\"1\" alt=\"\" border=\"0\"></td>
</tr>
<tr>
<td valign=\"top\" colspan=\"2\"><div class=\"head2\">Personal
Information</div></td>
</tr>

<tr>
<td valign=\"top\" colspan=\"2\"><img src=\"/images/spacer.gif\"
width=\"1\" height=\"1\" alt=\"\" border=\"0\"></td>
</tr>

<tr>
<td width=\"150\">Have you applied in <br>the last 12 months?</td>
<td width=\"\"><input type=\"radio\" name=\"AplyMonths\" id=\"AplyMonths\"
value=\"Y\">Yes"; if (AplyMonths == "N")
echo "checked";$this->mainContent .= "
<input type=\"radio\" name=\"AplyMonths\" value=\"N\">No"; if (AplyMonths
== "N")
echo "checked";$this->mainContent .= " <br>
</td>
</tr>


<tr>
<td width=\"150\">First Name<font color=\"red\">*</font></td>
<td width=\"\"><input type=\"text\" name=\"fname\" value=\"$this->fname\"
size\"24\" maxlength=\"15\"></td>
</tr>";
if ($this->error == 1)
{
$this->mainContent .= "
<tr>
<td> </td>
<td valign=\"top\"><div class=\"errorText\">Please Provide Your First
Name</div></td>
</tr>";
}
$this->mainContent .= "
<tr>
<td>Middle Name</td>
<td><input type=\"text\" name=\"mname\" value=\"$this->mname\" size\"24\"
maxlength=\"15\"></td>
</tr>
<tr>
<td>Last Name<font color=\"red\">*</font></td>
<td><input type=\"text\" name=\"lname\" value=\"$this->lname\" size\"24\"
maxlength=\"25\"></td>
</tr>
";
if ($this->error == 2)
{
$this->mainContent .= "
<tr>
<td> </td>
<td valign=\"top\"><div class=\"errorText\">Please Provide Your Last
Name</div></td>
</tr>";
}
$this->mainContent .= "
<tr>
<td>Maiden Name</td>
<td><input type=\"text\" name=\"maid_name\" value=\"$this->maid_name\"
size\"24\" maxlength=\"25\"></td>
</tr>
<tr>
<td>Social Sec. Number<font
color=\"red\">*</font><br><small>example:000112222 (No dashes)</small></td>
<td><input type=\"text\" name=\"ssNum\" value=\"$this->ssNum\" size\"9\"
maxlength=\"9\"></td>
</tr>
";
if ($this->error == 3)
{
$this->mainContent .= "
<tr>
<td> </td>
<td valign=\"top\"><div class=\"errorText\">Please Provide Your SS
#</div></td>
</tr>";
}
$this->mainContent .= "
<tr>
<td>Email</td>
<td><input type=\"text\" name=\"email\" value=\"$this->email\" size\"24\"
maxlength=\"100\"></td>
</tr>";
if ($this->error == 13)
{
$this->mainContent .= "
<tr>
<td> </td>
<td valign=\"top\"><div class=\"errorText\">Please Provide a valid
Email</div></td>
</tr>";
}
$this->mainContent .= "

<tr>
<td valign=\"top\" colspan=\"2\"><img src=\"/images/spacer.gif\"
width=\"1\" height=\"5\" alt=\"\" border=\"0\"></td>
</tr>
<tr>
<td valign=\"top\" colspan=\"2\"><div class=\"head2\">Present Address</div>
</tr>
<tr>
<td>Street Address 1<font color=\"red\">*</font></td>
<td><input type=\"text\" name=\"addr1\" value=\"$this->addr1\" size\"24\"
maxlength=\"25\"></td>
</tr>
";
if ($this->error == 4)
{
$this->mainContent .= "
<tr>
<td> </td>
<td valign=\"top\"><div class=\"errorText\">Please Provide Your Street
Address</div></td>
</tr>";
}
$this->mainContent .= "
<tr>
<td>Street Address 2</td>
<td><input type=\"text\" name=\"addr2\" value=\"$this->addr2\" size\"24\"
maxlength=\"25\"></td>
</tr>
<tr>
<td>City<font color=\"red\">*</font></td>
<td><input type=\"text\" name=\"city\" value=\"$this->city\" size\"24\"
maxlength=\"19\"></td>
</tr>
";
if ($this->error == 5)
{
$this->mainContent .= "
<tr>
<td> </td>
<td valign=\"top\"><div class=\"errorText\">Please Provide Your
City</div></td>
</tr>";
}
$this->mainContent .= "
<tr>
<td>State<font color=\"red\">*</font></td>
<td><select name=\"state\">$this->stateDropDown</select></td>
</tr>
";
if ($this->error == 6)
{
$this->mainContent .= "
<tr>
<td> </td>
<td valign=\"top\"><div class=\"errorText\">Please Provide Your
State</div></td>
</tr>";
}
$this->mainContent .= "
<tr>
<td>Zip Code<font color=\"red\">*</font></td>
<td><input type=\"text\" name=\"zip\" value=\"$this->zip\" size\"5\"
maxlength=\"5\"></td>
</tr>
";
if ($this->error == 7)
{
$this->mainContent .= "
<tr>
<td> </td>
<td valign=\"top\"><div class=\"errorText\">Please Provide Your
Zip</div></td>
</tr>";
}
$this->mainContent .= "
<tr>
<td>Telephone<font color=\"red\">*</font><br><small>example: 0001112222 (No
dashes)</small></td>
<td><input type=\"text\" name=\"phone\" value=\"$this->phone\" size\"12\"
maxlength=\"10\"></td>
</tr>
";
if ($this->error == 8)
{
$this->mainContent .= "
<tr>
<td> </td>
<td valign=\"top\"><div class=\"errorText\">Please Provide Your Phone
Number</div></td>
</tr>";
}
$this->mainContent .= "
<tr>
<td>Cell Phone<br><small>example: 0001112222 (No dashes)</small></td>
<td><input type=\"text\" name=\"cell\" value=\"$this->cell\" size\"12\"
maxlength=\"10\"></td>
</tr>";
if ($this->error == 9)
{
$this->mainContent .= "
<tr>
<td> </td>
<td valign=\"top\"><div class=\"errorText\">Please Provide Your Cell Phone
Number</div></td>
</tr>";
}
$this->mainContent .= "
<tr>
<td valign=\"top\" colspan=\"2\"><img src=\"/images/spacer.gif\"
width=\"1\" height=\"5\" alt=\"\" border=\"0\"></td>
</tr>
<tr>
<td valign=\"top\" colspan=\"2\"><div class=\"head2\">Permanent Address
<small>(if different)</small></div>
</tr>
<tr>
<td>Street Address 1</td>
<td><input type=\"text\" name=\"perm_addr1\" value=\"$this->perm_addr1\"
size\"24\" maxlength=\"24\"></td>
</tr>
<tr>
<td>Street Address 2</td>
<td><input type=\"text\" name=\"perm_addr2\" value=\"$this->perm_addr2\"
size\"24\" maxlength=\"24\"></td>
</tr>
<tr>
<td>City</td>
<td><input type=\"text\" name=\"perm_city\" value=\"$this->perm_city\"
size\"24\" maxlength=\"19\"></td>
</tr>
<tr>
<td>State</td>
<td><select name=\"perm_state\">
$this->stateDropDown
</select></td>
</tr>
<tr>
<td>Zip Code</td>
<td><input type=\"text\" name=\"perm_zip\" value=\"$this->perm_zip\"
size\"5\" maxlength=\"5\"></td>
</tr>";
if ($this->error == 10)
{
$this->mainContent .= "
<tr>
<td> </td>
<td valign=\"top\"><div class=\"errorText\">Please Provide Your Zip
Code</div></td>
</tr>";
}
$this->mainContent .= "
<tr>
<td>Telephone<br><small>example: 0001112222 (No dashes)</small></td>
<td><input type=\"text\" name=\"perm_phone\" value=\"$this->perm_phone\"
size\"12\" maxlength=\"10\"></td>
</tr>";
if ($this->error == 11)
{
$this->mainContent .= "
<tr>
<td> </td>
<td valign=\"top\"><div class=\"errorText\">Please Provide Your Phone
Number</div></td>
</tr>";
}
$this->mainContent .= "
<tr>
<td valign=\"top\" colspan=\"2\"><img src=\"/images/spacer.gif\"
width=\"1\" height=\"10\" alt=\"\" border=\"0\"></td>
</tr>
</table>
";



return;
} // displayStep3


/*********************************************************** **********/

function processStep3 ()
{
/*
* [at] author RFM
* [at] version 1.0
* [at] date 4/20/2005
*
*
*/

$this->validateStep3();


return;
} // processStep2


/*********************************************************** **********/

function validateStep3 ()
{
/*
* [at] author RFM
* [at] version 1.0
* [at] date 4/20/2005
* [at] revhist
*/

$this->error = 0;

if ($this->fname == ""){$this->error = 1;}
elseif ($this->lname == ""){$this->error = 2;}
elseif (($this->ssNum == "") ||
(!ereg("^[0-9]{9}",$this->ssNum))){$this->error = 3;}
elseif ($this->addr1 == ""){$this->error = 4;}
elseif ($this->city == ""){$this->error = 5;}
elseif ($this->state == ""){$this->error = 6;}
elseif (($this->zip == "") ||
(!ereg("^[0-9]{5}",$this->zip))){$this->error = 7;}
elseif (($this->phone == "") ||
(!eregi("^[0-9]{3}[0-9]{3}[0-9]{4}$",$this->phone))){$this-> error = 8;}
elseif (($this->email != "") &&
(!eregi("^[a-z0-9._-]+ [at] +[a-z0-9._-]+.+[a-z]{2,3}$",
$this->email))){$this->error = 13;}
elseif (($this->cell != "") &&
(!eregi("^[0-9]{10}$",$this->cell))){$this->error = 9;}
elseif (($this->perm_zip != "") &&
(!eregi("^[0-9]{5}$",$this->perm_zip))){$this->error = 10;}
elseif (($this->perm_phone != "") &&
(!eregi("^[0-9]{10}$",$this->perm_phone))){$this->error = 11;}


if ($this->error != 0)
{
//Form Error; Redisplay
$this->displayStep3();
}
else
{
$this->displayStep4();
}


return;
} // validateStep3
rsvore [ So, 18 März 2007 14:19 ] [ ID #1661141 ]

Re: Help with holding a value during validation

rsvore wrote:
> I've only been working with PHP for five months during this time I'm trying
> to improve a script written by someone by adding validation to the form. The
> form contains seven step functions with it's on validation for each page. My
> problem comes when the page is validated and the user has put a check in the
> radio button or a drop down list I can't get the script to apply that check
> back into the form when the user has to correct something else. Below is
> step3 with calls the function in this case I'm trying to get AplyMonths to
> remember that either yes or no was checked but again once the form is
> validated with an error the check comes out and the user has to select it
> again. Any help how to make this work please let me know.

First of all, youw code is a bit difficult to read IMHO, it's easier if you
use a few more newlines.

<input type=\"radio\" name=\"AplyMonths\" id=\"AplyMonths\" value=\"Y\">Yes";
if (AplyMonths == "N")
echo "checked";$this->mainContent .= "
<input type=\"radio\" name=\"AplyMonths\" value=\"N\">No"; if (AplyMonths
== "N")
echo "checked";

"checked" works only if you put it inside the input tag. And I'm not usre if
you have defined the value for "AplyMonths" correctly, maybe better you use a
variable instead.


$AplyMonths=$_REQUEST['AplyMonths'];

$this->mainContent .= "<input type=\"radio\" name=\"AplyMonths\"
id=\"AplyMonths\" value=\"Y\"";

if($AplyMonths == "Y") { $this->mainContent .= " checked"; }

$this->mainContent .=">Yes\n<input type=\"radio\" name=\"AplyMonths\"
value=\"N\"";

if($AplyMonths == "N") { $this->mainContent .= " checked"; }

$this->mainContent .= ">No\n";


In the same manner you need to test each option in your drop down menu for a
match with the old value

$this->mainContent .= "<option value=\"something\"";
if($SelectName=="something") { $this->mainContent .= " selected"; }
$this->mainContent .= ">SomeThing</option>\n";

Adding a new line after each thing you done, will make the HTML source a lot
more readable and easier for you to track problems. You may also want to take
a look at CSS and how to place html-elements instead of using the table you
are, good place for help is alt.html

--

//Aho
Shion [ So, 18 März 2007 13:52 ] [ ID #1661142 ]
PHP » alt.php » Help with holding a value during validation

Vorheriges Thema: Official website of Burt Young
Nächstes Thema: php and shoutcast