Redirecting Back To Forms

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
zunebuggy
Forum Commoner
Posts: 41
Joined: Wed Aug 27, 2008 1:22 pm

Redirecting Back To Forms

Post 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.
User avatar
deeessay
Forum Commoner
Posts: 55
Joined: Sat May 24, 2008 1:02 am

Re: Redirecting Back To Forms

Post 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...
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Redirecting Back To Forms

Post 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.
(#10850)
zunebuggy
Forum Commoner
Posts: 41
Joined: Wed Aug 27, 2008 1:22 pm

Re: Redirecting Back To Forms

Post 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>&nbsp;</td>";
echo "  </tr>";
echo "      </table>";
echo "      </td>";
echo "  </tr>";
echo "  <tr>";
echo "      <td>&nbsp;</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>&nbsp;</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>&nbsp;</td>";
echo "      <td height=\"441\">&nbsp;</td>";
echo "  </tr>";
echo "  <tr>";
echo "      <td width=\"72\">&nbsp;</td>";
echo "      <td width=\"910\">&nbsp;</td>";
echo "      <td height=\"52\" width=\"73\">&nbsp;</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)
 
 
 
?>
 
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Redirecting Back To Forms

Post 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);
(#10850)
Post Reply