How to insert data from dynamically created add/remove fields in database
-
Hi,
I have created a dynamically created add / remove input fields. I want to insert the data collected in these fields into database.i don’t want to insert it in serialize way. I want each data should be inserted on separate row.
Following is my code:
test.php
<form method="POST"> <div class="control-group before-add-more-fields"> <center><button class="btn btn-success add-fields" type="button"><i class="glyphicon glyphicon-plus"></i> Add Reward</button></center> </div> <div class="custom-fields hide"> <div class="control-group input-group"> <div class="row"> <div class="col-md-12"> <div class="form-group"> <label for="reward_title">Reward Title <b style="color:#FF0000;">*</b></label> <input type="text" class="form-control" name="reward_title[]" id="reward_title" placeholder=""> </div> </div> </div> <br> <button class="btn btn-danger remove-fields" type="button" style="float: right;"><i class="glyphicon glyphicon-plus"></i> Remove</button> <hr> <div class="clear"></div> </div> </div> <script type="text/javascript"> $(document).ready(function() { $(".add-fields").click(function(){ var html = $(".custom-fields").html(); $(".before-add-more-fields").before(html); }); $("body").on("click",".remove-fields",function(){ $(this).parents(".control-group").remove(); }); }); </script> <button class="btn btn-info" type="submit" name="submit_project">Submit Project</button> </form>
functions.php
function insert_in_database_reward_table() { if( isset( $_POST['submit_project'] ) ) { if ($_POST['reward_title']) { foreach ($_POST['reward_title'] as $key => $value) { global $wpdb; $result = $wpdb->insert( 'wpxa_oro_rewards', array( 'project_id' => '67', 'reward_title' => $value ), array( '%d', '%ds' ) ); if ( $result ) { $_SESSION['reward_id'] = $wpdb->insert_id; } else { $_SESSION['reward_id'] = 'Insert failed!'; } } }}} add_action( 'init', 'insert_in_database_reward_table');
But the data is not get inserted. Plz help. Thanks
Viewing 6 replies - 1 through 6 (of 6 total)
Viewing 6 replies - 1 through 6 (of 6 total)
- The topic ‘How to insert data from dynamically created add/remove fields in database’ is closed to new replies.