0

我使用下面的脚本在表单中添加动态行。

<TITLE> Add/Remove dynamic rows in HTML table </TITLE>
<SCRIPT language="javascript">
    function addRow(tableID) {

        var table = document.getElementById(tableID);

        var rowCount = table.rows.length;
        var row = table.insertRow(rowCount);

        var colCount = table.rows[0].cells.length;

        for(var i=0; i<colCount; i++) {

            var newcell = row.insertCell(i);

            newcell.innerHTML = table.rows[0].cells[i].innerHTML;
            //alert(newcell.childNodes);
            switch(newcell.childNodes[0].type) {
                case "text":
                        newcell.childNodes[0].value = "";
                        break;
                case "checkbox":
                        newcell.childNodes[0].checked = false;
                        break;
                case "select-one":
                        newcell.childNodes[0].selectedIndex = 0;
                        break;
            }
        }
    }

    function deleteRow(tableID) {
        try {
        var table = document.getElementById(tableID);
        var rowCount = table.rows.length;

        for(var i=0; i<rowCount; i++) {
            var row = table.rows[i];
            var chkbox = row.cells[0].childNodes[0];
            if(null != chkbox && true == chkbox.checked) {
                if(rowCount <= 1) {
                    alert("Cannot delete all the rows.");
                    break;
                }
                table.deleteRow(i);
                rowCount--;
                i--;
            }


        }
        }catch(e) {
            alert(e);
        }
    }

</SCRIPT>


<INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />

<INPUT type="button" value="Delete Row" onclick="deleteRow('dataTable')" />

<TABLE id="dataTable" width="350px" border="1">
    <TR>
        <TD><INPUT type="checkbox" name="chk[]"/></TD>
        <TD><INPUT type="text" name="txt[]"/></TD>
        <TD>
            <SELECT name="country[]">
                <OPTION value="in">India</OPTION>
                <OPTION value="de">Germany</OPTION>
                <OPTION value="fr">France</OPTION>
                <OPTION value="us">United States</OPTION>
                <OPTION value="ch">Switzerland</OPTION>
            </SELECT>
        </TD>
        <TD><INPUT type="text" name="passport[]"/></TD>
    </TR>
</TABLE>

使用以下php:

$chkbox = $_POST['chk'];
$txtbox = $_POST['txt'];
$country = $_POST['country'];
$passport= $_POST['护照'];
foreach($txtbox as $a => $b)
echo "$chkbox[$a] - $txtbox[$a] - $country[$a] - $passport[$a]";

效果很好,让它显示在我的屏幕上。然后我想将它全部集成到我的扩展表单中,我需要将其发送到我的邮件地址......请参阅下面的 php 示例(所有 $_POST 都有附加)。“foreach”代码能够将上述动态行中的所有信息发送到我的邮件地址是什么?我必须在我的 $message 中添加什么才能使其可见?谢谢你的帮助。

$TripType = $_POST['TripType'];
$DepartureDay = $_POST['DepartureDay'];
$DepartureMonth = $_POST['DepartureMonth'];
$DepartureYear = $_POST['DepartureYear'];
$ReturnDay = $_POST['ReturnDay'];
$ReturnMonth = $_POST['ReturnMonth'];
$ReturnYear = $_POST['ReturnYear'];
$Adults = $_POST['Adults'];
$Children411 = $_POST['Children411'];
$Children03 = $_POST['Children03'];
foreach ($_POST['Check'] as $value) {$check_msg .= $value\n";}
$Comments = $_POST['Comments'];
$mail = $_POST['mail'];
$telephone = $_POST['telephone'];
$message = "Booking\n\nTripType: $TripType\n\nDepartureDay: $DepartureDay\nDepartureMonth: $DepartureMonth\nDepartureYear: $DepartureYear\n\nReturnDay: $ReturnDay\nReturnMonth: $ReturnMonth\nReturnYear: $ReturnYear\n\nAdults: $Adults\nChildren411: $Children411\nChildren03: $Children03\n $check_msg\nComments: $Comments\nmail: $mail\ntelephone: $telephone";
$xHeaders = "From: $mail\nX-Mailer: PHP/" . phpversion(); mail ("mymail@gmail.com", internet booking", $message, $xHeaders);

4

2 回答 2

1

这应该做你想要的。您的 HTML 不包含 FORM 或 INPUT/submit,所以我猜它们在那里并且正在工作。

foreach($_POST['chk'] as $num => $value) {
        $check_msg .= "$num = $value \n";
}
于 2012-06-25T16:26:59.467 回答
0

尝试:

$message = '';
foreach ($_POST as $k => $v) {
    $message .= $k.': '.$v.'\n';
}
于 2012-06-25T16:27:57.373 回答