/    Sign up×
Community /Pin to ProfileBookmark

data from a form

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>[/CODE]

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]

[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;
});

});[/CODE]

Thanks for your help.
M.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@eastsideskiMay 31.2010 — I would suggest including request.php within confirmation.php, so that post variables can still be accessed.
[code=php]include("request.php");[/code]
Copy linkTweet thisAlerts:
@malthus23authorJun 01.2010 — Thanks, I've tried but it messes up the validation and I get an error message...
Copy linkTweet thisAlerts:
@criterion9Jun 01.2010 — You can add the results to your session. Alternatively (and probably a better solution) is to alter your logical flow in your intermediary page so that the confirmation is included if everything validates rather than redirecting the user again to a new page (for the second time since submission).
Copy linkTweet thisAlerts:
@malthus23authorJun 03.2010 — thanks mate, it took me a while to understand how to use the sessions with my file configurations but I did it!

Thanks again.
×

Success!

Help @malthus23 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...