duan198409 2013-01-21 20:19
浏览 52
已采纳

PHP / jQuery clone - 从表单获取变量并使用php发送电子邮件

I am self-learner and I am currently building (trying to build) a simple form that has a row with 3 fields, and which dynamically adds another row with the same fields depending on how many items the customer wants. It will have a limit of 20 items. I am using this html layout for the fields that are going to be added dynamically:

<div id="input1" style="margin-bottom:4px;" class="clonedInput">
        Part #: <input name="part'+i+'" type="text" id="part1" size="10" maxlength="15" />
        Description: <input name="description'+i+'" type="text" id="description1" size="30" maxlength="50" />
        Qty: <input name="quantity'+i+'" type="text" id="quantity1" size="5" maxlength="5" />
    </div>
    <p>&nbsp;</p>
    <div>
        <input type="button" id="btnAdd" onclick="dupForm('input', '.clonedInput', 'btnAdd', 'btnDel');" value="add item" />
        <input type="button" id="btnDel" onclick="rmForm('input', '.clonedInput', 'btnAdd', 'btnDel');" value="remove item" />
    </div>

and this java script:

function trimNums(stringToTrim) { return stringToTrim.replace(/\d+$/,""); }

    function dupForm(divId, divClass, btnAdd, btnRm)
    {
    //alert(divId+'   '+divClass);
        var num     = $(divClass).length;
        var newNum  = new Number(num + 1);
        var i;

        var newElem = $('#' + divId + num).clone().attr('id', divId + newNum).fadeIn('slow');

        for (i=0; i < newElem.children().length; i++)
        {
            var attrId = trimNums(newElem.children(':eq('+i+')').attr('id'));
            var attrName = trimNums(newElem.children(':eq('+i+')').attr('name'));

            newElem.children(':eq('+i+')').attr('id', attrId + newNum).val('');
        }
        $('#' + divId + num).after(newElem);
        $('#' + btnRm).attr('disabled','');

        if (newNum == 20)
            $('#' + btnAdd).attr('disabled','disabled');
    }

    function rmForm(divId, divClass, btnAdd, btnRm)
    {
        var num = $(divClass).length;

        $('#' + divId + num).remove();
        $('#' + btnAdd).attr('disabled','');

        if (num-1 == 1)
            $('#' + btnRm).attr('disabled','disabled');
    }
    </script>

My question is: How can I get the variables into PHP so I can send this form to a specific email.

please note: I have more fields on the form and to gather the information in php I am using the following php script:

$emailField = $_POST['email'];
$company_nameField = $_POST['company_name'];
$dateField = $_POST['date'];
$phoneField = $_POST['phone'];

$part1Field = $_POST['part1'];
$part2Field = $_POST['part2'];

$description1Field = $_POST['description1'];
$description2Field = $_POST['description2'];

and to post it on the email i am using this to test:

$body = <<<EOD




Email: $email
Company Name: $company_name
Date: $date
Phone: $phone
$part1 | $description1 | $quantity1
EOD;

I know by gathering the variables of dynamic values, there needs to be a different code and I am stuck there. If someone can direct me to the right solution I will appreciate that very much.

  • 写回答

1条回答 默认 最新

  • dongzongpeng6474 2013-01-21 20:46
    关注

    You can use arrays:

    <div id="inputContainer">
        <div class="clonedInput">
            Part #: <input name="part[]" type="text" class="part" size="10" maxlength="15" />
            <button type="button" class="remove">Remove</button>
            <button type="button" class="clone">Clone</button>
        </div>
    </div>
    <div id="toolbar">
            <button type="button" class="add">Add new</button>
    </div>
    

    Using following code you can remove and clone individual inputs:

    var limit = 20;
    $('#inputContainer').on('click', '.clonedInput>.remove', function(e) {
        $(this).parent().fadeOut().remove();
        if ($(this).parent().parent().length < limit) {
            $('#toolbar>.add').removeAttr('disabled');
        }
    });
    
    $('#toolbar').on('click', '.add', function(e) {
        var newElem = $('<div class="clonedInput">\
            Part #: <input name="part[]" type="text" class="part" size="10" maxlength="15" />\
            <button type="button" class="remove">Remove</button>\
            <button type="button" class="clone">Clone</button>\
        </div>');
    
    
        $('#inputContainer').append(newElem);
        if($('#inputContainer').length == limit) {
            $('#toolbar>.add').attr('disabled', 'disabled');
        }
    });
    
    $('#inputContainer').on('click', '.clonedInput>.clone', function(e) {
        var newElem = $(this).parent().clone().fadeIn('slow');
    
        $(this).parent().after(newElem);
        if ($(this).parent().parent().length == limit) {
            $('#toolbar>.add').attr('disabled', 'disabled');
        }
    });
    

    See the fiddle.

    You can now access individual inputs fields in PHP using array keys:

    echo $_POST['part'][0]; // first 'part' field
    
    // print all fields
    $mailData = '';
    for($i = 0; $i < count($_POST['part']); $i++) {
        $mailData .= '#' . $_POST['part'][$i] . ' | ';
        $mailData .= $_POST['description'][$i] . ' | ';
        $mailData .= $_POST['quantity'][$i] . PHP_EOL;//or replace PHP_EOL for "<br>
    " it will work just fine
    
    }
    
    $body = <<<EOD
    Email: $email
    Company Name: $company_name
    Date: $date
    Phone: $phone
    $mailData
    EOD;
    
    
    your_mail_function($body);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀