Solved

Form self-validation and submission

Posted on 2000-05-12
8
247 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

896 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

13 Experts available now in Live!

Get 1:1 Help Now