Page 1 of 1
Redirecting Back To Forms
Posted: Thu Sep 18, 2008 8:41 pm
by zunebuggy
I am new to PHP. I have an HTML form that sends values to a PHP script. There are four fields on my form and all four are required. I can handle the If statements in my PHP script if any fields are left blank, but what I do not know how to do is to stop the PHP script (if any are left blank) and return (or redirect) back to the HTML form.
Re: Redirecting Back To Forms
Posted: Fri Sep 19, 2008 12:31 am
by deeessay
Then you'll gonna need a little bit of javascript.
okay as an example let's use a single text field
Code: Select all
<html>
<head>
<script language='javascript' type='text/javascript'>
function verifyfields ()
{
if (document.frm_test.txt_name.value == "")
{
alert ("There is no value in the Name text field");
return false;
}
else
return true;
}
</script>
</head>
<body>
<form name='frm_test' action='nextpage.php' method='POST' onsubmit='return verifyfields();'>
<input type='text' name='txt_name'>
<br>
<input type='submit' value='Submit'>
</form>
</body>
</html>
I haven't checked this yet if it is actually working but I hope this helps...
Re: Redirecting Back To Forms
Posted: Fri Sep 19, 2008 12:33 am
by Christopher
You should have your PHP script include the HTML form page. The have it submit to itself. You can show the blank form first time on the page, show errors and repopulate values if there are problems, or process the data and redirect done page.
Re: Redirecting Back To Forms
Posted: Fri Sep 19, 2008 9:47 pm
by zunebuggy
OK. Using the concept of posting the form back to itself, I made the following, but it displays the "Instructions telling user that this field cannot be blank" even on the first time the form is displayed. I want the first time to be blank and if any fields are left blank, it will only display that message next to the blank field.
Code: Select all
<?php
function search_frm($req_field1, $req_field2, $req_field3, $req_field4)
{
echo "<html>";
echo "<head>";
echo "<meta http-equiv=\"Content-Language\" content=\"en-us\">";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1252\">";
echo "<title>My Title Here</title>";
echo "</head>";
echo "<body>";
echo "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"1055\" height=\"571\">";
echo " <tr>";
echo " <td valign=\"top\" colspan=\"3\" height=\"78\">";
echo " <img border=\"0\" src=\"my_image.jpg\" width=\"360\" height=\"118\"><table border=\"0\" width=\"100%\" id=\"table1\">";
echo " <tr>";
echo " <td> </td>";
echo " </tr>";
echo " </table>";
echo " </td>";
echo " </tr>";
echo " <tr>";
echo " <td> </td>";
echo " <td valign=\"top\">";
echo " <form method=\"POST\" action=\"search2.php\">";
echo " <table border=\"0\" width=\"100%\" id=\"table2\">";
echo " <tr>";
echo " <td width=\"83\"><b><font size=\"2\">Required Search Field 1</font></b></td>";
echo " <td width=\"283\"><input type=\"text\" name=\"reqfield1\" size=\"38\"></td>";
echo " <td><font size=\"2\" color=\"#FF0000\">" . $req_field1 . "</font></td>";
echo " </tr>";
echo " <tr>";
echo " <td width=\"83\"><b><font size=\"2\">Required Search Field 2</font></b></td>";
echo " <td width=\"283\"><input type=\"text\" name=\"reqfield2\" size=\"38\"></td>";
echo " <td><font size=\"2\" color=\"#FF0000\">" . $req_field2 . "</font></td>";
echo " </tr>";
echo " <tr>";
echo " <td width=\"83\"><b><font size=\"2\">Required Search Field 3</font></b></td>";
echo " <td width=\"283\"><input type=\"text\" name=\"reqfield3\" size=\"38\"></td>";
echo " <td><font size=\"2\" color=\"#FF0000\">" . $req_field3 . "</font></td>";
echo " </tr>";
echo " <tr>";
echo " <td width=\"83\"><b><font size=\"2\">Required Search Field 4</font></b></td>";
echo " <td width=\"283\"><input type=\"text\" name=\"reqfield4\" size=\"38\"></td>";
echo " <td><font size=\"2\" color=\"#FF0000\">" . $req_field4 . "</font></td>";
echo " </tr>";
echo " <tr>";
echo " <td width=\"83\"><b><font size=\"2\">Dropdown</font></b></td>";
echo " <td width=\"283\"><select size=\"1\" name=\"D1\"></select></td>";
echo " <td> </td>";
echo " </tr>";
echo " </table>";
echo " <p><input type=\"submit\" value=\"Submit\" name=\"B1\"><input type=\"reset\" value=\"Reset\" name=\"B2\"></p>";
echo " </form>";
echo " <p> </td>";
echo " <td height=\"441\"> </td>";
echo " </tr>";
echo " <tr>";
echo " <td width=\"72\"> </td>";
echo " <td width=\"910\"> </td>";
echo " <td height=\"52\" width=\"73\"> </td>";
echo " </tr>";
echo "</table>";
echo "</body>";
echo "</html>";
}
if ($reqfield1 == "")
{$req_field1 = 'Instructions telling user that this field cannot be blank.';}
if ($reqfield2 == "")
{$req_field2 = 'Instructions telling user that this field cannot be blank.';}
if ($reqfield3 == "")
{$req_field3 = 'Instructions telling user that this field cannot be blank.';}
if ($reqfield4 == "")
{$req_field4 = 'Instructions telling user that this field cannot be blank.';}
search_frm($req_field1, $req_field2, $req_field3, $req_field4)
?>
Re: Redirecting Back To Forms
Posted: Fri Sep 19, 2008 10:20 pm
by Christopher
Code: Select all
if (strtoupper($_SERVER[REQUEST_METHOD']) == 'POST') {
$errors = array();
if ($reqfield1 == "")
{$errors[1] = 'Instructions telling user that this field cannot be blank.';}
if ($reqfield2 == "")
{$errors[2] = 'Instructions telling user that this field cannot be blank.';}
if ($reqfield3 == "")
{$errors[3] = 'Instructions telling user that this field cannot be blank.';}
if ($reqfield4 == "")
{$errors[4] = 'Instructions telling user that this field cannot be blank.';}
if (! $errors) {
header('Location: mynewpage.php');
exit;
}
}
search_frm($errors);