javascript - JS message in PHP return to HTML page -


html code

<div id="fourmstyle" class="fourm">  <form action="scripts/mail.php" method="post">      <label for="name">your name <required>*</required>         </label>     <input type="text" name="name" id="name" placeholder="joe bloggs">      <label for="email">your email <required>*</required>         </label>     <input type="text" name="email" id="email" placeholder="joebloggs@example.com">      <label for="telephone">telephone         </label>     <input type="text" name="telephone" id="telephone">      <label for="type">type         </label>         <select name="type">                             <option value="booking" selected>booking</option>             <option value="b&b">b&amp;b</option>             <option value="question">question</option>             <option value="general">general</option>             <option value="website feedback">website feedback</option>         </select></p>           <label for="messsage">message <required>*</required>             </label>          <textarea name="message" id="message" rows="5" cols="25">          </textarea></p>           <label for="btn">&nbsp;</label>          <button type="submit" class="button">submit          </button>          <br>&nbsp;<requireddescription> *(indicates information required)          </requireddescription> </form> 

php code

<?php if(isset($_post))  {     $name = (isset($_post['name'])) ? strip_tags($_post['name']) : null; //if name set, strip html tags, , return it, otherwise set string null.     $email = (isset($_post['email'])) ? strip_tags($_post['email']) : null; //same above.     $telephone = (isset($_post['telephone'])) ? preg_replace('~[^0-9\-]~','',$_post['telephone']) : null; //if telephone set, remove characters not number, or dash, othewise set null.     $type = (isset($_post['type'])) ? strip_tags($_post['type']) : null; //strip tags.     $message = (isset($_post['message'])) ? strip_tags($_post['message']) : null; //strip tags.     if(empty($name) || empty($email) || empty($message))      {          //name, email, , message required fields, if empty, tell user go , fill them in.         echo '<script type="text/javascript"> alert ("please go , fill in required lines"); </script>';     }     else      {          //if fields not empty, proceed mailing.         $formcontent=" from: $name \n type: $type \n\n message: $message \n\n telephone: $telephone";         $recipient = "joebloggs@example.com";         $subject = "website contact form: $type";         $mailheader = "from: $email \r\n";            if(mail($recipient, $subject, $formcontent, $mailheader))          {              //if mail sent smtp server successfully, echo 'thank you'.             echo '<script type="text/javascript"> alert ("thankyou '.$name.' have submitted message , possible, if need speak in mean time please call 01983 872244 "); </script>';         }         else          {              //otherwise, tell user did not go through.             echo '<script type="text/javascript"> alert ("i sorry there has been error submitting request please try again or call on 01983 872244"); </script>';         }     } } ?> 

ok code above works quite nicely , have js pop alerts. however, when ok js alert takes me mail.php script , not html page in originated how rectify ?

this should solve immediate issue... in future, try out ajax call.

if(mail($recipient, $subject, $formcontent, $mailheader))          {              //if mail sent smtp server successfully, echo 'thank you' , return previous page.             echo '<script type="text/javascript"> alert ("thankyou '.$name.' have submitted message , possible, if need speak in mean time please call 01983 872244 "); window.history.back(); </script>';         }         else          {              //otherwise, tell user did not go through.             echo '<script type="text/javascript"> alert ("i sorry there has been error submitting request please try again or call on 01983 872244"); window.history.back(); </script>';         } 

edit

forgot include first instance

if(empty($name) || empty($email) || empty($message))      {          //name, email, , message required fields, if empty, tell user go , fill them in , send them back.         echo '<script type="text/javascript"> alert ("please go , fill in required lines"); window.history.back()</script>';     } 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -