I will try to keep it short and simple. I have a Form that Creates multiple fields via Jquery when a button is pushed.
The problem is that when the form is submitted and the Php saves the data in the MYSQL Database. It only saves the first hardcoded field. The generated fields do not submit.
If I hardcode the fields with name="mytext[]"
It works
However, the fields generated by the Jquery do not! they are the same code being submitted. I even went as far as adding a counter to each with name="mytext[+x+]"
in the js file. Still nothing.
Im curious if it has to do with my enviorment. Its a fresh download of XAMPP on windows
It only saves when the fields are hardcoded in. I'm at a loss here. I have used this method before and it has worked.
********UPDATE************** I did a var dump and it is showing that only the first field is being submited.
array(1) { [1]=> string(5) "TEST1" }
What am i doing wrong?
Here are the 3 parts:
HTML
<form class="form-horizontal" method="POST" action="">
<div class="input-group mb-3">
<div class="input-group-prepend">
<label class="input-group-text"
for="release_name">Name</label>
</div>
<input class="form-control" type="text" name="release_name" value="">
</div>
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<label class="input-group-text" for="release_desc">Description</label>
</div>
<input class="form-control" type="text" name="release_desc" value="">
</div>
</div>
<script src="assets\js\multifield.js"></script>
<div class="input_fields_wrap">
<button class="btn add_fields">Add More Fields</button>
<div class="wrapper">
<div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<label class="input-group-text" for="release_desc">New Variable</label>
</div>
<input class="form-control" type="text" name="mytext[1]"></input>
</div>
</div>
</div>
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<label class="input-group-text" for="release_content">Release</label>
</div>
<textarea name="release_content" rows="15" cols="80"></textarea>
</div>
</div>
<div class="form-group col-md-12 text-center">
<input type="hidden" name="company_id" value="<?php if(isset($compid)) echo $compid; ?>">
<input name="updateProfileBtn" type="submit" class="btn btn-primary pull-right bottom-margin" value="Create Release">
</div>
</form>
JS
//Add Input Fields
$(document).ready(function() {
var max_fields = 10; //Maximum allowed input fields
var wrapper = $(".wrapper"); //Input fields wrapper
var add_button = $(".add_fields"); //Add button class or ID
var x = 1; //Initial input field is set to 1
//When user click on add input button
$(add_button).click(function(e){
e.preventDefault();
//Check maximum allowed input fields
if(x < max_fields){
x++; //input field increment
//add input field
$(wrapper).append('<div><div class="input-group mb-3"><div
class="input-group-prepend"><label class="input-group-text"
for="release_desc">New Variable</label></div><input class="form-
control" type="text" name="mytext['+x+']"></div><a
href="javascript:void(0);" class="remove_field">Remove</a></div>');
//add input box
}
});
//when user click on remove button
$(wrapper).on("click",".remove_field", function(e){
e.preventDefault();
$(this).parent('div').remove(); //remove inout field
x--; //inout field decrement
})
});
PHP
else if(isset($_POST['updateProfileBtn'])) {
//Initilaise the array
$form_errors = array();
//Collect data from form
$release_name = $_POST['release_name'];
$release_desc = $_POST['release_desc'];
$release_content = $_POST['release_content'];
$company_id = $_POST['company_id'];
//Create JSON ARRAY FROM MULTI FEILD ARRAY
$fields="";
if(isset($_POST["mytext"]))
{
$fields = json_encode($_POST['mytext']);
}
$id = "";
if(empty($form_errors)){
try{
//create sql update
$sqlUpdate = "INSERT into releases SET id =:id, feilds =:feilds, release_name =:release_name, release_desc =:release_desc, release_content =:release_content, company_id =:company_id";
//Sanitise
$statement = $db->prepare($sqlUpdate);
//update
$statement->execute(array(':id' => $id, ':feilds' => $fields, ':release_name' => $release_name, ':release_desc' => $release_desc, ':release_content' => $release_content, ':company_id' => $company_id));
//Check if one new row was created
if($statement->rowCount() == 1){
$result = '
<script type="text/javascript">
swal("You have a New Release!", "Your new Release has been created", "success");
</script>
';
}
else {