php - Header and Echo dilemma -


i have contact form on site i'm developing , have written php return errors page if fields aren't filled in correctly, works fine, email sends if filled out correctly , if isn't right error messages sent out , displayed.

the problem if form filled out incorrectly, user redirected page error message form lower down page user redirected. i'd if there error user redirected same page error message echoed out, lower down on page form is.

i did research , saw use id anchors , header, if page load tell to, error message doesn't show. i've attempted use if loop _get variable check if matches in error message, can't seem working properly.

so summarise:
i'd if form filled out incorrectly, user returned same page, lower down form , error message variable set in code displayed above form.

i've started learning php appreciate resolving this. in advance , suggestions.

here's screenshots of site:
http://imgur.com/a/hv5ow

here's page's code:

<?php if ($_server["request_method"] == "post") {     $name    = trim($_post["name"]);     $email   = trim($_post["email"]);     $message = trim($_post["message"]);      if ($name == "" or $email == "" or $message == "") {         $error_message = "you must enter name, email address , message";     }      if (!isset($error_message)) {         foreach ($_post $value) {             if (stripos($value, 'content-type:') !== false) {                 $error_message = "there problem information submitted";             }         }     }      if (!isset($error_message) && $_post["address"] != "") {         $error_message = "your contact form submission has error";     }      require_once("inc/phpmailer/class.phpmailer.php");     $mail = new phpmailer();      if (!isset($error_message) && !$mail->validateaddress($email)) {         $error_message = "you must specify valid email address";     }      if (!isset($error_message)) {         $email_body = "";         $email_body = $email_body . "name: " . $name . "<br>";         $email_body = $email_body . "email: " . $email . "<br>";         $email_body = $email_body . "message: " . $message;          $mail->setfrom($email, $name);         $address = "name@email.co.uk";         $mail->addaddress($address, "corwen forestry timber products ltd.");         $mail->subject = "website contact form submission | " . $name;         $mail->msghtml($email_body);          if ($mail->send()) {             header("location: contact.html?status=thanks");             exit;         }         else {             $error_message = "there problem sending email: " . $mail->errorinfo;         }     } } ; ?>  <?php $title = "corwen forestry online | contact us"; $active = "contact"; include("inc/header.php"); ?>     <img class="banner" src="images/contact-us.jpg" alt="contact background image.">      <article class="band content-wrap clearfix">          <?php if (isset($_get["status"]) && $_get["status"] == "thanks") { ?>             <section>                 <p class="contact-thanks">thanks getting in touch, we'll can.</p>             </section>         <?php }         else { ?>              <header>                 <h2>contact us</h2>             </header>              <section class="contact-intro">                 <p>                     lorizzle hizzle dolor fizzle amizzle, consectetuer adipiscing elizzle. crackalackin sapien velizzle,                     shizz volutpizzle, suscipit quis, fo rizzle vel, arcu. pellentesque fizzle tortizzle. izzle                     mammasay mammasa mamma oo sa. fusce izzle dolor dapibus turpis tempizzle tellivizzle. crunk                     pellentesque nibh izzle turpizzle. fo shizzle nizzle funky fresh crackalackin. pellentesque                     eleifend rhoncizzle nisi. in sure habitasse platea dictumst. donec dapibizzle. curabitur gangsta                     urna, pretizzle eu, pizzle ac, shizz vitae, nunc. da bomb suscipizzle. integer break down                     pimpin' shiznit fo shizzle nizzle.                 </p>             </section>              <section id="error-anchor" class="contact-third first">                 <img src="images/contact-icon-email.png" alt="email icon">                  <h3><a href="mailto:sales@corwenforestry.co.uk">sales@corwenforestry.co.uk</a></h3>             </section>              <section class="contact-third">                 <img src="images/contact-icon-address.png" alt="address icon">                  <h3>                     ty&rsquo;n llidiart industrial estate<br>                     corwen<br>                     denbighshire<br>                     ll21 9rz                 </h3>             </section>              <section class="contact-third">                 <img src="images/contact-icon-phone.png" alt="phone icon">                  <h3>01490 412 146</h3>             </section>              <?php             if (isset($error_message)) {                 header("location: contact.html#error-anchor");                 echo '<p class="error-message">' . $error_message . '</p>';             }         ;             ?>              <form class="clearfix" method="post" action="contact.html">                 <table class="contact-us">                     <tr>                         <th>                             <label for="name">name</label>                         </th>                         <td>                             <input type="text" name="name" id="name">                         </td>                     </tr>                     <tr>                         <th>                             <label for="email">email</label>                         </th>                         <td>                             <input type="text" name="email" id="email">                         </td>                     </tr>                     <tr>                         <th>                             <label for="message">message</label>                         </th>                         <td>                             <textarea name="message" id="message"></textarea>                         </td>                     </tr>                     <tr style="display: none;">                         <th>                             <label for="address">address</label>                         </th>                         <td>                             <p>please leave address field blank, it's anti-spam measure.</p>                             <input type="text" name="address" id="address">                         </td>                     </tr>                 </table>                 <input type="submit" value="send message">             </form>              <section class="map">                 <iframe width="47.7%" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"                         src="https://maps.google.co.uk/maps/ms?msa=0&amp;msid=208085933608816427574.0004e1242973ccf3517ee&amp;ie=utf8&amp;t=m&amp;ll=52.977589,-3.414001&amp;spn=0.144707,0.291824&amp;z=11&amp;output=embed"></iframe>                 <br/>                 <small>view <a target="_blank"                                href="https://maps.google.co.uk/maps/ms?msa=0&amp;msid=208085933608816427574.0004e1242973ccf3517ee&amp;ie=utf8&amp;t=m&amp;ll=52.977589,-3.414001&amp;spn=0.144707,0.291824&amp;z=11&amp;source=embed"                                style="color:#0000ff;text-align:left">cftp hq , mills</a> in larger map                 </small>             </section>              <section class="map-key">                 <ul>                     <li>                         <img src="images/a-sml.png" alt="head office map icon key.">                         head office &amp; shop                     </li>                     <li>                         <img src="images/b-sml.png" alt="gwyddelwern sawmill map icon key.">                         gwyddelwern sawmill                     </li>                     <li>                         <img src="images/c-sml.png" alt="llandrillo sawmill map icon key.">                         llandrillo sawmill                     </li>                 </ul>             </section>          <?php } ?>     </article><!-- end band content-wrap -->  <?php include("inc/footer.php"); ?> 

as question can use following approach:

the idea "anchor" form onsubmit - allows go down form despite if form valiadtion passed or not, right after submit button pushed.

1) set "anchor"-action this:

<form class="clearfix" method="post" action="contact.html#myanchor"> 

2) set "fake" url right above form this:

<a href="#myanchor"></a>  <form class="clearfix" method="post" action="contact.html#myanchor"> 

you can style in way like, or use id="myanchor" in form tag if not break anything.

3) use current code, @ first glance looks fine:

if (!isset($error_message)) {    // send mail } else {    // want error variable. } 

4) echo error right above form

<a href="#myanchor"></a> <?= $sformattederrorvariable ?> <form class="clearfix" method="post" action="contact.html#myanchor"> 

hope helps. cheers!


Comments