我已经编写了用于动态创建单选按钮的jquery脚本。之后,我将在PHP中获取那些单选按钮值并将其存储在动态创建的数组中。然后,我想将这些数组值发送到数据库中的特定单元格。我需要知道将数组插入数据库表的正确方法。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
var cur=1, rest=0;
$(document).ready(function() {
var max_fields = 10; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID
var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append('<div> Q3e. Side unit:<br><input type="radio" name="unit'+cur+'" value="With storage"> With storage<br><input type="radio" name="unit'+cur+'" value="Without storage" > Without storage <br><br><input class="remove_field" name="submit" type="submit" value="Remove" style="width:200px;"></div>'); //add input box
cur++;
}
});
$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
});
</script>
<form action="" method="post">
Side unit:<br>
<input type="radio" name="unit0" value="With storage"> With storage<br>
<input type="radio" name="unit0" value="Without storage" > Without storage <br><br>
<div class="input_fields_wrap">
<input class="add_field_button" name="submit" type="submit" value="Add More" style="width:200px;"><br>
</div>
<input type="submit" value="submit">
</form>
<?php
$dynamicarray = array();
for ($i=0; $i <=10 ; $i++) {
$unit1=$_POST['unit'.$i];
if(is_null($unit1)){
break;
}
else{
$dynamicarray[i]=$unit1;
}
}
$sql = "INSERT INTO tbl_questions (side_unit) VALUES (?)";
$stmt= $pdo->prepare($sql);
$stmt->execute([$dynamicarray]);
?>
先谢谢您