dtvhqlc57127 2015-04-15 08:33
浏览 230
已采纳

将动态输入字段插入数据库

I have successfully add & remove the dynamic input fields. However, I am facing a problem on inserting the input fields' values into my database. Any input on this would be appreciated. Thank you.

Input fields

    <div id = "container"> 
        <p class = "origin-box">
            <label for = "Origin">From <span class = "box-number1">:</span></label>
            <input type = "text" name = "Origin" value = "" id = "Origin"/>
             <a class = "add-box" href = "#" name = "addBox"><img src = "Add Button.png" width = "35" height = "35" hspace = "20" vspace = "10" align = "middle"/></a>
        </p>

        <p class = "destination-box">
            <label for = "Destination">To <span class = "box-number2">:</span></label>
            <input type = "text" name = "Destination" value = "" id = "Destination"/>
        </p>
    </div>

JQuery for dynamically adding/removing input fields.

<script type="text/javascript">
jQuery(document).ready(function($){
var counter = 1;
var max_fields = 10;
$('.my-form .add-box').click(function(e){
    e.preventDefault();
    if (counter < max_fields){
        counter++;
        $('#container').append(
            '<div><strong>Link #' + counter + '</strong><br />' 
            + '<input id="field_' + counter + '" name="fields[]' + '" type="text" placeholder = "From" /><a href = "#" class = "remove-box"><img src = "Remove Button.png" height = "35" width = "35" align = "middle"/></a></div><br />' );
    }
});

$('.my-form').on('click', '.remove-box', function(e){
    $(this).parent().css( 'background-color', '#FF6C6C' );
    $(this).parent().slideUp("fast","linear", function() {
        e.preventDefault();
        $(this).closest('div').remove();    //$(this) references the closest <div>
        counter--;
    });
    return false;
});
});
</script>

After clicked on Submit button.

<p><input id = "btnSubmit" type = "submit" name= "submit" value = "Submit" /></p>

SQL statement for data insertion to database's tables.

if(isset($_POST['btnSubmit'])){
     //create instance of database class
$db = new mysqldb();
$db->select_db();
//Check if user has actually added additional fields to prevent a php error
if ($_POST['fields']) {

    //get last inserted userid
    $inserted_user_id = $db->last_insert_id();

    //Loop through added fields
    foreach ( $_POST['fields'] as $key=>$value ) {

        //Insert into transport table
        $sql_transport = sprintf("INSERT INTO tbl_transport (Origin) VALUES ('%s')",
            mysql_real_escape_string($value));  
        $result_transport = $db->query($sql_transport);
        $inserted_transport_id = $db->last_insert_id(); 

        //Insert into mainclaim table
        $sql_mainclaim = sprintf("INSERT INTO tbl_mainclaim (transportID) VALUES ('%s')",
                               mysql_real_escape_string($inserted_transport_id));  
        $result_mainclaim = $db->query($sql_mainclaim);
    }
} else {    
    //No additional fields added by user
}
//disconnect mysql connection
$db->kill();
}
  • 写回答

1条回答 默认 最新

  • dsi36131 2015-04-16 07:01
    关注

    Your

    if(isset($_POST['btnSubmit'])){
        //Other code
    }
    

    should be

    if(isset($_POST['submit'])){ 
        //Other code
    }
    

    Since the POST value is found by input name and not input id

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化