Jump to content

form validation


RobMs

Recommended Posts

hey all,

 

Just  wondering if I can pick your brains.

I have a page with two forms that send information back to the current page (so form 1 and form 2).

First the user has to fill in form 1, then they are presented with form 2.

In my validation for form 2 I have to check whether an array contain any empty items, this I can do, however this causes form 1 to be displayed again (because Im checking what form was submitted by the submit button)

eg:

 if(isset($_POST['submit']){ deal with form1, display form 2 } else if(isset($_POST['submit2']){ deal with form 2} else { display form 1}

 

Is there anyway I can get back to form 2, without majorly modifying my code and having to load data back into form2 with session data? I also want to avoid javascript too.

 

Any ideas? if I could make the browser go back, it would be handy :-/

     

Link to comment
Share on other sites

Have no idea where you are having a problem. I would assume that the Form 2 validation occurs in this section

else if(isset($_POST['submit2'])
{
   deal with form 2
}

 

So, if validation fails, display form 2. The line of code you displayed above has nothing to do with what form is displayed. That apparently happens later. Personally I would have the actual forms as separate files so you can include() whichever one you need based upon what was submitted and the validation.

Link to comment
Share on other sites

I would use logic similar to this

 

<?php

if(isset($_POST['submit'])
{
    //Form 1 was submitted, validate data
    $valid = true;
    if(empty($_POST['name'])) { $valid = false; }

    if(!$valid)
    {
        //Form 1 validation failed, redisplay the form
        $include = 'form1.php';
    }
    else
    {
        //Form 1 data is valid.
        //Save the data somewhere and display form 2
        $include = 'form1.php';
    }
}
else if(isset($_POST['submit2'])
{
    //Form 1 was submitted
    if(!$valid)
    {
        //Form 2 validation failed, redisplay the form
        $include = 'form2.php';
    }
    else
    {
        //Form 2 data is valid.
        //Save the data somewhere and display confirmation page
        $include = 'thank_you.php';
    }
}
else
{
    //No data was, submitted display form 1
    $include = 'form1.php';
}
?>
<html>
<body>
<?php include($include); ?>
</body>
</html>

Link to comment
Share on other sites

The main issue is losing the users input:

 

if(isset($_POST['submit'])

{ deal with form1, display form 2 }

else if(isset($_POST['submit2']){ deal with form 2}

 

^^ if it fails validation here, next time it returns to this same page it doesnt have submit or submit2 so it displays what is in the else ( which is an empty form 1), ideally I would like to return to form 2 and have the users data still in the form elements

 

else { display form 1}

 

 

Validation is happening like so: else if (isset($_POST['submit2']) && !isEmpty($_POST['fieldname']) ) {

Link to comment
Share on other sites

I always create my forms to populate the post data for when validation fails. Not sure what you mean by "if it fails validation here, next time it returns to this same page it doesnt have submit or submit2". If validation fails on page 2 don't you reload page 2? What are you doing with the data from page 1 when validation passes?

 

All you need to do is detemine how you will store the data when validation passes and then pull that data as needed. Here is what I would do. When validation passes for form 1, load form 2 with hidden fields to capture all the data from form 1. Then when validation fails for form 2, redisplay the form entering all the previous user submitted values (including the hidden fields). So you will then maintain the data previously submitted.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.