简体   繁体   English

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

[英]Save Dynamic textbox to Database

Hello guys I have this code below which will dynamically add textbox per click. 大家好,我在下面提供了此代码,该代码将为每次点击动态添加文本框。 my question is How can i save the dynamically created textbox into MYSQL Database 我的问题是如何将动态创建的文本框保存到MYSQL数据库中

i want to save each textbox in each row. 我想在每一行中保存每个文本框。 meaning one text box = one row 意思是一个文本框=一行

eg 例如

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);
    ?>

Nearly there (apart from the odd typo), $_POST['boxes'] is an array, so just foreach loop (explanation) through each key in the array. $_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);

    }
?>

UPDATE To loop through with two or more input arrays, ie name="boxes[]" and name="amount[]" , it will need to be assumed that each boxes[] is relative to amount[] , ie boxes[0] goes with amount[0] , boxes[1] goes with amount[1] , boxes[n] goes with amount[n] . 更新要遍历两个或多个输入数组,即name="boxes[]"name="amount[]" ,需要假设每个boxes[]相对于amount[] ,即boxes[0]amount[0] boxes[1]boxes[1]amount[1] boxes[n]boxes[n]amount[n] So, you will need a counter that increments every time the loop ends/restarts. 因此,您将需要一个计数器,该计数器每次循环结束/重新启动时都会增加。 See $i , you will see that the value of $i is being increased by 1 at the end of each go round the loop, and this is caught by the amount by key as $_POST['amount'][$i] . 看到$i ,您将看到$i的值在每次循环结束时都增加1,并且被键$_POST['amount'][$i]的金额所捕获。

The revised loop would be something like 修改后的循环类似于

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM