2

我正在动态地将文本字段添加到表单中,但现在我需要能够将这些值存储到 mysql 数据库中。如果没有动态创建的字段,我可以做到这一点。这是代码 -

<!DOCTYPE HTML>
<html>
    <head>  
        <title> Company Value form </title>
        <script language = "javascript">
            function addTextField(){
                var element = document.createElement("input");
                element.setAttribute("name", "i");
                element.setAttribute("value", "sample value");
                element.setAttribute("size", 30);

                var twitter = document.getElementById("twitterusernames");
                twitter.appendChild(element);

            }

        </script>
    </head>

    <body>
        <h1> Enter the values </h1>
        <form method = "post" name = "config_form" action = "message.php">
            <table border = "1px" >
                <tr> <td>
                    <b> Twitter </b> <br/> <br/>
                    FeatureId: <input type = "text" name = "FeatureId"> <br/>
                    Image: <input type = "text" name = "Image"> <br/>
                    LabelEn: <input type = "text" name = "LabelEn"> <br/>
                    LabelFr: <input type = "text" name = "LabelFr"> <br/>
                    URL: <input type = "text" name = "URL"> <br/>
                    TwitterUserNames: <input type = "text" name = "TwitterUserName" size = "50">  
                    <input type = "button" value = "Add" onclick = "addTextField()"/>
                    <span id="twitterusernames">&nbsp;</span>
                    <br/>
                    Minimum Count: <input type = "text" name = "MinimumCount"> <br/>
                    Refresh Count: <input type = "text" name = "RefreshCount"> <br/>
                    EmptyMessage English: <input type = "text" name = "EmptyMessageEnglish"> <br/>
                    EmptyMessage French: <input type = "text" name = "EmptyMessageFrench"> <br/>
                    Widget: <input type = "text" name = "Widget"> <br/>
                    Email Share Text: <input type = "text" name = "EmailShareText"> <br/>
                </td> </tr>
            </table>

            <input type="submit" value="Submit">
        </form>
    </body>


</html>

和PHP代码:

<?php   
    include 'database.php';

    $Twitter = array(
        'FeatureId' => $_POST["FeatureId"],
        'Image' => $_POST["Image"],
        'LabelEn' => $_POST["LabelEn"],
        'LabelFr' => $_POST["LabelFr"],
        'URL' => $_POST["URL"],
        'TwitterUserName' => $_POST["TwitterUserName"],
        'MinimumCount' => $_POST["MinimumCount"],
        'RefreshCount' => $_POST["RefreshCount"],
        'EmptyMessageEnglish' => $_POST["EmptyMessageEnglish"],
        'Widget' => $_POST["Widget"],
        'EmailShareText' => $_POST["EmailShareText"]
        );

    $crud = new database();
    $insert = $crud -> insert($Twitter);

    if ($insert) {
        echo "Success";
    }
    else {
        echo "Values were not inserted into the database";
    }

?>
4

4 回答 4

3

添加这个element.setAttribute("name", "i[]");

然后在你的 php 处理程序中

        $Twitter = array(
        'FeatureId' => $_POST["FeatureId"],
        'Image' => $_POST["Image"],
        'LabelEn' => $_POST["LabelEn"],
        'LabelFr' => $_POST["LabelFr"],
        'URL' => $_POST["URL"],
        'TwitterUserName' => $_POST["TwitterUserName"],
        'MinimumCount' => $_POST["MinimumCount"],
        'RefreshCount' => $_POST["RefreshCount"],
        'EmptyMessageEnglish' => $_POST["EmptyMessageEnglish"],
        'Widget' => $_POST["Widget"],
        'EmailShareText' => $_POST["EmailShareText"],
        'extraFields' => serialize( $_POST["i"] ) // Extra fields
        );

        $crud = new database();
        $insert = $crud -> insert($Twitter);

        if ($insert) {
        echo "Success";
        }
        else {
        echo "Values were not inserted into the database";
        }

        ?>

来显示数据。假设您已经拥有$extraFields

<?php
$extraFields = unserialize( $extraFields );

 foreach($extraFields as $extra ):
?>
<input type="text" name="i[]" value="<?php echo $extra; ?> ">
<?php
endforeach;
?>
于 2013-05-15T19:18:42.310 回答
2

给出以 结尾的动态元素名称[]

element.setAttribute("name", "i[]");

然后 PHP 将填充$_POST['i']所有值的数组。

于 2013-05-15T19:08:58.450 回答
2

根据您的脚本,我知道您在单击按钮后添加了文本框。因此,当您添加多个文本框时,您需要将文本框字段名称设为数组,即

<input type="text" name="i[]" value="sample data" />
<input type="text" name="i[]" value="sample data" />
<input type="text" name="i[]" value="sample data" />

修改您的 addTextField() 函数

function addTextField(){
            var element = document.createElement("input");
            element.setAttribute("name", "i[]");
            element.setAttribute("value", "sample value");
            element.setAttribute("size", 30);

            var twitter = document.getElementById("twitterusernames");
            twitter.appendChild(element);

        }

message.php文件中,您可以通过 $_POST[i] 以数组格式获取值。

于 2013-05-15T19:17:29.180 回答
0

如果添加的字段仅包含辅助数据,我只需添加另一列文本类型,然后将这些字段的 JSON 编码值转储到一个块中。

于 2013-05-15T19:06:45.477 回答