You should take a look at FormIt. It's the most popular modx addon regarding forms and form processing.
Example contact form
Load CSS
if (Form submitted) {
check for errors
if (error) {
echo error
display form
} else {
Process form
echo success message or forward user to another page
}
} else {
display form
}
Load CSS
if (not form submitted) {
display form
} else {
check for errors
if (error) {
echo error
display form
} else {
Process form
echo success message or forward user to another page
}
}<?php /* Set e-mail recipient */ $myemail = "[email protected]"; $subject = "Website Inquiry"." ".$_REQUEST['name']; /* Check all form inputs using filter_input function */ $name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING); // Visitor Name $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL); // Visitor Email $comments = filter_input(INPUT_POST, 'comments', FILTER_SANITIZE_STRING); // Visitor Message /* If e-mail is not valid show error message */ if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) { show_error("E-mail address not valid"); } /* Let's prepare the message for the e-mail */ $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; $headers .= "From: [email protected]" . "\r\n"; $headers .= "Reply-To: ".$email."\r\n"; $headers .= "Return-Path: ".$email."\r\n"; $message = " <html> <body> <p>Hello!</p> <p>Your website form has been submitted by:</p> <p><strong>Name:</strong> $name</p> <p><strong>E-mail:</strong> $email</p> <p><strong>Comments:</strong></p> <p>$comments</p> End of message </body> </html> "; /* Send the message using mail() function */ mail($myemail, $subject, $message, $headers); /* Redirect visitor to the thank you page */ header('Location:http://www.mydomain.com/_mailman/thankyou.html'); exit(); /* Functions we used */ function check_input($data, $problem='') { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); if ($problem && strlen($data) == 0) { show_error($problem); } return $data; } function show_error($myError) { ?> <html> <body> <b>Please correct the following error:</b><br /> <?php echo $myError; ?> </body> </html> <?php exit(); } ?>
$output = ''; /* put this at the top */ $output .= $msg;
echo $output;
return $output;


You shouldn't need any PHP to make the JS work. You'd just put the JS in your form (between script tags) and add an onClick function to your submit button.
To ease the transition to MODX (and generally make things easier to maintain), I'd recommend adding all the output to a variable called $output, like this:
$output = ''; /* put this at the top */ $output .= $msg;
Anything that gets shown on the screen should be a string that gets added to that variable.
Then at the end, do this:
echo $output;
That way, the code would work as a MODX snippet just by changing that last line to:
return $output;
There are plenty of good PHP primers on the web. There's a nice one (at least I think so) in the Appendix of my book: http://amzn.to/Qfsl1Z.
The key thing to know if you'll be using MODX, is that MODX doesn't allow you to mix PHP and HTML as you've done above.
PHP goes in snippets, and any HTML to be output is usually either in a chunk, or in strings in the PHP code that will be returned in the $output variable.
Most often, the way this works is you put all the HTML in a chunk which contains placeholder tags for anything you might want to change in the HTML. The PHP code in the snippet gets the chunk, replaces the placeholders, and returns the finished code in the $output variable.
BTW, your two functions *would* do something, but as far as I can see, they are never called in your PHP code, so the code in them will never execute. They would also need to be at the top of the code rather than the bottom.
TBH, teaching someone how to use PHP outside of MODX is pretty far outside the scope of this forum.
Without knowing what the 3 page site entails that might be special, I have to ask why not serve it with MODX? You can put any html and css into modx without leveraging anything special within MODX. Just put your html in a template, or even three templates, assign 3 resources/pages, and serve it right up.
Then you can use formIt and forget all this finagling with your form. FormIt will handle your errors and validation. I'd chunk out the form html though, rather than placing it in the template for that page. Easy enough.