May 16, 201214 yr Hi everyone, I have designed contact form with following values: First Name (Mandatory) E-mail (Mandatory) Phone (Optional) Message (Mandatory) I have validated this contact form using SpyValidation in Adobe Dreamweaver. Now, I would like to create server side validation, so it is validated if all mandatory fields are filled in, and check if any value is typed into optional text field before sending it off, and if successful next webpage called thankyou.html should appear where Thank you message appears. I have created both xhtml docs, but I'm struggling with PHP. Can anyone please help me? I have visited some links on forums, but unfortunately I'm not so good at it.
May 16, 201214 yr if( isset($_POST) ){ //form validation vars $formok = true; $errors = array(); //form data $name = $_POST['name']; $email = $_POST['email']; $phone= $_POST['phone']; $message = $_POST['message']; //validate form data //validate name is not empty if(empty($name)){ $formok = false; $errors[] = "You didn't put in a name"; } if(empty($phone)){ $formok = false; $errors[] = "U didn't fill in a number"; } //validate email address is not empty if(empty($email)){ $formok = false; $errors[] = "U heeft geen emailadres ingevult"; //validate email address is valid }elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){ $formok = false; $errors[] = "You didn't put in an emailaddress"; } //validate message is not empty if(empty($message)){ $formok = false; $errors[] = "You didn't put in a message"; } //validate message is greater than 15 charcters elseif(strlen($message) < 15){ $formok = false; $errors[] = "Your message must be longer then 15 characters"; } This is just the code for validation, not for sending the email! If you need help with that just ask Edited May 16, 201214 yr by RowruffDesign
May 16, 201214 yr Author Thanks, is that PHP? I have tried to paste that into PHP document into Adobe Dreamweaver but it is not detecting its PHP. Do you also know how to create PHP for sending ?
May 16, 201214 yr Thanks, is that PHP? I have tried to paste that into PHP document into Adobe Dreamweaver but it is not detecting its PHP. Do you also know how to create PHP for sending ? You need to add the PHP opening and closing tags
May 16, 201214 yr Author <?php if( isset($_POST) ){ //form validation vars $formok = true; $errors = array(); //form data $name = $_POST['name']; $email = $_POST['email']; $phone= $_POST['phone']; $message = $_POST['message']; //validate form data //validate name is not empty if(empty($name)){ $formok = false; $errors[] = "You didn't put in a name"; } if(empty($phone)){ $formok = false; $errors[] = "U didn't fill in a number"; } //validate email address is not empty if(empty($email)){ $formok = false; $errors[] = "U heeft geen emailadres ingevult"; //validate email address is valid }elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){ $formok = false; $errors[] = "You didn't put in an emailaddress"; } //validate message is not empty if(empty($message)){ $formok = false; $errors[] = "You didn't put in a message"; } //validate message is greater than 15 charcters elseif(strlen($message) < 15){ $formok = false; $errors[] = "Your message must be longer then 15 characters"; } ?> Dreamweaver tells me syntax error on line 48, which is ?> What is wrong? I just opened and closed php tags.
May 16, 201214 yr Dreamweaver tells me syntax error on line 48, which is ?> What is wrong? I just opened and closed php tags. There is a curly brace missing from the end of the code, should be: } } ?> Edited May 16, 201214 yr by GalaxyTramp
May 16, 201214 yr Author I see, thanks. Do you know how to make this code sending e-mail, and once form is sent, show next "thank you" web page? I want to achieve this: Have my form validated on client side by Java, then once clicked on submit, validated by PHP on server side, and then if sucessful sent form and show client next web-page saying thank you etc.
May 16, 201214 yr Remove the ?> from the end of your PHP file, and put this in (and change the settings at the top): $to = "Your Name <you@youremail.com>"; $subject = "Contact form submission"; $success_page = "/contact_success.php"; if (empty($errors)){ $body = "Contact form submission:\n\n"; foreach ($_POST as $field => $val){ $body .= ucfirst($field) . ": $val\n"; } $body .= "\nSent at " . date("H:i, jS F"); if (mail($to, $subject, $body)){ header("Location: $success_page"); exit(); } else { $errors[] = "Email could not be sent!"; } } ?> <h1>Sorry, an error occurred</h1> <ul> <? foreach ($errors as $error) :?> <li><?= $error ?></li> <? endforeach ?> </ul> Edited May 16, 201214 yr by andyl
Create an account or sign in to comment