My people table has rows which need a value of 1 if checked and 0 if not. I used checkboxes to collect that in my form. I also wanted to allow the user to add more fields so if they can multiple entries can be made at a time.
I scraped out how to collect the unchecked boxes by creating a hidden input for each checkbox by creating a input type=hidden before the checkboxes, so I just copied the whole table row for the jQuery to append.
Unfortunately, the checkbox values mess up on every other row:
(the lName column specifies the input on the following boolean columns: Y should be a check and N is blank)
The first row is correct, but I don't know why the appended rows are being screwed.
<?php
include_once("connections/db.inc.php");
if(isset($_POST['submit'])){
//insert people
$lName=$_POST['lName'];
$fName= $_POST['fName'];
$mName= $_POST['mName'];
$suffixName= $_POST['suffixName'];
$gender= $_POST['gender'];
$birthday= $_POST['birthday'];
$phoneNumber= $_POST['phoneNumber'];
$civilStatus= $_POST['civilStatus'];
$isHeadofFamily=$_POST['isHeadOfFamily'];
$isEmployed=$_POST['isEmployed'];
$isSelfEmployedInBusiness=$_POST['isSelfEmployedInBusiness'];
$isSelfEmployedInInformalSector=$_POST['isSelfEmployedInInformalSector'];
$isSoloParent=$_POST['isSoloParent'];
$isSeniorCitizen=$_POST['isSeniorCitizen'];
$isPWD = $_POST['isPWD'];
$relationToHeadOfFamily= $_POST['relationToHeadOfFamily'];
foreach ($lName as $key => $value) {
$sql = "INSERT INTO `people` (`addressId`, `lName`, `fName`, `mName`, `suffixName`, `gender`, `birthday`, `phoneNumber`, `civilStatus`, `isHeadOfFamily`, `isEmployed`, `isSelfEmployedInBusiness`, `isSelfEmployedInInformalSector`, `isSoloParent`, `isSeniorCitizen`, `isPWD`, `relationToHeadOfFamily`)
VALUES (:lastId,:a2,:b2,:c2,:d2,:e2,:f2,:g2,:h2,:i2,:j2,:k2,:l2,:m2,:n2,:o2,:p2)";
$stmt = $db->prepare($sql);
$stmt->bindParam(":lastId",$lastId);
$stmt->bindParam(":a2",$lName[$key]);
$stmt->bindParam(":b2",$fName[$key]);
$stmt->bindParam(":c2",$mName[$key]);
$stmt->bindParam(":d2",$suffixName[$key]);
$stmt->bindParam(":e2",$gender[$key]);
$stmt->bindParam(":f2",$birthday[$key]);
$stmt->bindParam(":g2",$phoneNumber[$key]);
$stmt->bindParam(":h2",$civilStatus[$key]);
$stmt->bindParam(":i2",$isHeadofFamily[$key]);
$stmt->bindParam(":j2",$isEmployed[$key]);
$stmt->bindParam(":k2",$isSelfEmployedInBusiness[$key]);
$stmt->bindParam(":l2",$isSelfEmployedInInformalSector[$key]);
$stmt->bindParam(":m2",$isSoloParent[$key]);
$stmt->bindParam(":n2",$isSeniorCitizen[$key]);
$stmt->bindParam(":o2",$isPWD[$key]);
$stmt->bindParam(":p2",$relationToHeadOfFamily[$key]);
$stmt->execute();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>BRGY</title>
<script>
$(function() {
var html = '<tr> <td><input type="text" name="lName[]" id="lName" ></td> <td><input type="text" name="fName[]" id="fName" ></td> <td><input type="text" name="mName[]" id="mName"></td> <td><input type="text" name="suffixName[]" id="suffixName" ></td> <td> <select name="gender[]" id="gender"> <option value="Male">Male</option> <option value="Female">Female</option> </select> </td> <td><input type="date" name="birthday[]" id="birthday" ></td> <td><input type="text" name="phoneNumber[]" id="phoneNumber" ></td> <td> <select name="civilStatus[]" id="civilStatus"> <option value="Single">Single</option> <option value="Married">Married</option> <option value="Widowed">Widowed</option> </select> </td> <td><input type="checkbox" name="isHeadOfFamily[]" id="isHeadOfFamily" value="1"> </td> <input type="hidden" name="isHeadOfFamily[]" id="isHeadOfFamilyHIDDEN" value="0"> <td><input type="checkbox" name="isEmployed[]" id="isEmployed" value="1"> </td> <input type="hidden" name="isEmployed[]" id="isEmployedHIDDEN" value="0"> <td><input type="checkbox" name="isSelfEmployedInBusiness[]" id="isSelfEmployedInBusiness" value="1"> </td> <input type="hidden" name="isSelfEmployedInBusiness[]" id="isSelfEmployedInBusinessHIDDEN" value="0"> <td><input type="checkbox" name="isSelfEmployedInInformalSector[]" id="isSelfEmployedInInformalSector" value="1"> </td> <input type="hidden" name="isSelfEmployedInInformalSector[]" id="isSelfEmployedInInformalSectorHIDDEN" value="0"> <td><input type="checkbox" name="isSoloParent[]" id="isSoloParent" value="1"> </td> <input type="hidden" name="isSoloParent[]" id="isSoloParentHIDDEN" value="0"> <td><input type="checkbox" name="isSeniorCitizen[]" id="isSeniorCitizen" value="1"> </td> <input type="hidden" name="isSeniorCitizen[]" id="isSeniorCitizenHIDDEN" value="0"> <td><input type="checkbox" name="isPWD[]" id="isPWD" value="1"> </td> <input type="hidden" name="isPWD[]" id="isPWDHIDDEN" value="0"> <td> <select name="relationToHeadOfFamily[]" id="relationToHeadOfFamily"> <option value="Spouse">Spouse</option> <option value="Child">Child</option> <option value="Sibling">Sibling</option> <option value="Parent">Parent</option> <option value="None">None</option> </select> </td> <td><button type="button" name="addmore" id="addmore">Add More</button></td> </tr>';
var x = 1;
$("#addmore").click(function(){
$("#table_input-people").append(html);
});
$("#table_input-people").on('click','#remove',function(){
$(this).closest('tr').remove();
});
});
</script>
</head>
<body>
<?php include_once("nav.php") ?>
<div>
<form action="" method="post">
<table class="table table-bordered" id="table_input-people">
<tr>
<th>Last Name</th>
<th>First Name</th>
<th>Middle Name</th>
<th>Suffix</th>
<th>Gender</th>
<th>Birthday</th>
<th>Phone Number</th>
<th>Civil Status</th>
<th>Are you the head of family?</th>
<th>Employed</th>
<th>Self Employed in Business</th>
<th>Self Employed in Informal Sector</th>
<th>Solo Parent</th>
<th>Senior Citizen</th>
<th>PWD</th>
<th>Relation to Head of Family</th>
</tr>
<tr>
<td><input type="text" name="lName[]" id="lName" ></td>
<td><input type="text" name="fName[]" id="fName" ></td>
<td><input type="text" name="mName[]" id="mName"></td>
<td><input type="text" name="suffixName[]" id="suffixName" ></td>
<td>
<select name="gender[]" id="gender">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</td>
<td><input type="date" name="birthday[]" id="birthday" ></td>
<td><input type="text" name="phoneNumber[]" id="phoneNumber" ></td>
<td>
<select name="civilStatus[]" id="civilStatus">
<option value="Single">Single</option>
<option value="Married">Married</option>
<option value="Widowed">Widowed</option>
</select>
</td>
<td><input type="checkbox" name="isHeadOfFamily[]" id="isHeadOfFamily" value="1"> </td>
<input type="hidden" name="isHeadOfFamily[]" id="isHeadOfFamilyHIDDEN" value="0">
<td><input type="checkbox" name="isEmployed[]" id="isEmployed" value="1"> </td>
<input type="hidden" name="isEmployed[]" id="isEmployedHIDDEN" value="0">
<td><input type="checkbox" name="isSelfEmployedInBusiness[]" id="isSelfEmployedInBusiness" value="1"> </td>
<input type="hidden" name="isSelfEmployedInBusiness[]" id="isSelfEmployedInBusinessHIDDEN" value="0">
<td><input type="checkbox" name="isSelfEmployedInInformalSector[]" id="isSelfEmployedInInformalSector" value="1"> </td>
<input type="hidden" name="isSelfEmployedInInformalSector[]" id="isSelfEmployedInInformalSectorHIDDEN" value="0">
<td><input type="checkbox" name="isSoloParent[]" id="isSoloParent" value="1"> </td>
<input type="hidden" name="isSoloParent[]" id="isSoloParentHIDDEN" value="0">
<td><input type="checkbox" name="isSeniorCitizen[]" id="isSeniorCitizen" value="1"> </td>
<input type="hidden" name="isSeniorCitizen[]" id="isSeniorCitizenHIDDEN" value="0">
<td><input type="checkbox" name="isPWD[]" id="isPWD" value="1"> </td>
<input type="hidden" name="isPWD[]" id="isPWDHIDDEN" value="0">
<td>
<select name="relationToHeadOfFamily[]" id="relationToHeadOfFamily">
<option value="Spouse">Spouse</option>
<option value="Child">Child</option>
<option value="Sibling">Sibling</option>
<option value="Parent">Parent</option>
<option value="None">None</option>
</select>
</td>
<td><button type="button" name="addmore" id="addmore">Add More</button></td>
</tr>
</table>
<button type ="submit" name="submit">Submit</button>
</form>
</div>
</body>
</html>
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JQuery is arguably the most popular JavaScript library with so many features for modern development. JQuery is a fast and concise JavaScript library created by John Resig in 2006. It is a cross-platform JavaScript library designed to simplify client-side HTML scripting. Over 19 million websites are currently using jQuery! Companies like WordPress, Facebook, Google, IBM and many more rely on jQuery to provide a kind of web browsing experience.
https://jquery.com/
DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL.
It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.