Returning to the previous page - without an event

Summary: The user has filled in a form and clicked “submit.” But, they made a mistake. An “IF” function has detected the error, and made adjustments.
Now I want to send the user back to the form on the previous page to correct their mistake - without resetting the entire form and forcing the user to re-enter everything.

My first effort:


if ($fileatt_size2 > 10000) { $bgc[31] = "background-color: #ff5555"; include 'cardorder.php'; die(); }

While this works, it does reset the form, empties all (33) fields, and starts from the top - which would annoy the user.

  1. Is there a PHP equivalent of " history.go(-1) "?
  2. If not, how do I execute this piece of JavaScript in a PHP file, without an ‘event’? The event is the “IF” function.

And in a somewhat related note: Is there another command similar to “include” that simply executes or goes to another file, rather than including it?
Thanks for any help.

Once you hit “Submit”, your creating a request and will lose the data. You have two options:

Use javascript to ensure that the data is correct before allowing them to submit. I do not like this option , ever, because javascript is not always available.

Or, within your if statement that catches the fail, assign the entire array from the $_POST to a session. At the beginning of cardorder.php, check to see if a session exists that contains the historic value, if so assign the values back to your form for the user, and clear the session. Certain web frameworks such as Zend Framework handles this sort of thing for your.

You can either ask the user to click the Back button to review the form, or repeat the form on the error page. Either way, you can create “sticky” fields, where the content typed by the user isn’t erased. A simple example:

<input name="name" type="text" id="name" value="[COLOR="#FF0000"]<?php if (isset($_POST["name"])) {echo $name;} ?>[/COLOR]">

This will only work if the form is on example.php, and the action being called is example.php. I believe he is forwarding off to a new script though, creating a new request and thus will lose the $_POST data by the time it gets back to the form.

The first ‘page’ is the form, called cardorder.php.
On Submit the page emailer.php is called. It examines the inputs for errors. If there are no mistakes it generates an email that contains all the form data and attachments the user has specified. Then it ‘includes’ the post-order.php file, which congratulates the user, etc, etc.

Note: if I click the back button from the post-order file, the form returns and all the fields are still filled in. I can actually re-submit the same data over and over, so the data is not lost by submitting.

If the emailer.php file detects errors it sets a new background color for the offending field, and should go back to the previous page (the form) to await corrections.

So, what is the proper syntax for using “history.go(-1)” in the middle of a PHP file?
Trial & error only take me so far. :slight_smile:

The code I posted above (which I changed after K. Wolfe quoted me, because I made some mistakes) would do what you want if you put the emailer.php code on the cardorder.php page. So instead of action=“emailer.php”, you’d use action=“<?php $_SERVER[‘PHP_SELF’] ?>”.

Ralph; I appreciate the advice - even if I don’t fully understand the answer. I have much to learn about PHP_SELF and $_POST functions.
In this case I am going to have an error screen come up, explain the mistakes to the user, and provide a ‘back’ button to have them return and make corrections. It’s clumsy, and requires writing a whole new module, but if PHP has no ‘back’ command it’s the best I can do.
I am still accustomed to ‘real’ computer programming, where an IF operator can call another program without user input, and variables can be pushed into the stack, and keep their values.

What we are trying to get to is that a back operator is a bad idea. PHP SELF is the same as cardorder.php, it is just using its own file name as the action. At the beginning of your php file, you do an if statement to catch if $_POST has set fields. If so, run your logic that you would have attempted on post-order.php. On success, you should use header() to forward them off to a “successful” page, else it populates the form using the $_POST array, and displays what went wrong.

Your entire process is now in one php file, rather than two, and displaying your users data is now very easy to do.

I will post an example later today if need be.

Untested:


// cardorder.php

// check if form was sbumitted
if($_SERVER['REQUEST_METHOD'] == "POST")   {
//run your validations that was before  done in emailer.php here, set $pass to true if good to go

if ($pass) {
//send your email to notify of success, forward to success page
header('http://www.you.com/order-success.php');
exit();
}
} else {
//the form was not submitted, or failed validation. display your forms with data from POST
// fields will be populated as such: 
echo '<input type="text" name="fname"      value="' . $_POST["fname"] . '">';
}


That is an interesting approach. I will save and study the code to see how I can apply it to the situation.

The project is a credit card ‘application’ form, and I am trying to minimize delays caused by incorrect user entries.
Some input fields are text, while others are file names to be uploaded. The first task is to look at the size of the selected file, and if it’s too big it should send the user back to the form while highlighting the offending field.
There are eight critical text fields that must have various minimum lengths, and three sets of file names and sizes to check.
Since there are at least three different card forms, I was hoping to use one standardized ‘emailer’ to send the filtered data on to the approvals office.

I may post all the code here tomorrow. Right now I must complete 3 sets of prototype circuit boards for digital temp controllers.

How far into development are you and what kind of deadline are you looking at? Things like this are handled with much ease with certain frameworks such as Zend Framework. They have many built in checks that can be applied to any field such as valid email checkers, etc. Picking up on a framework such as Zend, however, will take some time, especially giving Zends size.

I’m not sure if there is a misunderstanding. This is not about ordering BY credit card - this is ordering THE credit card. The information needed is not extensive, but certain information must be present.
The development site is here: http://www.angelicaccess.ca/testsite07/

Clicking on “Order Your Card” begins the process. The system isn’t active yet, so you can experiment with it. The form and mailer are operational, but without any error-checking.
I will try to get back to this project later today.

Right. i was referring to the handling of those requirements and displaying the error message next to the field in question. Have a look here: http://collstudio.wordpress.com/2011/01/10/basic-zend_form-by-example-with-netbeans/. Netbeans isn’t required to develop with Zend, but this is the best example of code I could find.

$email = new Zend_Form_Element_Text('email');
$email->setLabel('Email address')
->addFilter('StringToLower')
->setRequired(true)
->addValidator('NotEmpty', true)
->addValidator('EmailAddress');

That code there is about all you need, Zend takes care of validating it, and on error, takes them back tot he previous page, restores what they already had entered, and displays an error message next to the field in question. Zend can be very complex for a beginner, so it may not be something to use on this project, but perhaps the next it may be worth looking in to.