how to show data in textbox in php [duplicate] - php

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 6 years ago.
I have problem in this code. When I press "show button" I want data from database shows in text box and "modify" and "delete" buttons work.
But in this code when I press show button it shows error Notice: Undefined index: first_name, I am stuck in this code. How to resolve this?
include '../../../../database/dbConnection.php';
//-------------------------------------------
$row[0]="";
$isSaveDisabled = true;
$isCreateDisabled=false;
$isModifyDiasbled=true;
$isDeleteDisabled=true;
if (isset ($_POST['create_button'])) {
$isSaveDisabled = false;
$isCreateDisabled=true;
$sql="select ifnull(max(user_id),10000)+1 from user_master";
$res= mysql_query($sql);
$row= mysql_fetch_array($res);
$row[0];
}
if(isset($_POST['modify_button']))
{
}
if(isset($_POST['delete_button']))
{
}
?>
<!--
PHP CODE ENDS FROM HERE-------------------------
-->
<!--
BOOTSTRAP START FROM HERE-------------------------
-->
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../../../../bootStrap/css/bootstrap.css">
<link rel="stylesheet" href="../../../../bootStrap/css/bootstrap.min.css">
<link rel="stylesheet" href="../../../../css/StyleSheet.css">
<script src="../../../../js/ValidateKeyPress.js"></script>
<title></title>
<script>
</script>
</head>
<body>
<p id="demo"></p>
<!-- <div class="page-header1">
Hello
</div>-->
<div class="container jumbotron ">
<div class=" text-center"><h4>USER CREATION</h4></div>
<form class="form-inline " action="" method="POST" name="myForm">
<div class="form-group1">
<label class="control-label" for="ec" id="myID">ENTITY CODE</label>
<select class="form-control2" name="ecode" id="myID">
<option value="">SELECT ENTITY CODE</option>
<option>DOC</option>
<option>EMP</option>
<option>NUR</option>
<option>ORG</option>
<option>TEC</option>
<option>VEN</option>
</select>
</div>
<div class="form-group1">
<label class="control-label" for="uid">USER ID</label>
<input type="text" class="form-control2" name="userid" value="<?php echo $row[0];?>">
</div>
<hr>
<table class="table1 table-borderless table-responsive">
<tbody>
<?php
if(isset($_POST['select_button']))
{
$qrydatabind='SELECT ecode, first_name, middle_name, last_name, father_name, mother_name,
number_of_dependents, dob, gender, identification_mark, marital_status, spouse_name, mobile_number,
email_id, adhar_id, pan_number, passport_number, tin_number, dl_number FROM USER_MASTER ORDER BY user_id DESC
LIMIT 1';
$results1= mysql_query($qrydatabind) or die(mysql_error());
while( $row = mysql_fetch_array( $results1 ) ) {
}
}
?>
<tr>
<th scope="row"></th>
<td><label class="control-label" for="finame" id="myID1" >FIRST NAME</label></td>
<td><input value="<?php echo $row["first_name"]; ?>" type="text" class=" form-control2 input-sm text-uppercase"name="firstname" onkeypress="return allCharacter(event);" ></td>
<td><label class="control-label" for="mname">MIDDLE NAME</label></td>
<td><input type="text" class=" form-control2 input-sm text-uppercase" name="middlename"></td>
<td><label class="control-label" for="lname" id="myID2">LAST NAME</label></td>
<td><input type="text" class=" form-control2 input-sm text-uppercase" name="lastname" onkeypress="return allCharacter(event);"></td>
</tr>
<tr>
<th scope="row"></th>
<td><label class="control-label" for="fname">FATHER NAME</label></td>
<td><input type="text" class=" form-control2 input-sm text-uppercase" name="fathername"></td>
<td><label class="control-label" for="moname">MOTHER NAME</label></td>
<td><input type="text" class="form-control2 input-sm text-uppercase" name="mothername"></td>
<td><label class="control-label" for="nod">NO.OF DEPENDENTS</label></td>
<td><input type="text" class="form-control2 input-sm text-uppercase" name="nod"></td>
</tr>
<tr>
<th scope="row"></th>
<td><label class="control-label" for="dob" id="myID3">D.O.B</label></td>
<td><input type="text" class=" form-control2 input-sm text-uppercase" name="dob" placeholder="DD/MM/YYYY" id="myID3" ></td>
<td><label class="control-label" for="gen" id="myID4">GENDER</label></td>
<td><select id="myID4" class="form-control2" name="gender">
<option value="">SELECT GENDER</option>
<option>M</option>
<option>F</option>
<option>o</option>
</select></td>
<td><label class="control-label" for="idm">IDENTIFICATION MARK</label></td>
<td><input type="text" class=" form-control2 input-sm text-uppercase" name="idmark"></td>
</tr>
<tr>
<th scope="row"></th>
<td><label class="control-label" for="ms">MARITAL STATUS</label></td>
<td><select class=" form-control2" name="mstatus">
<option value="">SELECT STATUS</option>
<option>M</option>
<option>U</option>
</select></td>
<td><label class="control-label" for="sname">SPOUSE NAME</label></td>
<td><input type="text" class=" form-control2 input-sm text-uppercase" name="spname"></td>
<td><label class="control-label" for="mno" id="myID5">MOBILE NO.</label></td>
<td><input type="text" class=" form-control2 input-sm text-uppercase" name="mobileno" maxlength="12" onkeypress="return allnumeric(event);"></td>
</tr>
<tr>
<th scope="row"></th>
<td><label class="control-label" for="em">EMAIL-ID</label></td>
<td><input id="myID6" type="text" class="form-control2 input-sm text-uppercase" name="email" placeholder="sample#gmail.com"></td>
<td><label class="control-label" for="adhar">ADHAR-ID</label></td>
<td><input type="text" class="form-control2 input-sm text-uppercase" name="adharid"></td>
<td><label class="control-label" for="paname">PAN NUMBER</label></td>
<td><input type="text" class=" form-control2 input-sm text-uppercase" name="pannumber"></td>
</tr>
<tr>
<th scope="row"></th>
<td><label class="control-label" for="passno">PASSPORT NO.</label></td>
<td><input type="text" class="form-control2 input-sm text-uppercase" name="passportno"></td>
<td><label class="control-label" for="tno">TIN NUMBER</label></td>
<td><input type="text" class=" form-control2 input-sm text-uppercase" name="tinnumber"></td>
<td><label class="control-label" for="dno">DL NUMBER</label></td>
<td><input type="text" class=" form-control2 input-sm text-uppercase" name="dlnumber"></td>
</tr>
</tbody>
</table>
<div class="btn-group-sm2 text-center">
<button type="submit" class="btn btn-primary" name="create_button" <?php echo $isCreateDisabled?'disabled':'';?>>CREATE</button>
<?php
if($isCreateDisabled) {
echo '<script>document.getElementById("myID").style.color = "red";</script>';
echo '<script>document.getElementById("myID1").style.color = "#ff0000";</script>';
echo '<script>document.getElementById("myID2").style.color = "#ff0000";</script>';
echo '<script>document.getElementById("myID3").style.color = "#ff0000";</script>';
echo '<script>document.getElementById("myID4").style.color = "#ff0000";</script>';
echo '<script>document.getElementById("myID5").style.color = "#ff0000";</script>';
echo '<script>document.getElementById("myID6").style.color = "#ff0000";</script>';
}
?>
<button type="submit" class="btn btn-primary" name="modify_button"<?php echo $isModifyDiasbled?'disabled':'';?>>MODIFY</button>
<button type="submit" class="btn btn-primary" name="delete_button" <?php echo $isDeleteDisabled?'disabled':'';?>>DELETE</button>
<button type="submit" class="btn btn-primary" name="clear_button">CLEAR</button>
<button type="submit" class="btn btn-primary " name="save_button" <?php echo $isSaveDisabled?'disabled':''; ?> onclick="return validateForm();">SAVE</button>
<button type="submit" class="btn btn-primary" name="exit_button">EXIT</button>
<button type="submit" class="btn btn-default text-right" name="search_button">SEARCH</button>
<button type="submit" class="btn btn-default text-right" name="select_button">SHOW</button>
</div>
</form>
</div>
</tbody>
<div class="table-responsive">
<table class="table">
<?php
if(isset($_POST['save_button'])){
$isCreateDisabled=false;
if(isset($_POST['ecode']) && isset($_POST['firstname'])&& isset($_POST['middlename'])&& isset($_POST['lastname'])
&& isset($_POST['fathername'])&& isset($_POST['mothername'])&& isset($_POST['nod']) && isset($_POST['dob'])
&& isset($_POST['gender'])&& isset($_POST['idmark'])&& isset($_POST['mstatus'])&& isset($_POST['spname'])
&& isset($_POST['mobileno'])&& isset($_POST['email'])&& isset($_POST['adharid'])&& isset($_POST['pannumber'])
&& isset($_POST['passportno'])&& isset($_POST['tinnumber'])&& isset($_POST['dlnumber']))
{
$ecode=$_POST['ecode']; $first_name=$_POST['firstname']; $middle_name=$_POST['middlename'];
$last_name=$_POST['lastname']; $father_name=$_POST['fathername']; $mother_name=$_POST['mothername'];
$number_of_dependents=$_POST['nod']; $dob=$_POST['dob']; $gender=$_POST['gender'];
$identification_mark=$_POST['idmark']; $marital_status=$_POST['mstatus'];
$spouse_name=$_POST['spname']; $mobile_number=$_POST['mobileno']; $email_id=$_POST['email'];
$adhar_id=$_POST['adharid'];$pan_number=$_POST['pannumber']; $passport_number=$_POST['passportno'];
$tin_number=$_POST['tinnumber']; $dl_number=$_POST['dlnumber'];
}
$qry="insert into user_master(ecode, first_name, middle_name, last_name, father_name, mother_name,
number_of_dependents, dob, gender, identification_mark, marital_status, spouse_name, mobile_number,
email_id, adhar_id, pan_number, passport_number, tin_number, dl_number)
VALUES('$ecode','$first_name','$middle_name','$last_name','$father_name','$mother_name',
'$number_of_dependents',str_to_date('$dob','%d/%m/%Y'),'$gender','$identification_mark','$marital_status',
'$spouse_name','$mobile_number','$email_id','$adhar_id','$pan_number',
'$passport_number','$tin_number','$dl_number')";
$resultss= mysql_query($qry) or die(mysql_error());
if($resultss)
{
echo "<script>
alert('SuccessFully');
</script>";
}
else
{
return "Error...! Not Inserted.";
}
$qrydatabind='SELECT ecode, first_name, middle_name, last_name, father_name, mother_name,
number_of_dependents, dob, gender, identification_mark, marital_status, spouse_name, mobile_number,
email_id, adhar_id, pan_number, passport_number, tin_number, dl_number FROM USER_MASTER ORDER BY user_id DESC
LIMIT 1';
$results= mysql_query($qrydatabind) or die(mysql_error());
while( $row = mysql_fetch_array( $results ) ) {
echo
"
<div class='table-responsive'>
<table border='1' style= 'background-color: #84ed86; color: #761a9b; ' >
<thead>
<tr>
<th></th>
<th>Entity Code</th>
<th>User Id</th> <th>User Name</th> <th>Father Name</th> <th>Mother Name</th> <th>No.Of Dependents</th>
<th>D.O.B</th> <th>GENDER</th> <th>Id Mark</th> <th>MARITAL STATUS</th> <th>SPOUSE NAME</th>
<th>Mob. Number</th> <th>E-Id</th> <th>ADHAR-ID</th> <th>PAN-No.</th> <th>PASSPORT-No.</th>
<th>TIN-NO.</th> <th>DL-No.</th>
</tr>
</thead>
<tr >
<td> </td>
<td>{$row['ecode']}</td> <td> echo $row[0];</td>
<td>{$row['first_name']} {$row['middle_name']} {$row['last_name']}</td>
<td>{$row['father_name']}</td> <td>{$row['mother_name']}</td>
<td>{$row['number_of_dependents']}</td> <td>{$row['dob']}</td>
<td>{$row['gender']}</td> <td>{$row['identification_mark']}</td>
<td>{$row['marital_status']}</td> <td>{$row['spouse_name']}</td>
<td>{$row['mobile_number']}</td> <td>{$row['email_id']}</td>
<td>{$row['adhar_id']}</td> <td>{$row['pan_number']}</td>
<td>{$row['passport_number']}</td> <td>{$row['tin_number']}</td>
<td>{$row['dl_number']}</td>
</tr> </table>
</div>";
}}
?>
</table>
</div>
</body>
</html>

If edits and delete work that means your select statement is wrong. Maybe u are not properly declaring your fields in your select statement. Id look into that first. What I do when my sql is on error is open up an sql session and try doing the query straight from the sql editor.
Also,
why are you using limit 1? You are fetching a result of records for an array but you are limiting your query to one row?
EDIT:
Dont you have to select user_id in order to use ORDER BY? All you have to do is add user_id to select statement

Related

JQuery Append form not posting in PHP

I am trying to append table row which contains HTML form. The append form data not passing to controller. My Code is -
<form method="post" action="<?=base_url()?>controller/function_name">
<table class="table table-bordered tbsize tb_wp" id="">
<thead>
<tr >
<th class="firstrow">Item No.</th>
<th class="firstrow">Part No.</th>
<th class="firstrow">Qty</th>
<th class="firstrow">Price Quoted </th>
<th class="firstrow"> Notes </th>
<th class="firstrow">Internal Notes</th>
<th colspan="3" class="firstrow2">
<div class="pcrsec">
<div class="row"><span class="pcrsecbor">Perior Customer RFQ</span></div>
<div class="row">
<div class="pcrsec2">
<ul class="list-inline">
<li>Date</li>
<li>Cust No</li>
<li>Price</li>
</ul>
</div>
</div>
</div>
</th>
<th colspan="3" class="firstrow3">
<div class="pcrsec">
<div class="row"><span class="pcrsecbor">Perior Customer P/O</span></div>
<div class="row">
<div class="pcrsec2">
<ul class="list-inline">
<li>Date</li>
<li>Cust No</li>
<li>Price</li>
</ul>
</div>
</div>
</div>
</th>
<th colspan="3" class="firstrow4">
<div class="pcrsec">
<div class="row"><span class="pcrsecbor">Perior Vendor P/O</span></div>
<div class="row">
<div class="pcrsec2">
<ul class="list-inline">
<li>Date</li>
<li>Cust No</li>
<li>Price</li>
</ul>
</div>
</div>
</div>
</th>
<th class="firstrow">MU%</th>
<th></th>
</tr>
</thead>
<tbody id="what_i_do">
<tr>
<td><input type="text" name="item_no[]"></td>
<td colspan="3">
<table>
<tr>
<td><input type="text" class="smlbox" name="part_no[]" onblur="checkavailpart(this)"></td>
<td><input type="number" class="smlbox" name="qty[]"></td>
<td>
<input type="number" class="smlbox price_quotd_cls" name="price_quotd[]">
</td>
</tr>
<tr>
<td colspan="3"><input type="text" class="smlbox2" name="part_desc[]"></td>
</tr>
</table>
</td>
<td><input type="text" name="part_note[]"></td>
<td><input type="text" name="part_internal_note[]"></td>
<td><input type="text" class="datepicker" name="pc_rfq_date[]"></td>
<td><input type="text" name="pc_rfq_cus_no[]"></td>
<td><input type="text" name="pc_rfq_price[]"></td>
<td><input type="text" class="datepicker" name="pc_po_date[]"></td>
<td><input type="text" name="pc_po_cus_no[]"></td>
<td><input type="text" name="pc_po_price[]"></td>
<td><input type="text" class="datepicker" name="pv_po_date[]"></td>
<td><input type="text" name="pv_po_cus_no[]"></td>
<td><input type="text" name="pv_po_price[]"></td>
<td><input type="text" name="mu_per[]" onblur="calculate_quote_rate(this)"></td>
<td><i class="fa fa-plus-circle fa-2x" onclick="what_i_do(this);"></i></td>
</tr>
</tbody>
</form>
And JQuery is:-
<script>
function what_i_do(that) {
initid++;
var objTo = document.getElementById('what_i_do');
var divtest = document.createElement("tr");
divtest.setAttribute("class", "removeclass" + initid);
divtest.setAttribute("id", "removeclass" + initid);
var rdiv = 'removeclass' + initid;
divtest.innerHTML = '<td><input type="text" name="item_no[]"></td><td colspan="3"> <table> <tr> <td><input type="text" class="smlbox" name="part_no[]" onblur="checkavailpart(this)"></td><td><input type="number" class="smlbox" name="qty[]"></td><td> <input type="number" class="smlbox price_quotd_cls" name="price_quotd[]"> </td></tr><tr> <td colspan="3"><input type="text" class="smlbox2" name="part_desc[]"></td></tr></table> </td><td><input type="text" name="part_note[]"></td><td><input type="text" name="part_internal_note[]"></td><td><input type="text" class="datepicker" name="pc_rfq_date[]"></td><td><input type="text" name="pc_rfq_cus_no[]"></td><td><input type="text" name="pc_rfq_price[]"></td><td><input type="text" class="datepicker" name="pc_po_date[]"></td><td><input type="text" name="pc_po_cus_no[]"></td><td><input type="text" name="pc_po_price[]"></td><td><input type="text" class="datepicker" name="pv_po_date[]"></td><td><input type="text" name="pv_po_cus_no[]"></td><td><input type="text" name="pv_po_price[]"></td><td><input type="text" name="mu_per[]" onblur="calculate_quote_rate(this)"></td><td><i class="fa fa-times-circle fa-2x" style="color:red" onclick="remove_education_fields(' + initid + ');"></i></td>';
objTo.appendChild(divtest);
}
</script>
When I am submitting the form I use to get only zero index data. I have tried to clone the table row but there also I am not getting the value of the form which have been appended by the JQuery.
Please Help me out of this Problem
Thank You in advance.
in your appended html :
divtest.innerHTML = '<td><input type="text" name="item_no[]"></td><td colspan="3"> <table> <tr> <td>.....
remove all spaces between tags
First make sure that your submit button is inside form tag,
then you can just append your html code inside tbody tag :
#what_i_do refer to tbody tag
$("#what_i_do tr").append('<tr class="removeclass'+id+'" id="removeclass'+id+'">'+ 'html code'+ '</tr>');

Want to search by product_id and fetch all the data based on product_id in form field

I'm building an Inventory management system but stuck with a search query.
I've tables named:
products
product_purchases
product_sales
Now, want to get product details from "products" table by searching "product_id" and fetch/show the product details in the appropriate HTML fields then add/insert the product details data into "product_purchases" table.
Similarly, want to get product details from "product_purchases" table by searching "product_purchase_id" and fetch/show the product details in the appropriate HTML fields then add/insert the product details data into "product_sales" table.
The problem is - I used $.post method to query my product details but it won't work.
Here I just added the screenshot of my purchase
code of my purchase page:
<form action="" method="post" class="form-search">
<div class="input-group">
<input type="text" class="form-control" name="product_id" id="product_id" placeholder="Search a product by ID">
<span class="input-group-btn">
<button name="searchProduct" value="Search" class="btn btn-primary">Search</button>
</span>
</div>
</form>
<form action="" method="post">
<fieldset id="general-settings">
<legend><h3 class="fields-heading">Item details</h3></legend>
<?php
if (isset($_POST['addPurchase'])){
$product_id = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['product_id']));
$product_name = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['product_name']));
$supplier_name = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['supplier_name']));
$brand_name = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['brand_name']));
$group_name = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['group_name']));
$unit_price = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['unit_price']));
$quantity = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['quantity']));
$bonus_quantity = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['bonus_quantity']));
$total_price = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['total_price']));
$total_bonus_price = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['total_bonus_price']));
$unit_sale_price = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['unit_sale_price']));
if ( empty($product_id) && empty($product_name) && empty($supplier_name) && empty($brand_name) && empty($group_name) && empty($unit_price) && empty($quantity) && empty($bonus_quantity) && empty($total_price) && empty($total_bonus_price) && empty($unit_sale_price) ){
echo "<p class='text-error'>Star (*) marks are required fields and cannot be empty!</p>";
}else{
$insert = $db->insert("purchases", "product_id, product_name, supplier_id, brand_id, group_id, unit_price, quantity, bonus_quantity, total_price, total_bonus_price, unit_sale_price", "'$product_id', '$product_name', '$supplier_name', '$brand_name', '$group_name', '$unit_price', '$quantity', '$bonus_quantity', '$total_price', $total_bonus_price, '$unit_sale_price'");
if($insert){ ?>
<script>
alert("Product added successfully!");
window.location.assign("inventory-purchase.php");
</script>
<?php
}else{
echo "<p class='text-error'>Product not added!</p>";
}
}
}
?>
<table class="table table-responsive table-settings">
<tbody>
<tr>
<div class="form-group">
<td><label for="product_id">Product ID <span class="required">*</span></label></td>
<td>:</td>
<td><input type="text" class="form-control" name="product_id" id="product_id"></td>
</div>
</tr>
<tr>
<div class="form-group">
<td><label for="product_name">Name <span class="required">*</span></label></td>
<td>:</td>
<td><input type="text" class="form-control" name="product_name" id="product_name"></td>
</div>
</tr>
<tr>
<div class="form-group">
<td><label for="supplier_name">Supplier <span class="required">*</span></label></td>
<td>:</td>
<td>
<select class="form-control" name="supplier_name" id="supplier_name">
<option>Select a supplier</option>
<?php $getsuppliers = $db->select("supplier_id, supplier_name", "suppliers");
if ($getsuppliers) {
while ( $getsplr = $getsuppliers->fetch_assoc() ){
?>
<option value="<?php echo $getsplr['supplier_id']; ?>"><?php echo $getsplr['supplier_name']; ?></option>
<?php } }?>
</select>
</td>
</div>
</tr>
<tr>
<div class="form-group">
<td><label for="brand_name">Brand <span class="required">*</span></label></td>
<td>:</td>
<td>
<select class="form-control" name="brand_name" id="brand_name">
<option>Select a brand</option> <?php $getbrands = $db->select("brand_id, brand_name", "brands");
if ($getbrands) {
while ( $getbrnd = $getbrands->fetch_assoc() ){
?>
<option value="<?php echo $getbrnd['brand_id']; ?>"><?php echo $getbrnd['brand_name']; ?></option>
<?php } }?>
</select>
</td>
</div>
</tr>
<tr>
<div class="form-group">
<td><label for="group_name">Group <span class="required">*</span></label></td>
<td>:</td>
<td>
<select class="form-control" name="group_name" id="group_name">
<option>Select a group</option>
<?php $getgroups = $db->select("group_id, group_name", "groups");
if ($getgroups) {
while ( $getgrp = $getgroups->fetch_assoc() ){
?>
<option value="<?php echo $getgrp['group_id']; ?>"><?php echo $getgrp['group_name']; ?></option>
<?php } }?>
</select>
</td>
</div>
</tr>
<tr>
<div class="form-group">
<td><label for="unit_price">Unit price <span class="required">*</span></label></td>
<td>:</td>
<td>
<div class="input-group">
<span class="input-group-addon">$</span>
<input type="text" class="form-control" name="unit_price" id="unit_price">
</div>
</td>
</div>
</tr>
<tr>
<div class="form-group">
<td><label for="quantity">Quantity <span class="required">*</span></label></td>
<td>:</td>
<td><input type="text" class="form-control" name="quantity" id="quantity"></td>
</div>
</tr>
<tr>
<div class="form-group">
<td><label for="bonus_quantity">Bonus Quantity</label></td>
<td>:</td>
<td><input type="text" class="form-control" name="bonus_quantity" id="bonus_quantity"></td>
</div>
</tr>
<tr>
<div class="form-group">
<td><label for="total_price">Total price</label></td>
<td>:</td>
<td>
<div class="input-group">
<span class="input-group-addon">$</span>
<input readonly type="text" class="form-control" name="total_price" id="total_price">
</div>
</td>
</div>
</tr>
<tr>
<div class="form-group">
<td><label for="total_bonus_price">Total bonus price</label></td>
<td>:</td>
<td>
<div class="input-group">
<span class="input-group-addon">$</span>
<input readonly type="text" class="form-control" name="total_bonus_price" id="total_bonus_price">
</div>
</td>
</div>
</tr>
<tr>
<div class="form-group">
<td><label for="unit_sale_price">Unit sale price <span class="required">*</span></label></td>
<td>:</td>
<td>
<div class="input-group">
<span class="input-group-addon">$</span>
<input type="text" class="form-control" name="unit_sale_price" id="unit_sale_price">
</div>
</td>
</div>
</tr>
<tr>
<td colspan="3"><input type="submit" name="addPurchase" value="Add purchase" class="btn btn-primary btn-lg btn-register"></td>
</tr>
</tbody>
</table>
</fieldset>
</form>
and sale product page.
ocde of my sales page
<form action="" method="post" class="form-search">
<div class="input-group">
<input type="text" class="form-control" name="product_id" id="product_id" placeholder="Search a product by ID">
<span class="input-group-btn">
<button name="searchProduct" value="Search" class="btn btn-primary">Search</button>
</span>
</div>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$product_id = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['product_id']));
$product_name = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['product_name']));
$unit_sale_price = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['unit_sale_price']));
$quantity = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['quantity']));
$total_sale_price = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['total_sale_price']));
if ( empty($product_id) && empty($quantity) ){
echo "<p class='text-error'>Star (*) marks are required fields and cannot be empty!</p>";
}else{
$insert = $db->insert("sales", "product_id, product_name, unit_sale_price, quantity, total_sale_price", "'$product_id', '$product_name', '$unit_price', '$quantity', '$total_sale_price'");
if($insert){ ?>
<script>
alert("Product sent to invoice successfully!");
window.location.assign("inventory-sale.php");
</script>
<?php
}else{
echo "<p class='text-error'>Product not sent to invoice!</p>";
}
}
}
?>
<form action="" method="post">
<fieldset id="general-settings">
<legend><h3 class="fields-heading">Item details</h3></legend>
<table class="table table-responsive table-settings">
<tbody>
<tr>
<div class="form-group">
<td><label for="product_id">Product ID <span class="required">*</span></label></td>
<td>:</td>
<td><input type="text" class="form-control" name="product_id" id="product_id" placeholder="Search an item by ID"></td>
</div>
</tr>
<tr>
<div class="form-group">
<td><label for="product_name">Product name</label></td>
<td>:</td>
<td><input type="text" readonly class="form-control" name="product_name" id="product_name" value=""></td>
</div>
</tr>
<tr>
<div class="form-group">
<td><label for="unit_sale_price">Unit sale price</label></td>
<td>:</td>
<td>
<div class="input-group">
<span class="input-group-addon">$</span>
<input type="number" readonly class="form-control" name="unit_sale_price" id="unit_sale_price" value="">
</div>
</td>
</div>
</tr>
<tr>
<div class="form-group">
<td><label for="quantity">Quantity <span class="required">*</span></label></td>
<td>:</td>
<td><input type="number" class="form-control" name="quantity" id="quantity" placeholder="Enter quantity"></td>
</div>
</tr>
<tr>
<div class="form-group">
<td><label for="total_sale_price">Total price</label></td>
<td>:</td>
<td>
<div class="input-group">
<span class="input-group-addon">$</span>
<input readonly type="text" disabled class="form-control" name="total_sale_price" id="total_sale_price">
</div>
</td>
</div>
</tr>
<tr>
<td colspan="3"><input type="submit" name="sale_invoice" value="Sent to invoice" class="btn btn-primary btn-lg btn-register"></td>
</tr>
</tbody>
</table>
</fieldset>
</form>
FYI: I'm working with PHP OOP, but not any framework.
Anybody around to help me out of this problem?
Try to use ajax request with post method to get data and populate in your forms and archive your next step
how to use ajax with OOP
It help you get data without refreshing and populate in your form fields. Same for PHP just call your controller with ajax request then same OOP way get data from database from Model and return data in JSON format and use in your fields.
Need more information feel free to ask :)

Insert to Database on HTML Form Submit

I have a form that allows a user to enter data. At the end, there is a submit button that can be pressed that submits the data entered, into a database. I had this working a few weeks ago but had to take a detour on a few other projects. I've come back to it now and the insertion doesn't seem to be working and I am not sure why.
So I am mainly focused on the bottom of the code with the SQL statements but have provided the other HTML for your reference. Do you see any problems that I may have in my SQL statements at all?
Also note that the page itself does not load if this section of sql code is not commented out:
INSERT INTO Stage_Rebate_Agreements
(
Terms,
Tier,
`Tier Minimum`,
Multiplier,
UOM,
Retro,
Guaranteed,
Pay
)
VALUES
(
'$_POST[rows[0][0][term]]',
'$_POST[tier]',
'$_POST[rows[0][0][purchase_minimum]]',
'$_POST[rows[0][0][multiplier]]',
'$_POST[rows[0][0][uom]]',
'$_POST[rows[0][0][retro]]',
'$_POST[rows[0][0][guaranteed]]',
'$_POST[rows[0][0][paid]]'
)
But if it is not commented out, the page does not load for some reason.
If we can somehow get this fixed, it would be much appreciated!
<?php
$host="xxxxxxxx";
$dbName="xxxxxxxxx";
$dbUser="xxxxxxx";
$dbPass="xxxxxxxxxxxxx";
$dbh = new PDO("sqlsrv:Server=$host;Database=$dbName", $dbUser, $dbPass);
$usersQuery = "SELECT MR_Name, MR_POC_N, MR_POC_E, MR_POC_P FROM Stage_Rebate_Master";
$users = $dbh->query($usersQuery);
?>
<html>
<body>
<div class="wrapper">
<header>
<h2>test</h2>
</header>
<div class="container">
<form name="form1" action="confirmation.php" method="POST">
<!-- Vendor -->
<section class="desc-block-left0" align="left">
<div>
<div id="vendor">
<strong>Vendor:</strong>
</div>
<div class="align">
<select name="vendor_dropdown" id="ven" onChange="updateinput();">
<option value="">Choose a Vendor</option>
<?php foreach($users->fetchAll() as $user): ?>
<option
data-name="<?php echo $user['MR_POC_N'];?>"
data-email="<?php echo $user['MR_POC_E'];?>"
data-phone="<?php echo $user['MR_POC_P'];?>"
>
<?php echo $user['MR_Name'];?>
</option>
<?php endforeach; ?>
</select>
</div>
</div>
</section>
<!-- Program Name -->
<section class="desc-block-right0">
<div>
<div id="program_name">
<strong>Program Name:</strong><br>
</div>
<div class="align1">
<input class="textbox" type="text" id="program_name1" name="program_name" style="width: 50%">
</div>
</div>
</section>
<!-- Program Period -->
<section class="desc-block-left1">
<div>
<div id="program_period">
<strong>Program Period:</strong>
</div>
<div class="align">
<input type="text" name="program_period_dropdown" id="period" value="<?php echo date("Y");?>" readonly style="width: 50px">
</div>
</div>
</section>
<!-- Terms -->
<section class="desc-block-right1">
<div>
<div id="terms">
<strong>Terms:</strong><br>
</div>
<div class="align1">
<input type="text" id="year" name="term" style="width: 50px">
<label for="percent">%</label>
<input type="text" id="year" name="term" style="width: 50px">
<label for="percent">Net</label>
<input type="text" id="year" name="term" style="width: 50px">
</div>
</div>
</section>
<!-- Vendor Type -->
<section class="desc-block-left2">
<div>
<div id="vendor_type">
<strong>Vendor Type:</strong><br>
</div>
<div class="align">
<table align="left">
<tr align="center">
<td><input type="radio" name="ven_type" value="Coded" id="type" checked="true"></td>
<td align="left">Coded</td>
</tr>
<tr align="center">
<td><input type="radio" name="ven_type" value="Non-Coded" id="type"></td>
<td>Non-Coded</td>
</tr>
</table>
</div>
</div>
</section>
<!-- Contact Information -->
<section class="desc-block-right2" align="center">
<div>
<div id="contact_info">
<strong>Contact Information:</strong><br>
</div>
<div class="align1">
<table align="left" id="contact">
<tr align="left">
<th>Name</th>
<th>Email</th>
<th>Phone Number</th>
</tr>
<tr>
<td><input type="text" id="name" class="name" name="name"></td>
<td><input type="email" id="email" class="email" name="email"></td>
<td><input type="tel" id="tel" class="tel" name="number"></td>
<td><input type="button" class="save" name="edit" value="Save"></td>
</tr>
</table>
</div>
</div>
</section>
<!-- 400 Rebate Tables -->
<!-- 400P -->
<div align="center" id="border">
<div id="rebate_400p">
<strong>400P</strong><br>
</div>
<table id="tables" cellspacing="5">
<tr align="center" class="table_titles">
<td>Tier</td>
<td>Purchase Minimum</td>
<td>Multiplier</td>
<td>UOM</td>
<td>Retro</td>
<td>Guaranteed</td>
<td>Paid</td>
<td>Delete?</td>
<td>Add Row</td>
</tr>
<tr>
<td align="center" name="tier" id="tier">1</td>
<td><input type="text" class="rebate_tables" id="purchase_min" data-name="purchase_minimum" name="rows[0][0][purchase_minimum]"></td>
<td><input type="text" class="rebate_tables" id="multiplier" data-name="multiplier" name="rows[0][0][multiplier]"></td>
<td><input type="text" class="rebate_tables" id ="uom" data-name="uom" name="rows[0][0][uom]"></td>
<td><input type="text" class="rebate_tables" id="retro" data-name="retro" name="rows[0][0][retro]"></td>
<td><input type="text" class="rebate_tables" id="guaranteed" data-name="guaranteed" name="rows[0][0][guaranteed]"></td>
<td><input type="text" class="rebate_tables" id="paid" data-name="paid" name="rows[0][0][paid]"></td>
<td><input type="button" class="delRow" value="Delete" onclick="deleteRow(this)"></td>
<td><input type="button" class="addmoreRowsbutton" value="Add row" onclick="insRow()"></td>
</tr>
</table>
<!-- 400M -->
<div id="rebate_400m">
<strong>400M</strong><br>
</div>
<table cellspacing="5" id="tables1" style="display: none;">
<tr align="center" class="table_titles">
<td>Tier</td>
<td>Purchase Minimum</td>
<td>Multiplier</td>
<td>UOM</td>
<td>Retro</td>
<td>Guaranteed</td>
<td>Paid</td>
<td>Delete?</td>
<td>Add Rows</td>
</tr>
<tr>
<td align="center" name="tier" id="tier1">1</td>
<td><input type="text" class="rebate_tables" id="purchase_min1" data-name="purchase_minimum" name="rows[1][0][purchase_minimum]"></td>
<td><input type="text" class="rebate_tables" id="multiplier1" data-name="multiplier" name="rows[1][0][multiplier]"></td>
<td><input type="text" class="rebate_tables" id ="uom1" data-name="uom" name="rows[1][0][uom]"></td>
<td><input type="text" class="rebate_tables" id="retro1" data-name="retro" name="rows[1][0][retro]"></td>
<td><input type="text" class="rebate_tables" id="guaranteed1" data-name="guaranteed" name="rows[1][0][guaranteed]"></td>
<td><input type="text" class="rebate_tables" id="paid1" data-name="paid" name="rows[1][0][paid]"></td>
<td><input type="button" class="delRow" value="Delete" onclick="deleteRow(this)"></td>
<td><input type="button" class="addmoreRowsbutton" value="Add row" onclick="insRow1()"></td>
</tr>
</table>
<!-- Button to display table for 400M -->
<input type="button" name="row" value="+" onclick="show2();"/>
<!-- 400D -->
<div id="rebate_400d">
<strong>400D</strong><br>
</div>
<table cellspacing="5" id="tables2" style="display: none;">
<tr align="center" class="table_titles">
<td>Tier</td>
<td>Purchase Minimum</td>
<td>Multiplier</td>
<td>UOM</td>
<td>Retro</td>
<td>Guaranteed</td>
<td>Paid</td>
<td>Delete?</td>
<td>Add Rows</td>
</tr>
<tr>
<td align="center" name="tier" id="tier2">1</td>
<td><input type="text" class="rebate_tables" id="purchase_min2" data-name="purchase_minimum" name="rows[2][0][purchase_minimum]"></td>
<td><input type="text" class="rebate_tables" id="multiplier2" data-name="multiplier" name="rows[2][0][multiplier]"></td>
<td><input type="text" class="rebate_tables" id ="uom2" data-name="uom" name="rows[2][0][uom]"></td>
<td><input type="text" class="rebate_tables" id="retro2" data-name="retro" name="rows[2][0][retro]"></td>
<td><input type="text" class="rebate_tables" id="guaranteed2" data-name="guaranteed" name="rows[2][0][guaranteed]"></td>
<td><input type="text" class="rebate_tables" id="paid2" data-name="paid" name="rows[2][0][paid]"></td>
<td><input type="button" class="delRow" value="Delete" onclick="deleteRow(this)"></td>
<td><input type="button" class="addmoreRowsbutton" value="Add row" onclick="insRow2()"></td>
</tr>
</table>
<!-- Button to adisplay table for 400D -->
<input type="button" name="row" value="+" id="plus-button" onclick="show3();"/>
</div>
<!-- Products -->
<div id="align">
<p>
<div id="products">
<strong>Products:</strong><br>
</div>
<input type="radio" name="product" value="All" onclick="hide();"/ checked> All
<input type="radio" name="product" value="Category" onclick="show();"/> Category
<input type="radio" name="product" value="Sku" onclick="hide();"/> Sku
<select multiple id="area" name="category" style="display: none;">
<option value="Example A" align ="center">Example A</option>
<option value="Example B" align ="center">Example B</option>
<option value="Example C" align ="center">Example C</option>
<option value="Example D" align ="center">Example D</option>
<option value="Example E" align ="center">Example E</option>
<option value="Example F" align ="center">Example F</option>
</select>
</p>
<!-- Submit Button -->
<br>
<input type="submit" value="Submit" id="btn" name="submit">
</div>
</form>
</div>
<div class="push"></div>
</div>
<div class="footer">
<footer>
<h3>test</h3>
</footer>
</div>
</body>
</html>
<?php
if(isset($_POST['submit']))
{
$host="xxxxxxxxxxx";
$dbName="xxxxxx";
$dbUser="xxxxxxxxxxxxxx";
$dbPass="xxxxxxxxxx";
$pdo = new PDO("sqlsrv:server=".$host.";Database=".$dbName, $dbUser, $dbPass);
$sql = "INSERT INTO Stage_Rebate_Master (MR_Name, MR_POC_N, MR_POC_E, MR_POC_P) VALUES ('$_POST[vendor_dropdown]','$_POST[name]','$_POST[email]','$_POST[number]')";
$sql1 = "INSERT INTO Stage_Rebate_Programs (Program_Year) VALUES ('$_POST[program_period_dropdown]')";
$sql2 = "INSERT INTO Stage_Rebate_Agreements (Terms, Tier, `Tier Minimum`, Multiplier, UOM, Retro, Guaranteed, Pay) VALUES ('$_POST[rows[0][0][term]]','$_POST[tier]','$_POST[rows[0][0][purchase_minimum]]','$_POST[rows[0][0][multiplier]]','$_POST[rows[0][0][uom]]','$_POST[rows[0][0][retro]]','$_POST[rows[0][0][guaranteed]]','$_POST[rows[0][0][paid]]')";
$sql3 = "INSERT INTO Stage_Rebate_Programs (Program_Name) VALUES ('$_POST[program_name]')";
$stmt = $pdo->prepare($sql);
$stmt1 = $pdo->prepare($sql1);
$stmt2 = $pdo->prepare($sql2);
$stmt3 = $pdo->prepare($sql3);
$result = $stmt->execute();
$result1 = $stmt1->execute();
$result2 = $stmt2->execute();
$result3 = $stmt3->execute();
echo json_encode($result);
echo json_encode($result1);
echo json_encode($result2);
echo json_encode($result3);
}
?>
remove the action=confirmation.php attribute in form ,as a result of which the bottom submit code is not working
<form name="form1" method="POST">
Thats enough

Multiple select checkbox to display the table record

I want to filter the table by using multiple select checkbox by selecting multiple Company Name and the table will display the record related to the selected checkbox. It only able to select one checkbox and display one related record only but it cannot multiple select checkbox.
<form name="frmSearch" id="frmSearch" method="post" action="">
<label>Company Name </label>
<select id="multiple-checkboxes" multiple="multiple" name="COMPANYNAME">
<?php
$query = mysqli_query($conn_connection, "SELECT * FROM sl_iv GROUP by COMPANYNAME");
while ($row = mysqli_fetch_assoc($query)) {
echo "<option value='".$row["COMPANYNAME"]."'".($row["COMPANYNAME"]==$_POST["COMPANYNAME"] ? " selected" : "").">".$row["COMPANYNAME"]."</option>";
}
?>
</select>
<br></br>
<button type="submit" id="submit" class="btn btn-info btn-sm"><span class="glyphicon glyphicon-search"></span> Search</button>
<a href="cust_due_list.php">
<button type="button" class="btn btn-success btn-sm"><span class="glyphicon glyphicon-refresh"></span> RESET</button>
</a>
<br></br>
<table id="table">
<center>
<thead>
<tr class="item-row">
<th width="15%" style="text-align:center"><span>Doc No.</span></th>
<th width="10%" style="text-align:center"><span>Due</span></th>
<th width="5%" style="text-align:center"><span>Age</span></th>
<th width="20%" style="text-align:center"><span>Customer Name</span></th>
<th width="10%" style="text-align:center"><span>Ammount</span></th>
<th width="10%" style="text-align:center"><span>Payment</span></th>
<th width="10%" style="text-align:center"><span>OutStanding</span></th>
</tr>
</thead>
</center>
<tbody>
<?php
if(isset ($_POST['COMPANYNAME']))
{
$COMPANYNAME = $_POST['COMPANYNAME'];
$fetch = "SELECT sl_iv.DOCDATE, ar_iv.DUEDATE, payment_terms.terms, sl_iv.DOCNO, sl_iv.COMPANYNAME, ar_iv.DOCAMT, ar_iv.PAYMENTAMT FROM `sl_iv` Inner Join `ar_iv` On ar_iv.DOCNO = sl_iv.DOCNO Inner Join `payment_terms` On ar_iv.TERMS = payment_terms.id WHERE sl_iv.COMPANYNAME = '".$COMPANYNAME."' or sl_iv.DOCDATE <= '".$from."'";
$result = mysqli_query($conn_connection,$fetch)or die("MySQL error: " . mysqli_error($conn_connection) . "<hr>\nQuery: $fetch");
}
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$docamt = $row['DOCAMT'];
?>
<tr class="item-row">
<td>
<input type="text" style="text-align:center; font-size:15px" class="form-control input-sm DocNo" id=DocNo0 " name="DocNo " value="<?php echo htmlspecialchars($row[ 'DOCNO']);?>" readonly></td>
<td>
<input type="text" style="text-align:center; font-size:15px" class="form-control input-sm DueDate" id="DueDate0" name="DueDate" value="<?php echo htmlspecialchars($row['DUEDATE']);?>" readonly>
</td>
<td>
<input type="text" style="text-align:center; font-size:15px" class="form-control input-sm DateAge" id="DateAge0" name="DateAge" value="<?php echo $dateage;?>" readonly>
</td>
<td>
<input type="text" style="text-align:center; font-size:15px" class="form-control input-sm CompanyName" id="CompanyName0" name="CompanyName" value="<?php echo htmlspecialchars($row['COMPANYNAME']);?>" readonly>
</td>
<td>
<input type="text" style="text-align:right; font-size:15px" class="form-control input-sm TotalAmt" id="TotalAmt0" name="TotalAmt" value="<?php echo htmlspecialchars($row['DOCAMT']);?>" readonly>
</td>
<td>
<input type="text" style="text-align:right; font-size:15px" class="form-control input-sm payment" id="payment0" name="payment" value="<?php echo htmlspecialchars($row['PAYMENTAMT']);?>" readonly>
</td>
<td>
<input type="text" style="text-align:right; font-size:15px" class="form-control input-sm Total_Outstanding" id="Total_Outstanding0" name="Total_Outstanding" value="<?php echo number_format((float)$outstanding, 2, '.', '');?>" readonly>
</td>
</tr>
<?php
}
} else {
echo "0 results";
}
?>
</tbody>
</table>
</form>
<script type="text/javascript">
$(document).ready(function() {
$('#multiple-checkboxes').multiselect();
});
</script>
When you receive multiple-select POST it cames as array, so you should use "IN" operator in your mysql request instead of comparison.
Try to change one part of your code:
if(isset ($_POST['COMPANYNAME'])) {
$COMPANYNAME = $_POST['COMPANYNAME'];
$COMPANYNAME = is_array($COMPANYNAME) ? $COMPANYNAME : [$COMPANYNAME];
$companiesParam = '\''. join("', '", $COMPANYNAME) . '\'';
$fetch = "SELECT sl_iv.DOCDATE, ar_iv.DUEDATE, payment_terms.terms, sl_iv.DOCNO, sl_iv.COMPANYNAME, ar_iv.DOCAMT, ar_iv.PAYMENTAMT FROM `sl_iv` Inner Join `ar_iv` On ar_iv.DOCNO = sl_iv.DOCNO Inner Join `payment_terms` On ar_iv.TERMS = payment_terms.id WHERE sl_iv.COMPANYNAME IN (".$companiesParam.") or sl_iv.DOCDATE <= '".$from."'";
$result = mysqli_query($conn_connection,$fetch)or die("MySQL error: " . mysqli_error($conn_connection) . "<hr>\nQuery: $fetch");
}

Posting dynamic inputs and inserting into MySQL database

This is my schedule.php page, an example of the user selecting the specific day for the time.
Once the user clicks on to add, the confirmation page will show up. But unfortunately i got these errors.
In mySQL database, the values that suppose to belong to "1700 - 1800" appeared on the "1400 - 1600" row.
This is my code for the schedule.php page,
<form name="Create New schedule" class="form-horizontal" method="post" action="handleSchedule.php">
<div class="form-group">
<div class="col-sm-4">
<label for="AcadInst">Academic Institution</label>
<select class="form-control" id="AcadInst" type="text" class="form-control" placeholder="Institution Name" name="academicInstitution">
<option>Institution Name</option>
<option>Singapore Polytechnic (SP)</option>
<option>Ngee Ann Polytechnic (NP)</option>
<option>Temasek Polytechnic (TP)</option>
<option>Republic Polytechnic (RP)</option>
<option>Nanyang Polytechnic (NYP)</option>
<option>Others(Please specify)</option>
</select>
</div>
<div class="col-sm-4">
<label>Level of Teaching</label>
<input list="LvTeaching" type="text" class="form-control" placeholder="Teaching Stage" name="levelofteaching">
<datalist id="LvTeaching">
<option value="Undergraduate Teaching">
<option value="Postgraduate Teaching">
<option value="Continuing Education">
<option value="Others (Please specify)">
</datalist>
</div>
<div class="col-sm-4">
<label>Type of Teaching</label>
<input list="TyTeaching" type="text" class="form-control" placeholder="Mode of Teaching" name="typeofteaching">
<datalist id="TyTeaching">
<option value="Clinical Teaching">
<option value="Academic Teaching">
<option value="Talk">
<option value="Others (Please specify)">
</datalist>
</div>
</div>
<div class="form-group">
<div class="col-sm-4">
<label for="startdate">Start Date</label>
<input type="date" class="form-control" placeholder="Select Date" name="startdate">
</div>
<div class="col-sm-4">
<label for="enddate">End Date</label>
<input type="date" class="form-control" placeholder="Select Date" name="enddate" id="noEndDate">
<input onclick="document.getElementById('noEndDate').disabled = true;" type="checkbox" name="type">No end date
</div>
</div>
<div class="form-group">
<div class="panel-body table-responsive">
<table class="table table-hover table-bordered">
<thead>
<tr>
<th>Start Time</th>
<th>End Time</th>
<th>Mon</th>
<th>Tue</th>
<th>Wed</th>
<th>Thu</th>
<th>Fri</th>
<th><input type="button" class="btn btn-warning" value="Add Timeslot" onClick="addRow('dataTable')" /></th>
</tr>
</thead>
<tbody id="dataTable">
<tr>
<td><input type="text" class="form-control" name="startTime[0]"></td>
<td><input type="text" class="form-control" name="endTime[0]"></td>
<td><input type="checkbox" name="Monday[0]" value="1"></td>
<td><input type="checkbox" name="Tuesday[0]" value="1"></td>
<td><input type="checkbox" name="Wednesday[0]" value="1"></td>
<td><input type="checkbox" name="Thursday[0]" value="1"></td>
<td><input type="checkbox" name="Friday[0]" value="1"></td>
<td><input type="button" class="btn btn-danger" value="Delete" onClick="deleteRow('dataTable')" /></td>
</tr>
</tbody>
</table>
<br/>
</div>
</div>
<div class="form-group">
<div class="col-sm-4">
<input type="submit" value="Add" class="btn btn-success">
</div>
</div>
</form>
My codes for handleSchedule.php,
<?php
$con = getDbConnect();
$academicInstitution = $_POST['academicInstitution'];
$levelofteaching = $_POST['levelofteaching'];
$typeofteaching = $_POST['typeofteaching'];
$startdate = $_POST['startdate'];
$enddate = $_POST['enddate'];
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else {
if ($_POST['startTime']) {
foreach ($_POST["startTime"] as $key => $value) {
$endTime = $_POST["endTime"][$key];
$monday = $_POST["Monday"][$key];
$tuesday = $_POST["Tuesday"][$key];
$wednesday = $_POST["Wednesday"][$key];
$thursday = $_POST["Thursday"][$key];
$friday = $_POST["Friday"][$key];
$sql = "INSERT INTO timetableschedule (academicInstitution, lvteaching, tyteaching, startdate, endate, startTime, endTime, Monday, Tuesday, Wednesday, Thursday, Friday) " .
"VALUES ('$academicInstitution', '$levelofteaching', '$typeofteaching', '$startdate', '$enddate', '$value', '$endTime', '$monday', '$tuesday', '$wednesday', '$thursday', '$friday')";
mysqli_query($con, $sql);
}
}
echo "1 record added";
mysqli_close($con);
}
?>
I am suspecting that the unchecked checkbox are causing the problem. Do anyone have any idea to solve it?
Unchecked checkboxes don't get submitted to the server. So $_POST['Monday'] is just an array of all the checked boxes. The indexes of these parameters won't match up with the text boxes.
The solution is to put explicit indexes in the names:
<tr>
<td><input type="text" class="form-control" name="startTime[0]"></td>
<td><input type="text" class="form-control" name="endTime[0]"></td>
<td><input type="checkbox" name="Monday[0]" value="1"></td>
<td><input type="checkbox" name="Tuesday[0]" value="1"></td>
<td><input type="checkbox" name="Wednesday[0]" value="1"></td>
<td><input type="checkbox" name="Thursday[0]" value="1"></td>
<td><input type="checkbox" name="Friday[0]" value="1"></td>
<td><input type="button" class="btn btn-danger" value="Delete" onClick="deleteRow('dataTable')" /></td>
</tr>
When you add a new row, you need to increment the index.
In the PHP, you then need to check whether the parameter is set.
$monday = isset($_POST["Monday"][$key]) ? 1 : 0;

Categories