insert data from dynamically created add/remove fields in wordpress $wpdb?
(second method)
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<script>
$(document).ready(function() {
$("#Add").on("click", function() {
var backbone = $('.backbone').val();
$('.backbone').val(parseFloat(backbone) + 1);
var newbackbone = $('.backbone').val();
$("#textboxDiv").append("<div><input type='text' name='field" + newbackbone +"' placeholder='Field " + newbackbone +"'>"+ "<input type='text' name='field10" + newbackbone +"' placeholder='Field10" + newbackbone +"'>"+ "<input type='text' name='field200" + newbackbone +"' placeholder='Field200" + newbackbone +"'>"+ "<input type='text' name='field300" + newbackbone +"' placeholder='Field300" + newbackbone +"'>"+ "<input type='text' name='field400" + newbackbone +"' placeholder='Field400" + newbackbone +"'>"+ "<input type='text' name='field500" + newbackbone +"' placeholder='Field500" + newbackbone +"'>"+ "<input type='text' name='field600" + newbackbone +"' placeholder='Field600" + newbackbone +"'></div>");
});
$("#Remove").on("click", function() {
$("#textboxDiv").children().last().remove();
});
});
</script>
<?php
if(isset($_POST['submit'])){
$j=$_POST['backbone'];
$n=$j;
$f ="";
for($i=1; $i<=$j; $i++){
${"field$i"} = $_POST["field$i"];
${"field10$i"} = $_POST["field10$i"];
${"field200$i"} = $_POST["field200$i"];
${"field300$i"} = $_POST["field300$i"];
${"field400$i"} = $_POST["field400$i"];
${"field500$i"} = $_POST["field500$i"];
${"field600$i"} = $_POST["field600$i"];
$a= ${"field$i"};
$b= ${"field10$i"};
$c= ${"field200$i"};
$d= ${"field300$i"};
$e= ${"field400$i"};
$f= ${"field500$i"};
$g= ${"field600$i"};
$table_name = $wpdb->prefix . "createtable";
$wpdb->insert($table_name, array('field' => $a, 'field1' => $b, 'field2' => $c, 'field3' => $d, 'field4' => $e, 'field5' => $g, 'field6' => $g)
);
}
}
?>
<form action="" method="post">
<input type="text" name="field1" placeholder="Field1">
<input type="text" name="field101" placeholder="Field101">
<input type="text" name="field2001" placeholder="Field2001">
<input type="text" name="field3001" placeholder="Field3001">
<input type="text" name="field4001" placeholder="Field4001">
<input type="text" name="field5001" placeholder="Field5001">
<input type="text" name="field6001" placeholder="Field6001">
<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