Search code examples
javascriptphphtmlcssreal-time-updates

How do I get user-inputted information from the website directly to my e-mail?


So I've decided to make a website a few months ago and have then picked up basic HTML, CSS, Javascript and Jquery. Now I want to do two things with my website,

  1. show a user-voted poll somewhere on my website that will update live as the users vote on different optinos.
  2. A form on my website that when the user fill out and click the "submit" button, the form will automatically be forwarded to my desired e-mail address

I've been told to learn PHP because it's suppose to help my problem, but so far I have yet to see how it's going to help me solve the above two problems. Any suggestion on how I can achieve the two things above??

PS: if someone can explain what exactly is PHP, I will also be really grateful. I've been looking up what PHP is and the majority of the responses I get is "it's a server side language", which is not exactly helpful. And if PHP does help me solve the two questions above, how?


Solution

  • PHP is actually a "server side language" (mostly) :)

    It means that it allows your web server to be smart, i.e:

    • load/read data from a database
    • send emails
    • generate pages dynamically (instead of just serving static pages)
    • etc...

    Sending mail with PHP is documented here: https://www.php.net/manual/en/function.mail.php

    Basically in your HTML you'll tell your form to be posted to a specific PHP script which is hosted on your web server, e.g:

    <form action='send_mail.php'>.....</form>
    

    And this script will retrieve the form data and handle the mail sending, e.g:

    <php>
        $data = $_POST; // contains the form's input values
        mail("foo@mail.com", "Notification mail", "A form has been submitted with some data: ".$data["fieldName"]);
    </php>