【问题标题】:Automatic add variables in PHP sent form [closed]在 PHP 发送表单中自动添加变量 [关闭]
【发布时间】:2018-01-19 22:56:16
【问题描述】:

我有一个用 HTML 和 JQuery 制作的表单,它应该将数据传递到一个 php 文件以发送一封包含表单数据的电子邮件。我的表单是结构化的,因此用户可以根据需要单击按钮并添加其他字段。

如何在我的 php 文件中自动创建这些字段变量?

这是我的 HTML 代码:

<form action="sendmailseminari.php" class="form common_font" method="post">
    <div class="choosecontact">
        <div id="showfieldsem">
            <h4 class="parteciptitle">Partecipante principale</h4>
            <div class="field inline-block name">
                <label for="input_type" class="field-label common_font regular medium_font_size form_color">Ente, Comune, Azienda</label>
                <input id="input_type" name="input_type" class="field-input" type="text" required>
            </div>
            <div class="field inline-block name">
                <label for="input_name" class="field-label common_font regular medium_font_size form_color">Nome Cognome</label>
                <input id="input_name" name="input_name" class="field-input" type="text" required>
            </div>
            <div class="field inline-block name">
                <label for="input_tel_sem" class="field-label common_font regular medium_font_size form_color">Numero di telefono</label>
                <input id="input_tel_sem" name="input_tel_sem" class="field-input" type="text" required>
            </div>

            <div id="emailsem" class="field inline-block email">
                <label for="input_email_sem" class="field-label common_font regular medium_font_size form_color">Indirizzo Email (consiglio: email personale che controlli)</label>
                <input id="input_email_sem" name="input_email_sem" class="field-input" type="email" required>
            </div>


            <div id="InputsWrapper" >
            </div>
            <div class="contbtnaddrempart">
                <a href="javascript:void(0)" id="AddMoreFileBox" class="btn btn-primary"><span class="fa fa-user-plus"></span> Aggiungi Partecipante</a>
                <a href="javascript:void(0)" id="RemoveMoreFileBox" class="btn btn-secondary" style="display:none"><span class="fa fa-eraser"></span> Reset</a>
            </div>



            <div class="field msg">
                <label for="input_msg_sem" class="field-label common_font regular medium_font_size form_color">Se vuoi, fai la tua domanda sul DPO</label>
                <input id="input_msg_sem" name="message" class="field-input" type="text">
            </div>

            <div class="inline-block row">
                <div class="col-xs-2">
                    <input id="input_privacycheck_sem" name="PRIVACYCHECK" class="privacycheck" type="checkbox" required>
                </div>
                <div class="col-xs-10">
                    <label for="input_checkprivacy_sem" class="privacyconsent">Acconsento al trattamento dei dati personali come indicato nella Privacy Policy ai fini di questo servizio.</label>
                </div>
            </div>

            <button type="submit" class="send-btn center common_element_color common_font medium body_font_size white"><span class="fa fa-chevron-right"></span> Invia richiesta</button>
        </div>
    </div>
</form>

这是我的脚本:

/*---------------------------
    add/remove partecipanti
----------------------------*/
var InputsWrapper = $("#InputsWrapper");
var AddButton = $("#AddMoreFileBox");
var RemoveButton = $("#RemoveMoreFileBox");
var x = InputsWrapper.length;
var FieldCount = 1;
$(AddButton).click(function(e)//on add input button click
{

    FieldCount++;
    $(InputsWrapper).append('<h4 class="parteciptitle">Partecipante ' + FieldCount + '</h4><div class="field inline-block name"><label for="input_type' + FieldCount + '" class="field-label common_font regular medium_font_size form_color">Ente, Comune, Azienda</label><input id="input_type' + FieldCount + '" name="input_type' + FieldCount + '" class="field-input" type="text" required></div><div class="field inline-block name"><label for="input_name' + FieldCount + '" class="field-label common_font regular medium_font_size form_color">Nome Cognome</label><input id="input_name' + FieldCount + '" name="input_name' + FieldCount + '" class="field-input" type="text" required></div><div class="field inline-block name"><label for="input_tel_sem' + FieldCount + '" class="field-label common_font regular medium_font_size form_color">Numero di telefono</label><input id="input_tel_sem' + FieldCount + '" name="input_tel_sem' + FieldCount + '" class="field-input" type="text" required></div><div id="emailsem' + FieldCount + '" class="field inline-block email"><label for="input_email_sem' + FieldCount + '" class="field-label common_font regular medium_font_size form_color">Indirizzo Email (consiglio: email personale che controlli)</label><input id="input_email_sem' + FieldCount + '" name="input_email_sem' + FieldCount + '" class="field-input" type="email" required></div>');
    x++;
    return false;

});



$(RemoveButton).click(function(e)//on remove input button click
{
    $(InputsWrapper).empty('');
    FieldCount = 1;
});



/* mostra/nasconde bottone reset partecipanti */
$("#AddMoreFileBox").click(function(){
    $("#RemoveMoreFileBox").show();
});

/* torna su quando clicco sul bottone reset partecipanti */
$("#RemoveMoreFileBox").click(function() {
    $('html, body').animate({
        scrollTop: $(".choosecontact").offset().top
    }, 300);
    $("#RemoveMoreFileBox").hide();
});






/*-------------------------
    add form function
-------------------------*/

$('.form').on('focus','.field-input',function(){
    $(this).parent().addClass('is-focused has-label');
});

$('.field-input').each(function(){
    if($(this).val() != ''){
        $(this).parent().addClass('has-label');
    }
});

$('.form').on('blur','.field-input',function(){
    var $parent = $(this).parent();
    if($(this).val() == ''){
        $parent.removeClass('has-label');
    }
    $parent.removeClass('is-focused');
});

这是我的 PHP:

<?php
// Variables
$type = trim($_POST['input_type']);
$name = trim($_POST['input_name']);
$phone = trim($_POST['input_tel_sem']);
$email = trim($_POST['input_email_sem']);
$message = trim($_POST['message']);

// Email address validation - works with php 5.2+
function is_email_valid($email) {
    return filter_var($email, FILTER_VALIDATE_EMAIL);
}


if( isset($input_type) && isset($input_name) && isset($input_tel_sem) && isset($input_email_sem) && isset($message) && is_email_valid($input_email_sem) ) {

    // Avoid Email Injection and Mail Form Script Hijacking
    $pattern = "/(content-type|bcc:|cc:|to:)/i";
    if( preg_match($pattern, $input_name) || preg_match($pattern, $input_email_sem) || preg_match($pattern, $message) ) {
        exit;
    }

    // Email will be send
    $to = "myemailaddress@myemaildomain.extension"; // Change with your email address
    $sub = "$name from Agentar"; // You can define email subject
    // HTML Elements for Email Body
    $body = <<<EOD
    <strong>Tipo:</strong> $input_name <br>
    <strong>Nome:</strong> $input_name <br>
    <strong>Telefono:</strong> $input_tel_sem <br>
    <strong>Email:</strong> <a href="mailto:$input_email_sem?subject=feedback" "Iscrizione Seminario SAEV da LP DPO">$input_email_sem</a> <br> <br>
    <strong>Messaggio:</strong> $message <br>
EOD;
//Must end on first column

    $headers = "Da: $input_name <$input_email_sem>\r\n";
    $headers .= 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    // PHP email sender
    mail($to, $sub, $body, $headers);
}
?>

正如您所看到的,我的 PHP 文件目前只能处理表单中已经创建的现有字段,但我需要让它管理并发送用户可以通过单击添加按钮“Aggiungi Partecipante”创建的字段。

我怎么能这样做?非常感谢您提供带有代码示例的建议。

提前致谢。

【问题讨论】:

标签: javascript php jquery html forms


【解决方案1】:

我试图解释改变一点编码技术。

在您的 HTML 文件中使用以下技术。

<form> <input type="text" name="data[name1]" /> </form>

在您的服务器端使用以下技术。 <?php $postData = $_REQUEST['data']; //you only have to fetch array data so all post data will be get without their name ?>

【讨论】:

  • 感谢 Hitesh 的建议,反正我写的代码不是很好,你能帮我用我的 php 代码做一个功能性的例子吗?
猜你喜欢
  • 1970-01-01
  • 2013-02-05
  • 2017-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-11
  • 2012-11-29
相关资源
最近更新 更多