Solved

PHP Validation / Form Post problem

Posted on 2007-11-15
3
401 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
ID: 20296389
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
ID: 20296548
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
ID: 31409503
of course....can't believe i didn't think of this.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 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…

803 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