insert data from dynamically created add/remove fields in wordpress $wpdb?
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<script>
jQuery(document).ready(function($) {
$("#Add").on("click", function() {
var backbone = $('.backbone').val();
$('.backbone').val(parseFloat(backbone) + 1);
var newbackbone = $('.backbone').val();
$("#textboxDiv").append("<input type='text' name='a"+ newbackbone +"' placeholder='a " + newbackbone +"'>"+ "<input type='text' name='b"+ newbackbone +"' placeholder='b" + newbackbone +"'>"+ "<input type='text' name='c"+ newbackbone +"' placeholder='c" + newbackbone +"'>"+ "<input type='text' name='d" + newbackbone +"' placeholder='d" + newbackbone +"'>"+ "<input type='text' name='e" + newbackbone +"' placeholder='e" + newbackbone +"'>"+ "<input type='text' name='f" + newbackbone +"' placeholder='f" + newbackbone +"'>"+ "<input type='text' name='g" + newbackbone +"' placeholder='g" + newbackbone +"'><br><br>");
});
});
</script>
<?php
if(isset($_POST['submit'])){
global $wpdb;
$table_name = $wpdb->prefix . "createtable";
$backbone = $_POST['backbone'];
for($i=1;$i<=$backbone;$i++){
$wpdb->insert($table_name, array('field' => $_POST["a".$i], 'field1' => $_POST["b".$i], 'field2' => $_POST["c".$i], 'field3' => $_POST["d".$i], 'field4' => $_POST["e".$i], 'field5' => $_POST["f".$i], 'field6' => $_POST["g".$i]));
}
}
?>
<form action="" method="post">
<input type="text" name="a1" placeholder="a1">
<input type="text" name="b1" placeholder="b1">
<input type="text" name="c1" placeholder="c1">
<input type="text" name="d1" placeholder="d1">
<input type="text" name="e1" placeholder="e1">
<input type="text" name="f1" placeholder="f1">
<input type="text" name="g1" placeholder="g1">
<input type="hidden" name="backbone" class="backbone" value="1">
<div id="textboxDiv"></div>
<input type="button" value="add" name ="add" id="Add">
<input type="button" value="remove" name ="remove" id="Remove">
<input type="submit" name="submit">
</form>
</body>
</html>
Comments
Post a Comment