Solved

Form self-validation and submission

Posted on 2000-05-12
8
278 Views
Last Modified: 2007-11-27
This is a fairly common question that I have never been able to find a good answer for.  What I want to create is a series of forms that validate themselves, and if the information contained in them is correct, write to my database and go to the next form in a series.  I can do this if I write the page as one big .php3 file and use $PHP_SELF as the form action, but I want to break it up among several pages. How can I get the form to redisplay if it hits a flag that the page is incorrect, and go to the next page if the info is valid? Thanks guys.
0
Comment
Question by:vianetman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 8

Expert Comment

by:us111
ID: 2806523
I'm sure that it will work perfectely.
One form, check fields, insert into database and then go to next form.
If I understand what you need ....

<?
function TestField($f)
{
if ($f == "")
      print "<font color='#FF0000'>";
}

$field_list = array("Name", "Firstname");

if ($REQUEST_METHOD == "POST")
{ for($i=0; $i<count($field_list);$i++)
  {     if ($$field_list[$i] == "")
          $err=true;
  }
  if ($err<>true)
  {  #now insert into your databse
     # place your code here

     # then redirect to the next form
     header("Location: nextform.php3");
  }
}
?>

<form action="form.php3" method="post">
<?TestField($Name)?>Name</font>
<input type="text" value="<?print $Name?>" name="Name"><br>
<?TestField($Firstname)?>Firstname</font>
<input type="text" value="<?print $Firstname?>" name="Firstname"><br>
<input type="submit" value="submit" name="submit">
</form>
0
 

Author Comment

by:vianetman
ID: 2807514
That is sort of what I was trying to do, but I want to do it without a header() redirect function.  In some circumstances I want to validate the form on the same page, but still submit the data to the next page via the post method.  Any ideas?  Thanks.
0
 
LVL 8

Expert Comment

by:us111
ID: 2808217
I don't really understand why you need to post again the data to the next form after validating the form.
If you insert them in a database, you can easily get them in the next form.

well try
<?
include("nextform.php3")
exit();
?>

instead of header...
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 2

Expert Comment

by:freshmeat
ID: 2809062
why not try javascript on client?
0
 
LVL 8

Expert Comment

by:us111
ID: 2809633
I think it's better to use PHP, you don't depend of the browser.
0
 

Author Comment

by:vianetman
ID: 2811736
I mostly want to pass the data to the next page because I want it to pass some sort of identifying information, such as a user ID, so I will know to whom the data entered on the next page belongs to.  I know, I know, PHP4 has user tracking functions built in, but I cannot install any non-release quality software on the server, so I am stuck with PHP3.  Also, I am not going to spend all day installing the extra librarys needed for PHP3 to do session tracking.  I know the other option is client sided cookies, but I hate dealing with cookies.

Thanks for all the help. I am going to wait another day or two to give up the points in case someone has a miraculous answer that hasen't read this yet.
0
 
LVL 8

Accepted Solution

by:
us111 earned 100 total points
ID: 2813333
with the following solution, for example if you have an ID

<?  ### form.php3
                      function TestField($f)
                      {
                      if ($f == "")
                            print "<font color='#FF0000'>";
                      }

                      $field_list = array("Name", "Firstname");

                      if ($REQUEST_METHOD == "POST")
                      { for($i=0; $i<count($field_list);$i++)
                        {     if ($$field_list[$i] == "")
                                $err=true;
                        }
                        if ($err<>true)
                        {  #now insert into your databse
                           # place your code here

                           # then redirect to the next form
                           include("nextform.php3");
                           exit();
                        }
                      }
                      ?>

<form action="form.php3" method="post">
                      <?TestField($ID)?>ID</font>
                      <input type="hidden" value="<?print $ID?>" name="ID"><br>
                      <?TestField($Name)?>Name</font>
                      <input type="text" value="<?print $Name?>" name="Name"><br>
                      <?TestField($Firstname)?>Firstname</font>
                      <input type="text" value="<?print $Firstname?>" name="Firstname"><br>
                      <input type="submit" value="submit" name="submit">
                      </form>


and
nextform.php3
<?
........

........
?>
<form action="nextform.php3" method="post">
                      <input type="hidden" value="<?print $ID?>" name="ID"><br>
                      <input type="submit" value="submit" name="submit">
                      </form>



your ID will be define in the nextform.php3 page........

I'm lost with your question and your problem.
0
 

Author Comment

by:vianetman
ID: 2814614
I guess that will have to do.  PHP just does not have what I need here.  Sorry to be so picky.  Thanks for all of your help!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question