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 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥60 SOL语句中Where查询中的 from to 语句能不能从小到大换成从大到小(标签-SQL)
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 请教一下c语言的代码里有一个地方不懂
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))