0

I need some help to understand why I cannot get the form to return to the index.html page after its submitted. The form works fine and the email comes through but once submitted the website goes to an error page (file not found, 404 error).

<!-- begin snippet: js hide: false console: true babel: false -->
<?php
    $name = $_POST ['name'];
    $vistor_email = $_POST['email'];

    $email_from = 'enquires@emergencyplumbers247.com';

    $email_subject = "newsletter submission";

    $email_body = "User Name: $name.\n".
                    "User Email: $vistor_email.\n";

    $to = "newsletter@emergencyplumbers247.com";

    $headers = "From: $email_from \r\n";

    $headers .= "Reply-To:$vistor_email \r\n";

    mail($to,$email_subject,$email_body,$headers);

    header("Location: index.html")
?>

this is the PHP I've used. I've changed the header location to index.html but not having any joy in getting it to return to the index.html page.

<!-- Newsletter -->

<section>
  <div class="container text-center">
    <div class="newsletter mt-3 mb-3 p-4">
      <form id="contact-form" method="post" action="./php/contact-form-handler.php">
        <h5>Sign up to our newsletter</h5>
        <p>Recieve the lastest news and offers by signing up today.</p>
        <div class="form-group text-center">
          <label for="input-name" class="sr-only">Your Name:</label>
          <input type="text" name="name" class="form-control text-center" placeholder="full name" id="input-name" required>
        </div>
        <div class="form-group text-center">
          <label for="input-email" class="sr-only">Your Email:</label>
          <input type="email" name="email" class="form-control text-center" placeholder="mail@example.com" id="input-email" required>
        </div>
        <div class="form-check">
          <label class="form-check-label">
              <input type="checkbox" class="form-check-input" id="input-terms" value="terms">
              I have read and accept the <a href="#" data-toggle="modal" data-target="#modal"> terms and conditions.</a>
            </label>
        </div>
        <div>
          <small class="form-text">You can unsubscribe from the mailing list at anytime</small>
          <button type="submit" class="m-1 btn btn-light">SIGN UP</button>
        </div>
      </form>
      <div class="modal fade" id="modal" tabindex="-1" role="dialog" aria-labelledby="modalTitle" aria-hidden="true">
        <div class="modal-dialog modal-lg" role="document">
          <div class="modal-content">
            <div class="modal-header">
              <h5 class="modal-title" id="modalTitle">Terms and Conditions</h5>
              <button type="button" class="close" data-dismiss="modal" aria-label="close">
                    <span aria-hidden="true">&times;</span>
                  </button>
            </div>
            <div class="modal-body">
              <p> Emergency Plumbers 24-7 do not sell or forward any personal information, we use highly reliable And stable service which is protected by the latest technology. We are unable like every other company to protect your information 100%, but
                we take this very seriously and have every form of protection in place to ensure all areas are covered and protected to our best ability.
              </p>
              <p> The only information we hold is your email and name, this is forward on only back to you or our system administrators for the marketing of our own products, offers and services. Under no circumstances would we ever give or use your details
                for third-party companies.
              </p>
              <p> Should you wish for your details to be removed from our system simple send an email to unsubscribe@emergencyplumbers247.com, This process may take up to 48 hours. We promise not to spam and to only share information and offers that will
                benefit our customers.
              </p>
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-secondary" data-dismiss="modal">close</button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</section>


<!-- NEWSLETTER END-->
ADyson
  • 57,178
  • 14
  • 51
  • 63
Bazla
  • 127
  • 2
  • 14
  • 1
    and what exactly happens instead? You missed that part out. I'd _guess_ that maybe the script crashes before it gets to that line. Please provide some relevant info. If you have an error message, please state exactly what it says - we cannot guess. – ADyson Nov 15 '18 at 13:42
  • Says file not found, 404 error, I've also noticed the URL http://www.emergencyplumbers247.com/php/index.html is looking for index.html inside the PHP folder. would I just add ./index.html – Bazla Nov 15 '18 at 13:47
  • Use the full URL: *header("Location: .https://www.< your domain >.com/index.html")* – Gerard Nov 15 '18 at 13:49
  • 1
    @Gerard not very sustainable when the project is deployed to different environments...although of course PHP can be used to fill in the correct details - perhaps an example of that would be better. – ADyson Nov 15 '18 at 13:51
  • Does the mail get sent? Is the 404 specifically relating to the `index.html` page? Have you actually been redirected? – Harvey Fletcher Nov 15 '18 at 13:59
  • 1
    @HarveyFletcher the question says "the email comes through". And how do you expect a 404 to occur if there _wasn't_ a redirect? If the redirect header wasn't sent, or for some reason the browser didn't decide to follow it, they'd just be left on a blank screen (since the PHP script doesn't output any content), but it'd still be a a valid URL (since it would still be the URL the form was submitted to, the browser hasn't changed it). Your first question is already answered, and your last two don't make any technical sense. Do you understand how redirection works in HTTP? – ADyson Nov 15 '18 at 14:24
  • @ADyson I'm a complete novice really doing this for myself. learning as I go. The thing I've notice is if I create a page in the PHP folder called index.html this page works. So I'm guessing I need to change the location to something that takes it back out of the PHP folder and looks for file in main site folder. I know you can use ../ to take folder back in HTML but I don't know if this works the same in PHP – Bazla Nov 15 '18 at 18:22
  • I don't think it would - it's actually the _browser_ which makes the request to the new URL - PHP just spits out a header telling the browser where it should go next. The browser doesn't know the folder structure, it just knows about URLs. A browser _might_ be able to interpret that as a command to go back one level in the URL from the last one, but AFAIK it's not part of the standard. Try `header("Location: /index.html")` (i.e. just add a forward slash) - see if that makes it go to the path relative to the domain root. – ADyson Nov 16 '18 at 09:50
  • If not, then try Gerard's suggestion by putting the whole URL in - but if you want to make this behave properly when you deploy the site to different environments (e.g. for testing) then try getting PHP to prepend the correct domain and protocol to the string - see https://stackoverflow.com/questions/17201170/php-how-to-get-the-base-domain-url for suggestions – ADyson Nov 16 '18 at 09:52
  • @Gerard in your suggestion to use ("Location: .www.< your domain> .com/index.html") I noticed a . before the www, should I include this or should I add ( http://www.< my domain > .com ) – Bazla Nov 16 '18 at 10:32
  • @Bazla instead of waiting for a reply, it'll probably take you less time just to try it out and see what happens. It's only a dot.... :-) – ADyson Nov 16 '18 at 12:31
  • @ADyson I will bare that is mind thank you. – Bazla Nov 17 '18 at 10:30

0 Answers0