大家好,我有下面的代码,每次点击都会动态添加文本框。我的问题是如何将动态创建的文本框保存到MYSQL数据库中
我想保存每行中的每个文本框。表示一个文本框=一行
例如

ID | NAME | Training (the textbox)|
1    john   Driving
2    john   swimming
3    john   running

<!DOCTYPE html>
    <html>
    <head>
    <title>Add or Remove text boxes with jQuery</title>
    <script type="text/javascript" src="//code.jquery.com/jquery-latest.js"></script>
    <style type="text/css">
    <!--
    #main {
        max-width: 800px;
        margin: 0 auto;
    }
    -->
    </style>
    </head>
    <body>
    <div id="main">
        <h1>Add or Remove text boxes with jQuery</h1>
        <div class="my-form">
            <form role="form" method="post">
                  <label for="box1"><span class="namer">Name</span></label>
                   <input type="text" name="name" />
                <p class="text-box">
                    <label for="box1">Box <span class="box-number">1</span></label>
                    <input type="text" name="boxes[]" value="" id="box1" />
                    <a class="add-box" href="#">Add More</a>
                </p>
                <p><input type="submit" value="Submit" /></p>
            </form>
        </div>
    </div>
    <script type="text/javascript">
    jQuery(document).ready(function($){
        $('.my-form .add-box').click(function(){
            var n = $('.text-box').length + 1;
            if( 5 < n ) {
                alert('Stop it!');
                return false;
            }
            var box_html = $('<p class="text-box"><label for="box' + n + '">Box <span class="box-number">' + n + '</span></label> <input type="text" name="boxes[]" value="" id="box' + n + '" /> <a href="#" class="remove-box">Remove</a></p>');
            box_html.hide();
            $('.my-form p.text-box:last').after(box_html);
            box_html.fadeIn('slow');
            return false;
        });
        $('.my-form').on('click', '.remove-box', function(){
            $(this).parent().css( 'background-color', '#FF6C6C' );
            $(this).parent().fadeOut("slow", function() {
                $(this).remove();
                $('.box-number').each(function(index){
                    $(this).text( index + 1 );
                });
            });
            return false;
        });
    });
    </script>
    </body>
    </html>



    // MY PHP Insert code

    <?php
    $con = mysql_connect("localhost","root","");
    if (!$con){
    die("Can not connect: " . mysql_error());
    }
    mysql_select_db("SLL",$con);

//please assume boxes = training

    $name = $_POST['name'];
    $training= $_POST['boxes'];

    $AddQuery ="INSERT INTO db (name,training)VALUES ($name,training)"

   mysql_query($AddQuery, $con);
    ?>

最佳答案

几乎就在那里(除了奇怪的输入错误),$_POST['boxes']是一个数组,所以只要通过数组中的每个键foreach循环(explanation)

<?php
    $con = mysql_connect("localhost","root","");
    if (!$con){
    die("Can not connect: " . mysql_error());
    }
    mysql_select_db("SLL",$con);

    $name = $_POST['name'];

//please assume boxes = training
    foreach($_POST['boxes'] as $textbox){
        $training= $textbox;
        $AddQuery ="INSERT INTO db (name,training)VALUES ($name,$training)";
        mysql_query($AddQuery, $con);

    }
?>

更新
要与两个或多个输入数组循环,即name="boxes[]"name="amount[]",需要假设每个boxes[]都与amount[]相关,即boxes[0]amount[0]相关,boxes[1]amount[1]相关,boxes[n]amount[n]相关。因此,您需要一个计数器,每次循环结束/重新启动时都会递增。请参见$i,您将看到$i的值在每次循环结束时增加1,这是由键$_POST['amount'][$i]的数量捕获的。
修改后的循环将类似于
$i = 0;
foreach($_POST['boxes'] as $textbox){
    $training= $textbox;
    $amount = $_POST['amount'][$i];
    $AddQuery ="INSERT INTO db (name,training,amount)VALUES ($name,$training,$amount)";
    mysql_query($AddQuery, $con);
    $i++;
}

关于php - 将动态文本框保存到数据库,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27815348/

10-11 04:17