Skip to content
Advertisement

Why is my PHP Code not Working , HTML website?

Ok so i made a HTML website for a school project and i tried incorporating into it a ” Contact Us ” section in which u give your name , email and a message to be sent to my adress. I can’t seem to find any error into the code . When i am trying to send the info , instead of executing the php , it opens a new white Web Page and no mail is sent.

**That’s how the Page looks like and what happens when i Submit : https://imgur.com/a/1mLaIa4 ** ( as u can see the files are located in the same folder so that’s not a problem )

That’s my code HTML :

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Contact Page</title>
<link rel="stylesheet" href="style2.css" type="text/css" media="screen" />
<link rel="icon" href="icon2.png" type="image/x-icon"/>

</head>
<body>


<div class="container">
  <div style="text-align:center">
    <h2>Contact Us</h2>
    <p>Leave a message or a suggestion:</p>
  </div>
  <div class="row">
    <div class="column">
      <img src="gif3.gif" style="width:100%;">
    </div>
    <div class="column">
      <form id="column-form" method="post" action="column-form-handler.php">
        <label for="fullname">Full Name</label>
        <input type="text" id="fullname" name="fullname" placeholder="Your full name.." required>
        
        <label for="email">Email</label>
        <input type="email" id="email" name="email" placeholder="Your email adress.." required>
       
       <label for="country">Country</label>
        <select id="country" name="country" >
          <option value="australia">Australia</option>
          <option value="canada">Canada</option>
          <option value="usa">USA</option>
        </select>
        
        <label for="subject">Subject</label>
        <textarea id="subject" name="subject" placeholder="Write something.." style="height:170px"></textarea>
        <input type="submit" value="Submit">
      </form>
    </div>
  </div>
</div>

</body>
</html>

And here is the PHP :

<?php
     
    $name = $_POST['fullname'];
    $visitor_email = $_POST['email'];
    $message = $_POST['subject'];
    
    $email_from = 'abcdefgh@gmail.com';
    $email_subject = "Website Messages";
    $email_body = "User Name: $name.n".
                     "User Email: $visitor_email.n".
                       "User Message: $message.n";
                       
    $to = "efghhieig@gmail.com";
    $headers = "From: $email_from rn";
    $headers = "Reply-To: $visitor_email rn";
    mail($to,$email_subject,$email_body,$headers);
    header("Location: contact.html");
    
    
    ?>

Advertisement

Answer

When you load the pages into your browser via file://, the PHP script will not be executed by the PHP interpreter.

You need a webserver running on your machine – e.g. Apache or IIS – which is configured to serve PHP files, and then you need to go to the page in your browser like http://localhost/contact.html so that when you submit the form, it would end up posting to http://localhost/column-form-handler.php (instead of file://column-form-handler.php or whatever).

Doing this means that the webserver will pass requests for .php files to the PHP interpreter, which will execute them, and pass the result of executing the code (as opposed to just the raw source code) back to the webserver which then returns it to the client (i.e. browser).

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement