-4

I'm new to coding, what I'm trying to do is to make contact form, to send emails to myself

My question is: how do I connect these two codes together? I want when I press "send button" to capture all fields from HTML then use PHP code to send the email to myself

This is my PHP file called function.php

<?php
if (isset($_POST['submit'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $mailTo = "xxxx@xxxx.com";
    $headers = "From: ".$mailFrom;
    $txt = "You have received an e-mail from ".$name.".\n\n".$message;
    mail($mailTo, $subject, $txt, $headers);
    header("Location: index.php?mailsend");
}
?>

And this is my html file called index.html

<div class="row">
    <div class="col-lg-12 m-30px-b sm-m-15px-b">
        <div class="contact-form">
            <h4 class="dark-color font-alt m-20px-b">If you need help! feel free to contact me!</h4>
            <form class="contactform" method="post">
                <div class="row">
                    <div class="col-md-6">
                        <div class="form-group">
                            <input id="name" name="name" type="text" placeholder="Name" class="validate form-control" required="">
                                <span class="input-focus-effect theme-bg"></span>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <input id="email" type="email" placeholder="Email" name="email" class="validate form-control" required="">
                            <span class="input-focus-effect theme-bg"></span>
                        </div>
                    </div>
                    <div class="col-md-12">
                        <div class="form-group">
                            <textarea id="message" placeholder="Your Comment" name="message" class="form-control" required=""></textarea>
                                <span class="input-focus-effect theme-bg"></span>
                        </div>
                    </div>
                    <div class="col-md-12">
                        <div class="send">
                            <button class="btn btn-theme" type="submit" name="send"> send message</button>                   
                        </div>
                        <span class="output_message"></span>
                    </div>
                </div>
            </form>
        </div>
    </div> <!-- col -->
</div>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
N m
  • 3
  • 4
  • Any tutorial on form processing with PHP should explain this. You need `action="function.php"` in the `
    ` tag.
    – Barmar Oct 08 '19 at 19:20
  • @FunkFortyNiner What do either of those dupes have to do with the missing `action` attribute of the form? – Barmar Oct 08 '19 at 19:21
  • I didn't understand, Can you explain more for me? @Barmar , I already came from youtube tutorial, but the guy doesn't explain the basic things, for that I'm asking here – N m Oct 08 '19 at 19:23
  • Because @Barmar their form failed. – Funk Forty Niner Oct 08 '19 at 19:27
  • Their form never even runs the PHP script. The question is about how to get the form to run the script when it's submitted. @FunkFortyNiner – Barmar Oct 08 '19 at 19:29
  • @Barmar exactly, so where do I need to put this code "
    "
    – N m Oct 08 '19 at 19:34
  • You simply replace `
    ` with that. Like I said before, you just need to add the `action` attribute to the `
    ` tag that you already have. That tells the form where to send the data to.
    – Barmar Oct 08 '19 at 19:36
  • @Barmar now it seems the send button do something, but when I press send button it's open the file "function.php" and doesn't send the email – N m Oct 08 '19 at 19:40
  • See https://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-instead-code-shows-on-the-page – Barmar Oct 08 '19 at 19:42
  • @Barmar Methods default to self if there is no action. I personally think that you should have voted as a typographical error, rather than an answer. I'm sure there are other dupes for this. I can't see why this was reopened. – Funk Forty Niner Oct 08 '19 at 19:44
  • @FunkFortyNiner It probably is a dupe, but I don't have a saved target for this issue. I thought I could help him in comments, but he wasn't getting it, so I reopened so I could answer more clearly. – Barmar Oct 08 '19 at 19:48

1 Answers1

1

You connect an HTML form to the script that processes it using the action attribute of the <form> tag. So you need to change

        <form class="contactform" method="post">

to

        <form action="function.php" class="contactform" method="post">

Without this, it defaults to posting to the same URL as the page containing the form.

A common style is to use a single PHP script to create the form and also process the input, so they leave out this attribute, and the tutorial you read may have assumed this style.

Another problem is that the name of your submit button is name="send", not name="submit". So you need to change

if (isset($_POST['submit']))

to

if (isset($_POST['send']))
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I will look into that link, I use short tags but my php is enables on my server. I will check other options, thanks :) – N m Oct 08 '19 at 19:48
  • If the PHP were enabled on the server you wouldn't get PHP code showing in the result. – Barmar Oct 08 '19 at 19:49
  • I checked again it says " PHP Version ea-php72 " and also I checked the server cpanle php short tag is enabled – N m Oct 08 '19 at 20:08
  • You're not using short tags, so what difference does that make? If you're seeing the PHP script in the browser, you don't have PHP enabled properly. – Barmar Oct 08 '19 at 20:10
  • I don't see any code, the send button only open empty page "function.php" but is white and blank.. – N m Oct 08 '19 at 20:12
  • `$_POST['submit']` should be `$_POST['send']`. – Barmar Oct 08 '19 at 20:12
  • I misunderstood you when you said "it opens the file function.php", I thought you meant that it was showing the PHP code in the browser. – Barmar Oct 08 '19 at 20:13
  • Now it works :D thanks, I received the email, but inside the email it says "invaled adress" but the name and the message arrived, hmm I guess it's something wrong with my email field name – N m Oct 08 '19 at 20:21
  • You have no `$mailFrom` variable. – Barmar Oct 08 '19 at 20:24
  • Maybe it should be `$headers = "From: ".$email;`? – Barmar Oct 08 '19 at 20:24
  • Yup that's solve the problem :) I don't know how to thank you. – N m Oct 08 '19 at 20:27