如何在PHP中自动添加动态文本字段的名称值?

时间:2015-05-22 17:17:12

标签: php html

如果有人选择人员或婴儿,我会有一个表格,总人数会计算,并且乘客名单将根据总人数出现,他们必须填写每位乘客的信息。

对于第二位乘客,每个输入名称将使用不同的名称<input name=" ">,例如"passenger_name_1""passenger_name_2"

现在,如果有人选择1个人并填写表格中的数据,我将得到值的结果但仅限于passenger_name_1但如果有人选择了5个人,我怎么能这样做,我将获得所有5个人的信息?

您可以在JSFIDDLE

上查看

所以如果总人数是5,那么应该在我的php文件中添加这些值并发送填充值:

'Passenger 1: ' . $_POST['passenger_name_1'] . " " . $_POST['passenger_lname_1'] . " " . $_POST['passenger_age_1'] . "<br />";
'Passenger 2: ' . $_POST['passenger_name_2'] . " " . $_POST['passenger_lname_2'] . " " . $_POST['passenger_age_2'] . "<br />";
'Passenger 3: ' . $_POST['passenger_name_3'] . " " . $_POST['passenger_lname_3'] . " " . $_POST['passenger_age_3'] . "<br />";
'Passenger 4: ' . $_POST['passenger_name_4'] . " " . $_POST['passenger_lname_4'] . " " . $_POST['passenger_age_4'] . "<br />";
'Passenger 5: ' . $_POST['passenger_name_5'] . " " . $_POST['passenger_lname_5'] . " " . $_POST['passenger_age_5'] . "<br />";

这是我的PHP

    <?php

if (isset($_POST['bestemming']))
{
$to = 'myemail@mail.com';
$subject = "Reservation";
$message = 

'Trip: ' . $_POST['bestemming'] . "<br /><br />" . 
'Persons: ' . $_POST['vol'] . "<br />" . 
'Kids: ' . $_POST['kinderen'] . "<br />" . 
'Babys: ' . $_POST['babys'] . "<br /><br />" . 

'Passenger infomation: ' . $_POST['passenger_name_1'] . " " . $_POST['passenger_lname_1'] . " " . $_POST['passenger_age_1'] . "<br />";

// Define the main headers.

$headers = 'From:' . $_POST['email'] . "\r\n";
$headers.= 'Reply-To:' . $_POST['email'] . "\r\n";
$headers.= "MIME-Version: 1.0\r\n";
$headers.= "Content-Type: multipart/mixed; ";
$headers.= "boundary=$num\r\n";
$headers.= "--$num\r\n";

// Define the message section

$headers.= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers.= "Content-Transfer-Encoding:8bit\r\n\n";
$headers.= "$message\r\n";
$headers.= "--$num\r\n";

// Send email

mail($to, $subject, '', $headers);
}

对于自动添加基于总人数的字段,这是JS:

$(document).ready(function () {
    var sum = 0;
    $(document).on('change', '.count-select', function () {
        sum = 0;
        $('.count-select').each(function () {
            console.log($(this));
            var thisVal = parseInt($(this).val());
            sum += thisVal;
        });
        $('#person-total').text(sum);
        // you can use here 

        // remove all divs
        $('div[class^="passenger"]').remove();
        // to show divs depending on number of persons
        for (var i = 1; i <= sum; i++) {
            $('.passengerlistinfo').append('<div class="passenger"> <div class="form-group col-md-4 passe"><label for="name"></label> <b>Passenger ' + i + ' </b> <br> <input type="text" class="form-control" placeholder="FirstName" name="passenger_name_' + i + '" ></div><div class="form-group col-md-4"><label for="name"></label><input type="text" class="form-control" placeholder="LastName" name="passenger_lname_' + i + '" ></div><div class="form-group col-md-4"><label for="name"></label><input type="text" class="form-control" placeholder="Age" name="passenger_age_' + i + '" ></div></div></div>');
        }
    });
});

2 个答案:

答案 0 :(得分:0)

也许这有助于你

<?php


    $count = 1;
    $next = true;

    while($next==true){

        //check if passenger_n exits
        if(isset($_POST['passenger_name_'.$count])){
            $message .='Passenger infomation: ' . $_POST['passenger_name_'.$count] . 
                " " . $_POST['passenger_lname_'.$count] . " 
                " . $_POST['passenger_age_'.$count] . "<br />";
        }else{
            //finish the loop
            $next = false;
        }
        //increase the passenger id
        $count++;
    }


?>

答案 1 :(得分:0)

您在使用名称passenger_name_*命名输入时使用了一些反模式。

您应该考虑将名称定义为passenger_name[][] connote输入字段名称,在POST到服务器时应被视为数组的一部分。 PHP将使用这些名称发布值,并将它们组合成一个可用的$_POST['passenger_name']数组。

这极大地简化了服务器端和客户端代码,因为它使您无需在计数器上保留这些具有相似名称的字段的数量。因此,在客户端,您可以仅使用此通用名称轻松添加或删除输入(无需计算有多少输入,并在输入名称的末尾应用某些“索引”值,如_1

在服务器端,您只需访问$_POST中的数组,然后它们将自动以数字方式为您编制索引,因此您无需检查一堆变量,例如$_POST['passenger_name_1'],{ {1}}等等。