html - PHP form validation (don't know how to split it in two different pages) -
i working on contact form in php. knowledge of php pretty non-existent.
i've tried while html form submit php form have text fields validated , required if blank couldn't work. don't know ajax, otherwise, have attempted that.
so, have resorted having php self-form inside html page.
this current version:
<?php // define variables , set empty values $firstnameerr = $lastnameerr = $emailerr = $messageerr = ""; $first_name = $last_name = $email = $message = ""; if ($_server["request_method"] == "post") { if (empty($_post["first-name"])) { $firstnameerr = "first name required"; } else { $first_name = test_input($_post["first-name"]); } if (empty($_post["last-name"])) { $lastnameerr = "last name required"; } else { $last_name = test_input($_post["last-name"]); } if (empty($_post["email"])) { $emailerr = "email required"; } else { $email = test_input($_post["email"]); } if (empty($_post["message"])) { $messageerr = "message required"; } else { $message = test_input($_post["message"]); } } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?>
this form on same page:
<form class="ui form" method="post" action="<?php echo htmlspecialchars($_server["php_self"]);?>"> <div class="field"> <label>first name</label> <input name="first-name" id="first-name" placeholder="first name" type="text"> <span class="error">* <?php echo $firstnameerr;?></span> </div> <div class="field"> <label>last name</label> <input name="last-name" id="last-name" placeholder="last name" type="text"> <span class="error">* <?php echo $lastnameerr;?></span> </div> <div class="field"> <label>email</label> <input name="email" id="email" placeholder="email" type="email"> <span class="error">* <?php echo $emailerr;?></span> </div> <div class="field"> <label>message</label> <textarea rows="2" placeholder="please type in message" name="message" id="message"></textarea> <span class="error">* <?php echo $messageerr;?></span> </div> <button class="ui button" type="submit">submit</button> </form>
is there way in 2 different pages, ie, form in first page, , carried on second page in sessions mode? or acceptable commercial site? please let me know. thank you.
addendum
if (empty($_post["email"])) { $emailerr = "email required"; } else { $email = filter_vars(test_input($_post["email"], filter_validate_email)); }
addendum 2
i have included @ bottom of php stuff, ie, after function test_input.header('location: php_mailer_form.php');
correct placement of it?
but reason when attempt visit contact.php form doesn't show up, goes straight error form have @ bottom of php_mailer_form.php.
if(!$mail->send()) { header('location: url/contacterror.html'); } else { header('location: url/contactresult.html'); }
why? (please let me know if need include additional information).
addendum 3
<?php session_start(); $first_name = $_session['first-name']; $last_name = $_session['last-name']; $email = $_session['email']; $message = nl2br($_session['message']); require 'phpmailerautoload.php'; $mail = new phpmailer; //$mail->smtpdebug = 3; // enable verbose debug output $mail->issmtp(); // set mailer use smtp $mail->host = 'host_specified'; // specify main , backup smtp servers $mail->smtpauth = true; // enable smtp authentication $mail->username = 'email_specified'; // smtp username $mail->password = 'password_specified'; // smtp password $mail->smtpsecure = 'tls'; // enable tls encryption, `ssl` accepted $mail->port = 587; $mail->addreplyto( $email, $first_name ); $mail->addaddress( $email, $first_name ); $mail->addaddress( 'email_specified', 'staff' ); $mail->from = 'email_specified'; $mail->fromname = 'staff'; $mail->ishtml(true); // set email format html $mail->subject = 'hotel room request'; $mail->body = $message; $mail->altbody = 'to view message, please use html compatible email viewer!'; if(!$mail->send()) { header('location: url/contacterror.html'); } else { header('location: url/contactresult.html'); }
addendum 4
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>contact</title> <!--[if lt ie 9]> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <style type="text/css"> .ui.fixed.sticky + p { margin-top: 39px; } .error { color: #ff0000; } </style> </head> <body> <?php session_start(); //allows use of session variables if ($_server["request_method"] == "post") { if (!isset($_post["first-name"])) { $firstnameerr = "first name required"; } else { $first_name = test_input($_post["first-name"]); } if (!isset($_post["last-name"])) { $lastnameerr = "last name required"; } else { $last_name = test_input($_post["last-name"]); } if (!isset($_post["email"])) { $emailerr = "email required"; } else { $email = test_input($_post["email"]); } if (!isset($_post["message"])) { $messageerr = "message required"; } else { $message = test_input($_post["message"]); } if(isset($first_name) && isset($last_name) && isset($email) && isset($message)) { $_session['first_name'] = $first_name; $_session['last_name'] = $last_name; $_session['email'] = $email; $_session['message'] = $message; header("location: contact9sessions.php"); } } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> <div class="ui container"> <div class="ui segment"> <div> <div class="ui fluid 5 item tabular menu"> <a class="item" href="index.html">home</a> <a class="item" href="about.html">about</a> <a class="item" href="rooms.html">rooms info & rates</a> <a class="item" href="book.html">to book</a> <a class="item" href="contact.html">contact</a> </div> </div> <div class="ui 2 column stackable grid"> <div class="ten wide column"> <form class="ui form" method="post" action="<?php echo htmlspecialchars($_server["php_self"]);?>"> <div class="field"> <label>first name</label> <input name="first-name" id="first-name" placeholder="first name" type="text"> <?php if(isset($firstnameerr)) print ('<span class="error">* ' . $firstnameerr . '</span>'); ?> </div> <div class="field"> <label>last name</label> <input name="last-name" id="last-name" placeholder="last name" type="text"> <?php if(isset($lastnameerr)) print ('<span class="error">* ' . $lastnameerr . '</span>'); ?> </div> <div class="field"> <label>email</label> <input name="email" id="email" placeholder="email" type="email"> <?php if(isset($emailerr)) print ('<span class="error">* ' . $emailerr . '</span>'); ?> </div> <div class="field"> <label>message</label> <textarea rows="2" placeholder="please type in message" name="message" id="message"></textarea> <?php if(isset($messageerr)) print ('<span class="error">* ' . $messageerr . '</span>'); ?> </div> <button class="ui button" type="submit">submit</button> </form> </div> <div class="six wide column"> <br><br> <img class="ui centered large bordered rounded image" src="images/tobereplaced.jpg"> </div> </div> </div> <div class="ui 2 column grid"> <div class="ui left aligned "> <p>left footer stuff here</p> </div> <div class="ui right aligned"> <p>right footer stuff here</p> </div> </div> </div> </body> </html>
<?php session_start(); //allows use of session variables if ($_server["request_method"] == "post") { if (!isset($_post["first-name"])) { $firstnameerr = "first name required"; } else { $first_name = test_input($_post["first-name"]); } if (!isset($_post["last-name"])) { $lastnameerr = "last name required"; } else { $last_name = test_input($_post["last-name"]); } if (!isset($_post["email"])) { $emailerr = "email required"; } else { $email = test_input($_post["email"]); } if (!isset($_post["message"])) { $messageerr = "message required"; } else { $message = test_input($_post["message"]); } if(isset($first_name) && isset($last_name) && isset($email) && isset($message)) { $_session['first_name'] = $first_name; $_session['last_name'] = $last_name; $_session['email'] = $email; $_session['message'] = $message; header("location: php_mailer_form.php"); } } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> <form class="ui form" method="post" action="<?php echo htmlspecialchars($_server["php_self"]);?>"> <div class="field"> <label>first name</label> <input name="first-name" id="first-name" placeholder="first name" type="text"> <?php if(isset($firstnameerr)) print ('<span class="error">* ' . $firstnameerr . '</span>'); ?> </div> <div class="field"> <label>last name</label> <input name="last-name" id="last-name" placeholder="last name" type="text"> <?php if(isset($lastnameerr)) print ('<span class="error">* ' . $lastnameerr . '</span>'); ?> </div> <div class="field"> <label>email</label> <input name="email" id="email" placeholder="email" type="email"> <?php if(isset($emailerr)) print ('<span class="error">* ' . $emailerr . '</span>'); ?> </div> <div class="field"> <label>message</label> <textarea rows="2" placeholder="please type in message" name="message" id="message"></textarea> <?php if(isset($messageerr)) print ('<span class="error">* ' . $messageerr . '</span>'); ?> </div> <button class="ui button" type="submit">submit</button> </form>
now in other page, access variables, call "session_start();" @ top of page did here, use call "$_session['message']" value of message. answer question? note, edited html error message div prints if error variable set.
Comments
Post a Comment