dougou8639 2015-03-11 21:31
浏览 74
已采纳

单击按钮时,如何发送包含表单中包含的数据的电子邮件?

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?

  • 写回答

2条回答 默认 最新

  • duanlan7903 2015-03-11 22:56
    关注

    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 the submit 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 and variable 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:

    A simple example

    More complicated example

    Populate dropdown 2 based on selection in dropdown 1

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有没有实力的写手?有过成品的优先
  • ¥15 图像信息库的建立与识别
  • ¥15 韩国网站购物,KG支付的支付回调如何解决
  • ¥15 workstation导入ovf文件,报错,怎么解决呢?
  • ¥15 关于#c语言#的问题:构成555单稳态触发器,采用LED指示灯延时时间,对延时时间进行测量并显示(如楼道声控延时灯)需要Proteus仿真图和C语言代码
  • ¥15 workstation加载centos进入emergency模式,查看日志报警如图,怎样解决呢?
  • ¥50 如何用单纯形法寻优不能精准找不到给定的参数,并联机构误差识别,给定误差有7个?matlab
  • ¥15 workstation加载centos进入emergency模式,查看日志报警如图,没有XFS,怎样解决呢?
  • ¥15 应用商店如何检测在架应用内容是否违规?
  • ¥15 Ubuntu系统配置PX4