[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

PHP Validation / Form Post problem

Posted on 2007-11-15
3
Medium Priority
?
409 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
This article discusses how to implement server side field validation and display customized error messages to the client.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

650 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