Pergunta

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?

Foi útil?

Solução 2

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: http://us2.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>

Outras dicas

PHP standing for Hypertext Preprocessor, is a server side programming language. Taking a look at what you have currently learned (HTML, CSS and Javascript) these are all Client side languages.

The difference is that any processing done by a client side language is done within the browser straight in front of the user's eyes.

A server side language like PHP is used when we want to compile relevant data from sources like a MySQL database, before sending the information to the browser. Meaning it's far more dynamic.

You should start by going through some tutorials, do not use w3schools.com, because I don't believe they teach the best practices and it will probably do you more harm than good in the long run.

A quick google search should lead you to some decent tutorials, however I'd highly recommend http://php.net/manual/en/getting-started.php

PHP is exactly what they have told you - a server side scripting language. What that means is that PHP will perform actions at the server level before rendering a page. This includes:

  • Loading dynamic content from a database
  • Posting information from a form to a database
  • Sending it to an email address like what you want to do
  • A lot of other stuff you'll learn if you stick with it.

To send an email when someone submits your form, the form needs to post to a PHP page.

Your form:

<form method="POST" action="sendmail.php">
  <input type="text" name="name" />
  <!-- Other inputs go here, etc. -->
  <input type="submit" value="submit">
</form>

PHP to send the form (In sendmail.php)

<?php

   $email = $_POST['inputName1'];
   $name = $_POST['inputName2'];
   $message = $_POST['inputName3'];
   $phone = $_POST['inputName4'];
   $yourEmail = "youremail@domain.com";

   $to = $yourEmail;
   $subject = "My Contact Form";
   $message = $message;
   $from = $email;
   $headers = "From:" . $name;
   mail($to,$subject,$message,$headers);
   echo "Mail Sent.";

?>

Here is a tutorial I found via Google: http://www.daniweb.com/web-development/php/threads/38784/php-sendmail-tutorial

There are probably better ones, but that should get you started.

Long story short, you need to learn PHP, and javascript.

Your other requirement (The poll) can be done many different ways, just google "Set up an online poll" for different ways. You can probably use jQuery/javascript and JSON, or XML, etc to power the polls as well as php. This tutorial may help you with that: http://net.tutsplus.com/tutorials/javascript-ajax/creating-a-dynamic-poll-with-jquery-and-php/

Good luck.

PHP is processed at the server level and the results are sent to the client's computer for display. JavaScript does its processing on the client's computer.

You can use the PHP Mail function to have the data entered in the form by the user sent to your email. Here is a link on how to use that: http://www.w3schools.com/php/php_mail.asp

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top