将动态文本框保存到数据库


Save Dynamic textbox to Database

大家好,我在下面有这段代码,它将在每次单击时动态添加文本框。 我的问题是如何将动态创建的文本框保存到 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 循环(解释)遍历数组中的每个键。

<?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++;
}