0

So whenever i press submit it opens the browser showing the code.. what can i do? I watched a lot of youtube videos but i can't figure out. I downladed wampp, set the path but it still does not work. It's the first time i used php so i don't really have any type of experience with it. Can you help me please? It's the last step to completing my portfolio.

Homepage.html:

<form action="contactform.php" method="POST">
    <div class="row">
        <div class="col-xl-6 col-lg-6 col-md-12 col-sm-12 col123 col12">
            <label for="">First Name </label>
            <input type="text" name="fname" required>
        </div>
        <div class="col-xl-6 col-lg-6 col-md-12 col-sm-12 col123 col12">
            <label for="">Last Name </label>
            <input type="text" name="sname" required>
        </div>
        <div class="col-xl-6 col-lg-6 col-md-12 col-sm-12 col123 col12">
            <label for="">Email</label>
            <input type="email" name="email" required>
        </div>
        <div class="col-xl-6 col-lg-6 col-md-12 col-sm-12 col123 col12">
            <label for="">Phone Number</label>
            <input type="number" name="number" required>
        </div>
        <div class="col-lg-12 col-xl-12 col-md-12 col-sm-12 col123">
            <label for="">Message</label>
            <textarea name="message" id="" style="resize: vertical;"
                placeholder="Write your message here ... " required></textarea>
        </div>
        <div class="col-lg-12 col-xl-12 col-md-12 col-sm-12 col44">
            <button type="submit" name="send" role="button" id="button">
                Send
            </button>
        </div>
    </div>
</form>

contactform.php:

<?php

if(isset($_POST["send"])){
    $fname = $_POST['fname'];
    $sname = $_POST['sname'];
    $number = $_POST['number'];
    $mailFrom = $_POST['mail'];
    $message = $_POST['message'];

    $mailTo = "bogdybogdan34@gmail.com";
    $headers = "From: ".$mailFrom;
    $txt = "You have received and e-mail from ".$sname."\n".$number."\n\n".$message;

    mail($mailTo , $txt , $headers);
    header("Location: homepage.html?mailsend");
}
?>
El_Vanja
  • 3,660
  • 4
  • 18
  • 21
Bogdyapple
  • 11
  • 1
  • 2
    Does this answer your question? [PHP code is not being executed, instead code shows on the page](https://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-instead-code-shows-on-the-page) – El_Vanja Mar 28 '20 at 17:27
  • That means PHP is not running. – user3601546 Mar 28 '20 at 17:29
  • So how do i make it run? – Bogdyapple Mar 28 '20 at 17:32
  • @El_Vanja Probably.. but i don't quite understand.. i just wanted to copy-paste php because it's a must to complete a portfolio.. I know it's not that okay but i just want to finish it so i can start other projects without learning the entire php – Bogdyapple Mar 28 '20 at 17:37
  • Read point 4 in that linked question's answer. – El_Vanja Mar 28 '20 at 17:49
  • @El_Vanja On the video i watched, everyone had homepage.php instead of html. But then how do i open it in browser if i rename it .php? – Bogdyapple Mar 28 '20 at 18:05
  • Sorry, I hadn't realized that you were posting code for two separate files since they were all in one code block. Have you [placed your files correctly](https://stackoverflow.com/a/5308625/4205384)? Side note: any html file can have the php extension and still normally work as html. – El_Vanja Mar 28 '20 at 18:31

1 Answers1

0

First, if you are using wampp like you said, you need a mail server set up that is of course assuming that you are running this on localhost, if it's on an online server, make sure the mail function is enabled within your cpanel.

Now back to your code, it seems to return a

HTTP ERROR 405

A 405 Method Not Allowed Error is an HTTP response status code that indicates a web browser has requested access to one of your web pages and your web server received and recognized its HTTP method. But, unfortunately, your server has decided to reject that specific HTTP method, so the web browser can’t access the requested web page. To solve this you need to see change the SMTP settings in the php.ini file here is a tutorial send mail on localhost then try sending the email again. if it doesn't work consider other web servers like xampp or try sending emails using libraries such as PHP MAILER, it's really friendly for beginners