Solved

PHP Validation / Form Post problem

Posted on 2007-11-15
3
405 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Change Wording in Wordpress Plugin 4 38
Testrail - Active Directory integration. 4 35
SSL unsecure page mystery 17 46
replacate wordpress websites 3 22
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

732 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