Solved

Form self-validation and submission

Posted on 2000-05-12
8
266 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
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…

828 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