Good day, ive been reading all the possible questions and answers here in this site all day, i know im almost getting the right answer but it seems some of the suggestions here doesnt work for me. I have a dynamic form where the user can add and remove text fields and submit the request through ajax and php. The form consist of two required text field and buttons to add and remove another field(aside from the two required fields). The user can submit the form even not using another extra field.
My problem is if I press the add button and later on decide to remove it, I am getting a '0' value in corresponding table in database even after pressing the remove button.
Here is my HTML:
<form method="POST">
<span class="text-label">Subject:</span>
<input type="text" name="subject" id="subject-field" placeholder="Subject name here" maxlength="10" class="record-input-forms" /> <span class="text-label">Section:</span>
<input type="text" name="section" id="section-field" placeholder="Subject section here" maxlength="3" class="record-input-forms" /> <a href="#" class="add-field" title="Add student field">+</a> <a class="remove-field" href="#" title="Remove student field">×</a> <a href="#" id="save-button" title="Save">→</a>
<div id="student-box-wrap"></div> <span id="status-message"></span> </form>
Here is my AJAX
$(document).ready(function() {
$("#save-button").click(function() {
var subject = $("input#subject-field").val();
if (subject == "") {
$('#status-message').css({
"color": "#ec3f8c"
});
$('#status-message').html('Please fill the subject fields');
return false;
}
var section = $("input#section-field").val();
if (section == "") {
$('#status-message').css({
"color": "#ec3f8c"
});
$('#status-message').html('Please fill the section fields');
return false;
}
var studid = [];
$('input[name="studid[]"]').map(function() {
studid.push($(this).val());
});
var dataString = 'subject=' + subject + '§ion=' + section + '&studid=' + studid;
$.ajax({
type: "POST",
url: 'save.php',
data: dataString,
dataType: "html",
success: function(data) {
$("input#subject-field").val('');
$("input#section-field").val('');
$("input#field-wrap").remove();
$("#status-message").css({
"color": "#39b1c6"
});
$("#status-message").html('Save successfully');
$("#status-message").fadeOut(2000);
},
error: function(xhr, ajaxOptions, thrownError) {
alert(thrownError);
}
});
return false;
});
});
my Jquery counter
$(document).ready(function() {
var counter = 0;
$(".add-field").click(function() {
counter += 1;
$("#student-box-wrap").append('<div class="field-wrap-' + counter + '"><span id="number-' + counter + '">' + counter + '.</span> Student ID: <input type="text" name="studid[]" class="record-input-forms" /></div>');
});
$(".remove-field").click(function() {
if (counter == 0) {
alert("Nothing to remove!");
} else {
$(".field-wrap-" + counter + "").remove();
counter--;
}
});
});
And my PHP
<?php
require 'connection.php';
session_start();
$studid = (explode(",", $_POST['studid']));
$subject = mysql_real_escape_string(strtoupper($_POST['subject']));
$section = mysql_real_escape_string(strtoupper($_POST['section']));
$adminid = $_SESSION['AdminID'];
mysqli_query($con, "INSERT INTO tbl_subjects(SubjectName, SubjectSection, AdminID) VALUES ('$subject', '$section', '$adminid')");
if (!empty($studid) && !empty($name)) {
foreach ($studid as $new) {
$sql_1 = "INSERT INTO tbl_student(StudentID, SubjectID) VALUES ('$new', LAST_INSERT_ID())";
mysqli_query($con, $sql_1);
}
}
if (!mysqli_query($con, $sql)) {
die('Error: ' . mysqli_error($con));
}
?>
i used !empty in my php and im getting same result. If i dont press the add button at all, im not getting any issue. Its just about when pressing it and even after removing it the variable in ajax seems to carry an empty data to database.