Solved

PHP Validation / Form Post problem

Posted on 2007-11-15
3
406 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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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…

690 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