Hi,
I have a rather complicated problem for me as it involves both php and js. And I’m getting lost… Let me explain:
On a page, order.html, I have a form including the following fields:
– name
– email
– comment
When posted, it is linked to request.php and request.validation.js, to end up on confirmation.php.
On confirmation.php, I would like to reuse the information entered in the fields on order.php (name, email, comment) to display them on the page.
My issue I can’t find how to ‘take them along’ in request.php / request.validation.js to confirmation.php where I’m trying to display them with :
[CODE]Contact: <span id=”name”><?php echo($_POST[‘name’]); ?></span>
etc.
For reference, here are my files request.php and request.validation.js:
[code=php]<?php
// CONFIGURATION ————————————————————–
// This is the email where the contact mails will be sent to.
$config[‘recipient’] = ‘[email protected]’;
// This is the subject line for contact emails.
// The variable %name% will be replaced with the reference ordered.
$config[‘subject’] = ‘Website enquiry from %name%’;
// These are the messages displayed in case of form errors.
$config[‘errors’] = array
(
‘no_name’ => ‘Veuillez entrer votre nom.’,
‘no_email’ => ‘Votre adresse email est requise.’,
‘invalid_email’ => ‘Votre adresse email n est pas valide.’,
);
// END OF CONFIGURATION ——————————————————-
// Ignore non-POST requests
if ( ! $_POST)
exit(‘Nothing to see here. Please go back to the site.’);
// Was this an AJAX request or not?
$ajax = (isset($_SERVER[‘HTTP_X_REQUESTED_WITH’]) AND strtolower($_SERVER[‘HTTP_X_REQUESTED_WITH’]) == ‘xmlhttprequest’);
// Set the correct HTTP headers
header(‘Content-Type: text/’.($ajax ? ‘plain’ : ‘html’).’; charset=utf-8′);
// Extract and trim contactform values
$name = isset($_POST[‘name’]) ? trim($_POST[‘name’]) : ”;
$email = isset($_POST[’email’]) ? trim($_POST[’email’]) : ”;
$comment = isset($_POST[‘comment’]) ? trim($_POST[‘comment’]) : ”;
// Take care of magic quotes if needed (you really should have them disabled)
set_magic_quotes_runtime(0);
if (get_magic_quotes_gpc())
{
$name = stripslashes($name);
$email = stripslashes($email);
$comment = stripslashes($comment);
}
// Initialize the errors array which will also be sent back as a JSON object
$errors = NULL;
// Validate name
if ($name == ” || strpos($name, “r”) || strpos($name, “n”))
{
$errors[‘name’] = $config[‘errors’][‘no_name’];
}
// Validate email
if ($email == ”)
{
$errors[’email’] = $config[‘errors’][‘no_email’];
}
elseif ( ! preg_match(‘/^[-_a-z0-9’+*$^&%=~!?{}]++(?:.[-_a-z0-9’+*$^&%=~!?{}]+)*+@(?:(?![-.])[-a-z0-9.]+(?<![-.]).[a-z]{2,6}|d{1,3}(?:.d{1,3}){3})(?::d++)?$/iD’, $email))
{
$errors[’email’] = $config[‘errors’][‘invalid_email’];
}
// Validation succeeded
if (empty($errors))
{
// Prepare subject line
$subject = str_replace(‘%name%’, $name, $config[‘subject’]);
// Set date
$todayis = date(“l, F j, Y”) ;
// Prepare message
$message = “Date: $todayis
Nom: $name
email: $email
Commentaire: $comment”;
// Additional mail headers
$headers = ‘Content-Type: text/plain; charset=utf-8’.”rn”;
$headers .= ‘From: ‘.$email;
// Send the mail
if ( ! mail($config[‘recipient’], $subject, $message, $headers))
{
$errors[‘server’] = ‘Server problem’;
}
}
if ($ajax)
{
// Output the possible errors as a JSON object
echo json_encode($errors);
}
else
{
// Show a simple HTML feedback message in case of non-javascript support
if (empty($errors))
{
header(‘Location: ../steps/confirmation.php’);
}
else
{
echo ‘<h2>Oups!</h2>’;
echo ‘<ul><li>’;
echo implode(‘</li><li>’, $errors);
echo ‘</li></ul>’;
echo ‘<br><br><a href=”javascript:history.back(-1);”>Retour</a>’;
}
}
[CODE]/*
* Request Form Validation v1.0
* By Simon Bouchard <www.simonbouchard.com>
* You need at least PHP v5.2.x with JSON support for the live validation to work.
*/
jQuery(document).ready(function(){
jQuery(‘#requestform’).submit(function() {
// Disable the submit button
jQuery(‘#requestform input[type=submit]’)
.attr(‘value’, ‘Send…’)
.attr(‘disabled’, ‘disabled’);
// AJAX POST request
jQuery.post(
jQuery(this).attr(‘action’),
{
name:jQuery(‘#name’).val(),
email:jQuery(‘#email’).val(),
comment:jQuery(‘#comment’).val(),
},
function(errors) {
// No errors
if (errors == null) {
document.location.href=”confirmation.php”;
}
// Errors
else {
// Re-enable the submit button
jQuery(‘#requestform input[type=submit]’)
.removeAttr(‘disabled’)
.attr(‘value’, ‘Envoyer’);
// Technical server problem, the email could not be sent
if (errors.server != null) {
alert(errors.server);
return false;
}
// Empty the errorbox and reset the error alerts
jQuery(‘#requestform .errorbox’).html(‘<ul></ul>’).show();
jQuery(‘#requestform li’).removeClass(‘alert’);
// Loop over the errors, mark the corresponding input fields,
// and add the error messages to the errorbox.
for (field in errors) {
if (errors[field] != null) {
jQuery(‘#’ + field).parent(‘li’).addClass(‘alert’);
jQuery(‘#requestform .errorbox ul’).append(‘<li>’ + errors[field] + ‘</li>’);
}
}
}
},
‘json’
);
// Prevent non-AJAX form submission
return false;
});
});
Thanks for your help.
M.