Solved

Form self-validation and submission

Posted on 2000-05-12
8
235 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
  • 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
 
LVL 2

Expert Comment

by:freshmeat
ID: 2809062
why not try javascript on client?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now