Solved

PHP Validation / Form Post problem

Posted on 2007-11-15
3
399 Views
Last Modified: 2010-05-18
HI, I have a huge problem with checkout forms on my shopping cart.

As with most carts, the user must go through 3 or 4 steps to check and purchase products.

I have decided to do form validation on server side (i.e. using PHP), i think it's a little safer.

I have the following form ;

switch ($mode)
{
      case 'validate:
            // For register_global on PHP settings
            $first = mysql_real_escape_string($_POST['txtPaymentFirstName']);
                        
            //check mandatory fields
            if (empty($first)){ $first_error = "FIRST NAME IS MANDATORY"; }

            //Check for no errors, if no errors then move to next step
            if (empty($first_error)){
                 echo "<script language=\"JavaScript\"> window.location = '" . $_SERVER['PHP_SELF'] . "?step=2'</script>";      
                     exit;
            }      
                  
            break;
      }

<form action="<?php echo $_SERVER['PHP_SELF']; ?>?mode=validate&step=1" method="post" name="frmCheckout" id="frmCheckout">

      <input name="txtPaymentFirstName" type="text" class="box" id="txtPaymentFirstName" value="<?PHP echo $first ?>" size="30" maxlength="50" />
      <?PHP  if (empty($first_error)){} else{ echo $first_error; }  ?>


<input class="box" name="btnStep1" type="submit" id="btnStep1" value="Proceed &gt;&gt;" />
</form>

THE PROBLEM:
On the next step, I require the fields to be posted from the previous step. As you can see from the code, I exit the page if there are no errors and in turn, don't post the fields.

QUESTION:
How can I post the fields without reloading the whole page again? (there is going to be several fields, some of which will require trips to the database, so I would prefer not to load the whole page again).

Thanks
0
Comment
Question by:jset_expert
3 Comments
 
LVL 82

Expert Comment

by:hielo
Comment Utility
You can save the fields and their values in hidden fields. That way if the user clicks on your form's "Edit" button (assuming you have one) or "Continue" and both are of type submit, you will still get the data:
<input type="hidden" name="firstName" value="John"/>
<input type="submit" name="step" value="Edit"/>
<input type="submit" name="step" value="Continue"/>

The other alternative is to save the data on Session variables.
0
 
LVL 21

Accepted Solution

by:
nizsmo earned 500 total points
Comment Utility
I suggest using session variables, it is quite hassle free and easy to pass variables.

To use session variables, put:
session_start(); at the start of every page you want to assign or access the variables.

Then to set the variables simply:

$_SESSION["your_variable_name"] = $your_variable;

and in another page you can do as you wish by retrieving the variable

session_start();
echo $_SESSION["your_variable_name"];

Hope this helps with your query.
0
 
LVL 1

Author Closing Comment

by:jset_expert
Comment Utility
of course....can't believe i didn't think of this.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

771 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

11 Experts available now in Live!

Get 1:1 Help Now