我的问题是,我想创建一个注册表,注册人可以添加尽可能多的代表,他想要通过点击"添加代表".问题是表单工作得很好,但是当点击提交时,它只考虑了数据库中添加值的第一行.

我try 了很多方法来解决这个问题,现在有点迷路了.

下面是我的测试网站上的表单链接和我正在使用的代码.

https://www.test.graincomevents.com/246479-2

<?php
// Template Name: Form Processor
get_header();

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    global $wpdb;

     
    // Check if the 'delegate' array is set in $_POST
    if (isset($_POST['delegate']) && is_array($_POST['delegate'])) {
        // Function to sanitize and validate input
        function sanitize_input($data) {
            return is_array($data) ? array_map('sanitize_input', $data) : sanitize_text_field($data);
        }

        // Sanitize and validate each delegate registration data
        foreach ($_POST['delegate'] as $delegate) {
            // Check if array keys exist before accessing them
            if (
                isset($delegate['company']) &&
                isset($delegate['company_country']) &&
                isset($delegate['first_name']) &&
                isset($delegate['last_name']) &&
                isset($delegate['designation']) &&
                isset($delegate['email']) &&
                isset($delegate['phone']) &&
                isset($delegate['appear_on_list'])
            ) {
                $delegate_data = sanitize_input($delegate);

                // Insert delegate data into the database
                $wpdb->insert(
                    'wp_685721_delegate_registration',
                    array(
                        'company' => $delegate_data['company'],
                        'company_country' => $delegate_data['company_country'],
                        'first_name' => $delegate_data['first_name'],
                        'last_name' => $delegate_data['last_name'],
                        'designation' => $delegate_data['designation'],
                        'email' => $delegate_data['email'],
                        'phone' => $delegate_data['phone'],
                        'appear_on_list' => $delegate_data['appear_on_list']
                    )
                );
            }
        }
    }
    // Sanitize and validate billing information
    $billing_info = array_map('sanitize_input', $_POST['billing_info']);

    // Insert billing information into database
    $wpdb->insert(
        'wp_685721_billing_information',
        array(
            'billing_first_name' => $billing_info['billing_first_name'],
            'billing_last_name' => $billing_info['billing_last_name'],
            'billing_email' => $billing_info['billing_email'],
            'billing_company' => $billing_info['billing_company'],
            'billing_country' => $billing_info['billing_country'],
            'billing_address' => $billing_info['billing_address'],
            'billing_postcode' => $billing_info['billing_postcode'],
            'billing_city' => $billing_info['billing_city'],
            'billing_phone' => $billing_info['billing_phone'],
            'billing_vat_number' => isset($billing_info['billing_vat_number']) ? $billing_info['billing_vat_number'] : '',
            'billing_message' => isset($billing_info['billing_message']) ? $billing_info['billing_message'] : ''
        )
    );
    // Display success message or redirect
    echo '<p>Form submitted successfully!</p>';
} else {
    // Display the form here
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Registration and Billing Information</title>
<style>
    table {
        width: 100%;
        border-collapse: collapse;
    }
    th, td {
        padding: 8px;
        border-bottom: 1px solid #ddd;
        text-align: left;
    }
    th {
        background-color: #f2f2f2;
    }
    input[type="text"], input[type="email"], input[type="tel"], select, textarea {
        width: 100%;
        padding: 8px;
        box-sizing: border-box;
    }
    input[type="submit"] {
        padding: 10px 20px;
        background-color: #4CAF50;
        color: white;
        border: none;
        border-radius: 4px;
        cursor: pointer;
    }
    input[type="submit"]:hover {
        background-color: #45a049;
    }
</style>
</head>
<body data-rsssl=1>

<h2>Delegate Registration Form and Billing Information</h2>

<form action="/246479-2" method="post">
    <h3>Delegate Registration</h3>
    <table id="delegateTable">
        <tr>
            <th>ID</th>
            <th>Company*</th>
            <th>Company Country*</th>
            <th>First Name*</th>
            <th>Last Name*</th>
            <th>Designation/ Job Title*</th>
            <th>Email Address*</th>
            <th>Phone* (+XX)</th>
            <th>Appear on the Delegate List*</th>
        </tr>
        <tr>
            <td>Delegate 1</td>
            <td><input type="text" name="delegate[0][company]" required></td>
            <td><input type="text" name="delegate[0][company_country]" required></td>
            <td><input type="text" name="delegate[0][first_name]" required></td>
            <td><input type="text" name="delegate[0][last_name]" required></td>
            <td><input type="text" name="delegate[0][designation]" required></td>
            <td><input type="email" name="delegate[0][email]" required></td>
            <td><input type="tel" name="delegate[0][phone]" required></td>
            <td>
                <select name="delegate[0][appear_on_list]" required>
                    <option value="yes">Yes</option>
                    <option value="no">No</option>
                </select>
            </td>
        </tr>
    </table>
    <button type="button" class="add-row" onclick="addDelegateRow()">Add Delegate</button>

    <hr>

    <h3>Billing Information</h3>
    <table>
        <tr>
            <th>First Name*</th>
            <th>Last Name*</th>
            <th>Email Address*</th>
            <th>Company*</th>
            <th>Country*</th>
            <th>Address*</th>
            <th>Postcode*</th>
            <th>Town/City*</th>
            <th>Phone* (+XX)</th>
            <th>VAT Number (optional)</th>
            <th>Any message for GrainCom Team?</th>
        </tr>
        <tr>
            <td><input type="text" name="billing_info[billing_first_name]" required></td>
            <td><input type="text" name="billing_info[billing_last_name]" required></td>
            <td><input type="email" name="billing_info[billing_email]" required></td>
            <td><input type="text" name="billing_info[billing_company]" required></td>
            <td><input type="text" name="billing_info[billing_country]" required></td>
            <td><input type="text" name="billing_info[billing_address]" required></td>
            <td><input type="text" name="billing_info[billing_postcode]" required></td>
            <td><input type="text" name="billing_info[billing_city]" required></td>
            <td><input type="tel" name="billing_info[billing_phone]" required></td>
            <td><input type="text" name="billing_info[billing_vat_number]"></td>
            <td><textarea name="billing_info[billing_message]"></textarea></td>
        </tr>
    </table>

    <br>
    <input type="submit" value="Submit Registration and Billing Information">
</form>

<script>
    var delegateCount = 1;

    function addDelegateRow() {
        delegateCount++;
        var table = document.getElementById("delegateTable");
        var row = table.insertRow(-1); // Append row at the end of the table
        var cell, input;

        var headers = ["Company", "Company Country", "First Name", "Last Name", "Designation/ Job Title", "Email Address*", "Phone* (+XX)", "Appear on the Delegate List*"];

        cell = row.insertCell(0);
        cell.textContent = "Delegate " + delegateCount;

        for (var i = 1; i <= headers.length; i++) {
            cell = row.insertCell(i);
            if (i == headers.length) { // Handle the "Appear on the Delegate List" field
                var originalSelect = document.querySelector("#delegateTable select");
                var clonedSelect = originalSelect.cloneNode(true);
                clonedSelect.name = "delegate[" + (delegateCount - 1) + "][appear_on_list]";
                // Set the value of the cloned select element to the currently selected value
                var currentValue = originalSelect.options[originalSelect.selectedIndex].value;
                clonedSelect.value = currentValue;
                cell.appendChild(clonedSelect);
            } else {
                input = document.createElement("input");
                input.type = "text";
                input.name = "delegate[" + (delegateCount - 1) + "][" + headers[i - 1].toLowerCase().replace(/\s/g, '_') + "]";
                input.required = true;
                cell.appendChild(input);
            }
        }
    }
</script>
</body>
</html>

<?php
}
get_footer();
?>
说

我试着聊天GPT,我找不到一个好的解决方案,因为我不是一个程序员在PHP!

推荐答案

当添加新的代表designation输入错误

增加了[designation/_job_title].但应该是[designation]

[email_address*][phone*_(+xx)]字段也是错误的

input.name = "delegate[" + (delegateCount - 1) + "][" + headers[i - 1].toLowerCase().replace(/\s/g, '_') + "]";

在脚本下面使用

<script>
    var delegateCount = 1;

    function addDelegateRow() {
        delegateCount++;
        var table = document.getElementById("delegateTable");
        var row = table.insertRow(-1); // Append row at the end of the table
        var cell, input;

        var headers = ["Company", "Company Country", "First Name", "Last Name", "Designation/ Job Title", "Email Address*", "Phone* (+XX)", "Appear on the Delegate List*"];

        var inputFields = ["company", "company_country", "first_name", "last_name", "designation", "email", "phone", "appear_on_list"];

        cell = row.insertCell(0);
        cell.textContent = "Delegate " + delegateCount;

        for (var i = 1; i <= headers.length; i++) {
            cell = row.insertCell(i);
            if (i == headers.length) { // Handle the "Appear on the Delegate List" field
                var originalSelect = document.querySelector("#delegateTable select");
                var clonedSelect = originalSelect.cloneNode(true);
                clonedSelect.name = "delegate[" + (delegateCount - 1) + "][appear_on_list]";
                // Set the value of the cloned select element to the currently selected value
                var currentValue = originalSelect.options[originalSelect.selectedIndex].value;
                clonedSelect.value = currentValue;
                cell.appendChild(clonedSelect);
            } else {
                input = document.createElement("input");
                input.type = "text";
                input.name = "delegate[" + (delegateCount - 1) + "][" + inputFields[i - 1].toLowerCase().replace(/\s/g, '_') + "]";
                input.required = true;
                cell.appendChild(input);
            }
        }
    }
</script>

Php相关问答推荐

Laravel 9如何过滤$的项目与结果?

PHP Array to Powershell exec命令Args

允许Laravel路由指定`withTrashed`,尽管有显式的模型绑定

如何让PHP变量跨越多个脚本调用?

根据WooCommerce中的客户计费国家/地区更改产品价格

如何不重新查询模型A-&>;相关模型B-&>模型

PHP -流加密启用服务器切断字节

Laravel自定义验证规则更正

在PHP项目中安装OpenAI PHP SDK时出现问题

获取作者';在WordPress中使用PHP

HTTPPost请求在从php脚本调用时返回404,但在从node.js脚本调用时有效.终结点有效

如何在PHP中根据会话将用户重定向到不同的页面?

如何在自定义邮箱内容中获取WooCommerce订单项目的详细信息?

根据页面的最后修订日期设置 MediaWiki 内部链接的样式

如果有很多重定向,PHP curl 的行为不像命令行 curl

正则表达式抓取递归括号的内容

如何更改或管理Carbon时区,使其不会来回移动时钟

自定义错误页面不适用于 Apache 和 nginx 设置的 .htaccess

我需要获取传递到这个数组中的数据,但我没有得到它,因为在 Laravel 中数组是这样出现的

无法在 Laravel 10 中安装 jenssegers/mongodb