单击按钮时,如何发送包含表单中包含的数据的电子邮件?
So I simply need to collect the information from a HTML form I made and send it via an email. How can I achieve this? Do I need PHP or some stuff like that? What should I put in the HTML code?
$(function() {
/*
number of fieldsets
*/
var fieldsetCount = $('#formElem').children().length;
/*
current position of fieldset / navigation link
*/
var current = 1;
/*
sum and save the widths of each one of the fieldsets
set the final sum as the total width of the steps element
*/
var stepsWidth = 0;
var widths = new Array();
$('#steps .step').each(function(i){
var $step = $(this);
widths[i] = stepsWidth;
stepsWidth += $step.width();
});
$('#steps').width(stepsWidth);
/*
to avoid problems in IE, focus the first input of the form
*/
$('#formElem').children(':first').find(':input:first').focus();
/*
show the navigation bar
*/
$('#navigation').show();
/*
when clicking on a navigation link
the form slides to the corresponding fieldset
*/
$('#navigation a').bind('click',function(e){
var $this = $(this);
var prev = current;
$this.closest('ul').find('li').removeClass('selected');
$this.parent().addClass('selected');
/*
we store the position of the link
in the current variable
*/
current = $this.parent().index() + 1;
/*
animate / slide to the next or to the corresponding
fieldset. The order of the links in the navigation
is the order of the fieldsets.
Also, after sliding, we trigger the focus on the first
input element of the new fieldset
If we clicked on the last link (confirmation), then we validate
all the fieldsets, otherwise we validate the previous one
before the form slided
*/
$('#steps').stop().animate({
marginLeft: '-' + widths[current-1] + 'px'
},500,function(){
if(current == fieldsetCount)
validateSteps();
else
validateStep(prev);
$('#formElem').children(':nth-child('+ parseInt(current) +')').find(':input:first').focus();
});
e.preventDefault();
});
/*
clicking on the tab (on the last input of each fieldset), makes the form
slide to the next step
*/
$('#formElem > fieldset').each(function(){
var $fieldset = $(this);
$fieldset.children(':last').find(':input').keydown(function(e){
if (e.which == 9){
$('#navigation li:nth-child(' + (parseInt(current)+1) + ') a').click();
/* force the blur for validation */
$(this).blur();
e.preventDefault();
}
});
});
/*
validates errors on all the fieldsets
records if the Form has errors in $('#formElem').data()
*/
function validateSteps(){
var FormErrors = false;
for(var i = 1; i < fieldsetCount; ++i){
var error = validateStep(i);
if(error == -1)
FormErrors = true;
}
$('#formElem').data('errors',FormErrors);
}
/*
validates one fieldset
and returns -1 if errors found, or 1 if not
*/
function validateSteps(){
var FormErrors = false;
for(var i = 1; i < fieldsetCount; ++i){
var error = validateStep(i);
if(error == -1)
FormErrors = true;
}
$('#formElem').data('errors',FormErrors);
}
/*
validates one fieldset
and returns -1 if errors found, or 1 if not
*/
function validateStep(step){
if(step == fieldsetCount) return;
var error = 1;
var hasError = false;
$('#formElem').children(':nth-child('+ parseInt(step) +')').find(':input[required]:not(button)').each(function(){
var $this = $(this);
var valueLength = jQuery.trim($this.val()).length;
if(valueLength == '')
{
hasError = true;
$this.css('background-color','red');
}
else
{
$this.css('background-color','yellow');
}
}
);
var $link = $('#navigation li:nth-child(' + parseInt(step) + ') a');
$link.parent().find('.error,.checked').remove();
var valclass = 'checked';
if(hasError)
{
error = -1;
valclass = 'error';
}
$('<span class="'+valclass+'"></span>').insertAfter($link);
return error;
}
/*
if there are errors don't allow the user to submit
*/
$('#registerButton').bind('click',function(){
if($('#formElem').data('errors')){
alert('Please correct the errors in the Form');
return false;
}
});
I took this .PHP file from a website:
<?php
if(isset($_POST['email'])) {
// Debes editar las próximas dos líneas de código de acuerdo con tus preferencias
$email_to = "my.email@email.com";
$email_subject = "Contacto desde el sitio web";
// Aquí se deberían validar los datos ingresados por el usuario
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
echo "<b>Ocurrió un error y el formulario no ha sido enviado. </b><br />";
echo "Por favor, vuelva atrás y verifique la información ingresada<br />";
die();
}
$email_message = "Detalles del formulario de contacto:
";
$email_message .= "Nombre: " . $_POST['first_name'] . "
";
$email_message .= "Apellido: " . $_POST['last_name'] . "
";
$email_message .= "E-mail: " . $_POST['email'] . "
";
$email_message .= "Teléfono: " . $_POST['telephone'] . "
";
$email_message .= "Comentarios: " . $_POST['comments'] . "
";
// Ahora se envía el e-mail usando la función mail() de PHP
$headers = 'From: '.$email_from."
".
'Reply-To: '.$email_from."
" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
echo "¡El formulario se ha enviado con éxito!";
}
?>
How can I implement it that when clicking the "Register" button it sends the email. I know that I need to change some stuff there but how can I connect them?
You have some studying ahead of you. Fortunately, there are free materials that will help you.
I recommend submitting the form using AJAX -- it's not that difficult. At all. What AJAX does is allow you to submit the form without navigating the user to a new page. User stays on page as form is submitted. Here's a free 10-min video that explains it:
https://phpacademy.org/videos/submitting-a-form-with-ajax
Also, there are about 200 ten-minute videos on the next link, and you should do them all. But the ones for sending a form are lessons 98 - 103:
https://www.thenewboston.com/videos.php?cat=11
Summary of FORMS: Forms are comprised of a number of HTML fields (usually
<input>
elements) that are enclosed by<form></form>
tags. When thesubmit
button is pressed, the form data is "POSTED" to the (usually PHP) file specified in the<form action="somefile.php"
attribute of the form tag. Basically, the form elements are collected in a series of varName=varValue pairs and sent to another (often PHP) file for processing.Every HTML element can have a
name=
attribute:
<input type="text" name="fname" />
When the form is submitted, the receiving (PHP) file receives the form data as:
variable name ==> "name=" attribute
andvariable contents ==> field contents
And that, in a nutshell, is how FORMs work.
How do you know what kind of "receiving" file your form will use? If your web page is hosted on a typical "CPanel" hosting account, you can use PHP. If a Microsoft server, you will use an M$ solution (aspx?)
Here are some very simple examples that will help you get the gist of AJAX:
PHP is certainly not the only way to send e-mail from a web form, but it is a very common method (especially for people creating their first form).
Here is one reference that may help you to get started, and explain why other methods (i.e. HTML-only, JavaScript) don't work for your needs:
http://www.html-form-guide.com/email-form/html-email-form.html