jQuery(ajax) submit form - javascript

I am having some issues with $.ajax post method.
I would like my form to submit form data while on the current page, however for some odd reason my script is redirecting to the action page.
Please could someone identify what's causing this to happen and how I can resolve this problem?
Here is my script:
$(document).ready(function() {
$('#OverLaySetupScreen').submit(function(e) {
e.preventDefault();
var color1 = $('#color1').val();
var color2 = $('#color2').val();
var color3 = $('#color3').val();
var color4 = $('#color4').val();
var color5 = $('#color5').val();
var bigtext = $('#bigtext').val();
var bgcolor = $('#bgcolor').val();
var lowtextcolor = $('#lowertextcolor').val();
var lowertexttext = $('#lowertextext').val();
var bgimage = $('#bgimage').val();
var bgvideo = $('#bgvideo').val();
var toggle1 = $('#toggle1').val();
var toggle2 = $('#toggle2').val();
var toggle3 = $('#toggle3').val();
var toggle5 = $('#toggle5').val();
var toggle6 = $('#toggle6').val();
var toggle7 = $('#ltoggle7').val();
var discordname = $('#discordname').val();
var discordhash = $('#discordhash').val();
$.ajax({
url: 'actions/save.php',
type: 'POST',
data: {
color1: color1,
color2: color2,
color3: color3,
color4: color4,
color5: color5,
bigtext: bigtext,
bgcolor: bgcolor,
lowtextcolor: lowtextcolor,
lowertexttext: lowertexttext,
bgimage: bgimage,
bgvideo: bgvideo,
toggle1: toggle1,
toggle2: toggle2,
toggle3: toggle3,
toggle5: toggle5,
toggle6: toggle6,
toggle7: toggle7,
discordname: discordname,
discordhash: discordhash
}
})
.done(function(data) {
var result = $.trim(data);
alert(result);
})
.fail(function() {
alert('Failed to login...');
});
});
});
<form action="actions/save.php" method="post" id="OverLaySetupScreen" name="OverLaySetupScreen" pb-autologin="true" autocomplete="off" novalidate>
<table align="center" width="100%;" border="1">
<tbody>
<tr>
<td colspan="2">Event Toggle Control</td>
<td width="46%">
<div align="center">Preview</div>
<?php //<div align="right"><input type="button" value="Update Preview" onClick="preview()"></div>?>
</td>
<td width="5%">
<div align="center">Load Settings</div>
</td>
</tr>
<tr>
<td colspan="2">
<div align="left">
<font color="black">
<ul>
<li>
<input type="checkbox" <?php if($_GET[ "haloween"]=="false" ) { } else { echo "checked"; } ?> id="1toggle" name="1toggle" onChange="updatethis()"> Haloween [Preview]
</li>
<li>
<input type="checkbox" <?php if($_GET[ "xmas"]=="false" ) { } else { echo "checked"; } ?> id="2toggle" name="2toggle" onChange="updatethis()"> Christmas [Preview]</li>
<li>
<input type="checkbox" <?php if($_GET[ "newyear"]=="false" ) { } else { echo "checked"; } ?> id="3toggle" name="3toggle" onChange="updatethis()"> New Years [Preview]
</li>
</ul>
US Based Events
<ul>
<li>
<input type="checkbox" <?php if($_GET[ "4july"]=="true" ) { echo "checked"; } else { } ?> id="5toggle" name="5toggle" onChange="updatethis()"> 4th July [Preview]
</li>
</ul>
Jewish Faith Based Events
<ul>
<li>
<input type="checkbox" <?php if($_GET[ "confetti"]=="true" ) { echo "checked"; } else { } ?> id="6toggle" name="6toggle" onChange="updatethis()"> Rosh Hashanah [
Preview]</li>
</ul>Charity Events
<ul>
<li><input type="checkbox" <?php if($_GET[ "extralife"]=="true" ) { echo "checked"; } else { } ?> id="7toggle" name="7toggle" onChange="updatethis()"> Extra Life [Preview]</li>
</ul>
</font>
</div>
</td>
<td colspan="2" rowspan="9" valign="top"><iframe id="previewframe" src="<?php if($_GET[" importSettings "]) { echo $protocol . $_SERVER["SERVER_NAME "] . "https://crossfire151.gq/twitch/chottis/?chottis=false&preview=true&text=Loading&text2=Importing%20Settings "; } else { ?><?php echo $protocol . $_SERVER["SERVER_NAME "]; ?>/twitch/chottis/preview/<?php if($_GET["chottis "] == "true ") { echo "? "; } else { echo "?chottis=false& "; } ?>preview=true<?php } ?>" frameborder="0" scrolling="no" style="height: 250px; width: 100%;"></iframe></td>
</tr>
<tr>
<td colspan="2">Big Text Customization</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td width="15%">
<div align="center">Color 1</div>
</td>
<td width="34%">
<div align="center"><input class="input" onchange="preview()" type="color" value="#<?php if($_GET[" c1 "]) { echo $_GET["c1 "]; } else { echo "7E147C "; } ?>" id="color1" name="color1"></div>
</td>
</tr>
<tr>
<td>
<div align="center">Color 2</div>
</td>
<td>
<div align="center"><input class="input" onchange="preview()" type="color" value="#<?php if($_GET[" c2 "]) { echo $_GET["c2 "]; } else { echo "7C2B88 "; } ?>" id="color2" name="color2"></div>
</td>
</tr>
<tr>
<td>
<div align="center">Color 3</div>
</td>
<td>
<div align="center"><input class="input" onchange="preview()" type="color" value="#<?php if($_GET[" c3 "]) { echo $_GET["c3 "]; } else { echo "AC44AE "; } ?>" id="color3" name="color3"></div>
</td>
</tr>
<tr>
<td>
<div align="center">Color 4</div>
</td>
<td>
<div align="center"><input class="input" onchange="preview()" type="color" value="#<?php if($_GET[" c4 "]) { echo $_GET["c4 "]; } else { echo "9633A4 "; } ?>" id="color4" name="color4"></div>
</td>
</tr>
<tr>
<td>
<div align="center">Color 5</div>
</td>
<td>
<div align="center"><input class="input" onchange="preview()" type="color" value="#<?php if($_GET[" c5 "]) { echo $_GET["c5 "]; } else { echo "B13DC2 "; } ?>" id="color5" name="color5"></div>
</td>
</tr>
<tr>
<td>
<div align="center">Text</div>
</td>
<td>
<div align="center"><input class="input" onchange="preview()" type="text" value="<?php if($_GET[" text "]) { echo $_GET["text "]; } else { echo "be right back "; } ?>" id="bigtext" name="bigtext"></div>
</td>
</tr>
<tr>
<td colspan="4">Lower text customization</td>
</tr>
<tr>
<td>
<div align="center">Text</div>
</td>
<td><input class="input" onchange="preview()" type="text" value="<?php if($_GET[" text2 "]) { echo $_GET["text2 "]; } else { echo "go treat yourselves! "; } ?>" id="lowertexttext" name="lowertexttext"></td>
<td colspan="2">Click here to set this text to blank - This will remove the lower text completely</td>
</tr>
<tr class="ltccollumn">
<td>
<div align="center">Color</div>
</td>
<td>
<div align="center"><input class="input" onchange="preview()" type="color" value="#<?php if($_GET[" lowertextcolor "]) { echo $_GET["lowertextcolor "]; } else { echo "702E6D "; } ?>" id="lowertextcolor" name="lowertextcolor"></div>
</td>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="4">Background Customization</td>
</tr>
<tr>
<td>
<div align="center">Custom Background</div>
</td>
<td>
<div align="center">Setup...</div>
</td>
<td colspan="2"> </td>
</tr>
<tr>
<td>
<div align="center">Background Color</div>
</td>
<td>
<div align="center"><input class="input" onchange="preview()" type="color" value="#<?php if($_GET[" bgcolor "]) { echo $_GET["bgcolor "]; } else { echo "37133C "; } ?>" id="bgcolor" name="bgcolor"></div>
</td>
<td colspan="2">
<div id="copystate"></div>
</td>
</tr>
<tr>
<td>
<div align="center"></div>
</td>
<td colspan="3">
<div align="center">
<?php if(isset($_SESSION["brb-authenticated"])) { ?>
<input class="input" onchange="preview()" type="text" id="sourceurl" value="<?php if($_GET[" importSettings "]) { } else { ?><?php echo $protocol . $_SERVER["SERVER_NAME "]; ?>/twitch/chottis/preview/<?php if($_GET["chottis "] == "true ") { echo "? "; } else { echo "?chottis=false& "; }?>c1=7e147c&c2=7c2b88&c3=ac44ae&c4=9633a4&c5=b13dc2&text2=go treat yourselves&text=be right back&chromakey=37133c&text2c=702e6d&bgimg=&bgvideo=&haloween=true&xmas=true&newyear=true&4july=false&confetti=false&extralife=false <?php } ?>" onClick="this.select(),copythis(),document.execCommand('copy');" readonly>
<?php } elseif(!isset($_SESSION["brb-authenticated"])) { ?>
Finish Setup <input type="submit" value="submit"></div>
<?php } ?>
</div>
</td>
</tr>
</tbody>
</table>
<div align="center"><input type="reset" onClick="setTimeout('preview()', 500)" value="Reset to default Settings"></div>
<div class="modal" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog animated zoomIn animated-3x" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true"><i class="zmdi zmdi-close"></i></span></button>
<h3 class="modal-title" id="myModalLabel">Custom Background</h3>
</div>
<div class="modal-body">
<center>
<p>
<label>
<input type="radio" name="RadioGroup1" value="radio" id="RadioGroup1_0" <?php if($_GET["bgvideo"]) {} else { echo "checked"; }?> onChange="radioupdate()">
Image</label>
<label>
<input type="radio" name="RadioGroup1" value="radio" id="RadioGroup1_1" <?php if($_GET["bgvideo"]) { echo "checked"; } else {} ?> onChange="radioupdate()">
Video</label>
<br>
</p>
<p id="type1">
<?php if($_GET["bgvideo"]) { ?>Please enter an video location(URL) below.<br/>This video can be from any source but hotlinking must be enabled.<br/>Dropbox is supported, make sure you set "dl=1"<br/>Supported format: MP4.
<?php } else { ?>Please enter an image location(URL) below.<br/>This image can be from any source but hotlinking must be enabled.<br/>Dropbox is supported, make sure you set "dl=1"<br/>Supported formats: jpg,jpeg,png,bmp,gif.
<?php } ?>
</p>
<p>
<div id="type2">
<?php if($_GET["bgvideo"]) { ?><input type="hidden" id="bgimage"><input type="text" id="bgvideo" name="bgvideo" <?php if($_GET[ "bgvideo"]) { echo 'value="' . $_GET[ "bgvideo"] . '" '; } ?>style="width: 60%">
<?php } else { ?><input type="hidden" id="bgvideo"><input type="text" id="bgimage" name="bgimage" <?php if($_GET[ "bgimage"]) { echo 'value="' . $_GET[ "bgimage"] . '" '; } ?>style="width: 60%">
<?php } ?>
</div>
<span id="type3"><?php if($_GET["bgvideo"]) { ?><br/><div align="left" style="color: red"><b>Warning: If you use a background video, the Haloween event will NOT show it's background, meaning this video will over-ride the events background.</b></div><?php } else { ?>Transparent Image<br/>Your image must be at least <b>1920x1080</b> for it to fit within the overlay.<br/><div align="left" style="color: red"><b>Warning: If you use a background image, the Haloween event will NOT show it's background, meaning this image will over-ride the events background.</b></div><?php } ?></span>
</p>
</center>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" onClick="preview()" class="btn btn-primary" data-dismiss="modal">Apply</button>
</div>
</div>
</div>
</div>
<div class="modal" id="Authentication" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog animated zoomIn animated-3x" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close" onClick="AuthCancel()"><span aria-hidden="true"><i class="zmdi zmdi-close"></i></span></button>
<h3 class="modal-title" id="myModalLabel">Authentication</h3>
</div>
<div class="modal-body">
<center>
<p>
<?php //<table border="1" id="disclogin" style="display: none;">
//<tbody>
//<tr>
//<td><div align="right">Discord name:</div></td>
//<td><input type="text" name="discordname" required></td>
//<td><input placeholder="#1245" required name="discordhash" required></td>
//</tr>
//<tr>
//<td colspan="3"><button type="submit" style="border: hidden;" class="btn btn-block btn-raised btn-success" id="sub1">Save & Get my URL</button></td>
//</tr>
//<tr>
//<td colspan="3"></td>
// </tr>
//</tbody>
//</table> ?>
<div id="discordbegin" style="display: none;">
To begin you must first join our Discord Server!
<table border="0" align="center">
<tbody>
<tr>
<td>Join Discord</td>
</tr>
</tbody>
</table>
<table border="0" align="center">
<tbody>
<tr>
<td>I already Joined!</td>
</tr>
</tbody>
</table>
</div>
<table border="1" id="adddiscord" style="display: none;">
<tbody>
<tr>
<td>
<div align="right">Discord name:</div>
</td>
<td><input type="text" name="discordname" id="discordname" required></td>
<td><input type="text" placeholder="#1245" name="discordhash" id="discordhash" required></td>
</tr>
<tr>
<td colspan="3"><button type="submit" style="border: hidden;" class="btn btn-block btn-raised btn-success" id="regthat">Whitelist my Discord name</button></td>
</tr>
</tbody>
</table>
<?php //<form>
//<table border="1" id="newname" style="display: none;">
//<tbody>
//<tr>
//<td><div align="right">Old Discord name:</div></td>
//<td><input type="text" name="olddiscordname" required></td>
//<td><input placeholder="#1245" required name="discordhash" required></td>
//</tr>
//<tr>
//<td><div align="right">New Discord name:</div></td>
//<td><input type="text" name="newdiscordname" required></td>
//<td><input placeholder="#1245" required name="newdiscordhash" required></td>
//</tr>
//<tr>
//<td colspan="3"><button type="submit" style="border: hidden;" class="btn btn-block btn-raised btn-primary" id="updatethat">Save new my Discord name</button></td>
//</tr>
//<tr>
//<td colspan="3"></td>
//</tr>
//</tbody>
//</table></form> ?>
<?php } ?>
</p>
</center>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" onClick="AuthCancel()">Cancel</button>
</div>
</div>
</div>
</div>
</form>
Please note that this form contains PHP functionality which will be used to get saved data at a later date, once I can get this script working as it should.
For best results you can see a live testing page over here:
Testing page to this script

This resolved my issue:
$(document).ready(function() {
$('#overlaysetupscreen').submit(function(e){
e.preventDefault();
var color1 = $('#color1').val();
var color2 = $('#color2').val();
var color3 = $('#color3').val();
var color4 = $('#color4').val();
var color5 = $('#color5').val();
var bigtext = $('#bigtext').val();
var bgcolor = $('#bgcolor').val();
var lowtextcolor = $('#lowertextcolor').val();
var lowertexttext = $('#lowertextext').val();
var bgimage = $('#bgimage').val();
var bgvideo = $('#bgvideo').val();
var toggle1 = $('#toggle1').val();
var toggle2 = $('#toggle2').val();
var toggle3 = $('#toggle3').val();
var toggle5 = $('#toggle5').val();
var toggle6 = $('#toggle6').val();
var toggle7 = $('#ltoggle7').val();
var discordname = $('#discordname').val();
var discordhash = $('#discordhash').val();
$.ajax({
url: 'actions/save.php',
type: 'POST',
data: {
color1: color1,
color2: color2,
color3: color3,
color4: color4,
color5: color5,
bigtext: bigtext,
bgcolor: bgcolor,
lowtextcolor: lowtextcolor,
lowertexttext: lowertexttext,
bgimage: bgimage,
bgvideo: bgvideo,
toggle1: toggle1,
toggle2: toggle2,
toggle3: toggle3,
toggle5: toggle5,
toggle6: toggle6,
toggle7: toggle7,
discordname: discordname,
discordhash: discordhash
}
})
.done(function(data){
var result = $.trim(data);
alert("Success");
})
.fail(function(){
alert('Failed');
});
});
});
I simply counted each of the opening and closing tags and realized a slight problem with the end tags.
So I added }); }); onto the end, that resolved the issue.

Related

OnChange Not Working in cloned Input fields

I have created a form and in the form I have given room to clone input fields ie add or delete rows.
In one input field I am using Jquery Ajax to get the value.
Unfortunately after adding an additional role ie cloning the parent row to get a child row, ajax does not pass the value for the input field expecting it value from the Ajax.
I have attached the code below to explain better
function restrictAlphabets(e) {
var x = e.which || e.keyCode;
if ((x >= 48 && x <= 57))
return true;
else
return false;
}
// Below Clones Table
document.querySelector('button[data-name="add"]').addEventListener('click', e => {
let tbody = e.target.closest('table').querySelector('tbody');
let clone = tbody.firstElementChild.cloneNode(true);
clone.querySelector('button[data-name="del"]').hidden = false;
clone.querySelectorAll('input, select').forEach(n => {
n.value = '';
});
tbody.appendChild(clone);
});
document.querySelector('form table#dyntbl').addEventListener('click', e => {
e.stopPropagation();
if (e.target != e.currentTarget && (e.target.dataset.name == 'del' || e.target.parentNode.dataset.name == 'del')) {
let tbody = e.target.closest('table').querySelector('tbody');
if (tbody.childNodes.length > 3) tbody.removeChild(e.target.closest('tr'))
}
});
// Below begins AJAX Function
function checkDeviceStatus() {
var dModel = $("#model").val();
var dBrand = $("#brand").val();
var dserial = $("#serialNo").val();
var client = $("#client").val();
$.ajax({
url: "./handlers/slaChecker.php",
type: "POST",
data: {
dModel: dModel,
dserial: dserial,
client: client,
dBrand: dBrand,
},
success: function(result) {
$("#deviceLevel").val(result);
console.log(result);
}
})
}
<div class="col-xl-8 col-md-12">
<form>
<div class="card">
<div class="card-header">
<h3 class="card-title"><strong class="text-success" style="cursor: pointer;"><?=ucwords($clientName)?></strong> REP'S INFORMATION</h3>
</div>
<div class="card-body">
<div class="row">
<div class="col-sm-4 col-md-4">
<div class="form-group">
<label class="form-label">Reps Name<span class="text-red">*</span></label>
<input type="text" name="" class="form-control" required="">
</div>
</div>
<div class="col-sm-4 col-md-4">
<div class="form-group">
<label class="form-label">E-Mail<span class="text-red">*</span></label>
<input type="email" name="" class="form-control" required="">
</div>
</div>
<div class="col-sm-4 col-md-4">
<div class="form-group">
<label class="form-label">Phone No.<span class="text-red">*</span></label>
<input type="text text-dark" class="form-control" name="client_contact2" required="" id="client_contact2" onkeypress="return restrictAlphabets(event)" onpaste="return false;" ondrop="return false;" autocomplete="off" required="">
<input type="date" name="" value="<?=date(" Y-m-d ")?>" hidden="">
</div>
</div>
</div>
</div>
</div>
<div class="card">
<div class="card-header">
<h3 class="card-title">ADD DEVICE(S) INFORMATION</h3>
</div>
<div class="card-body">
<?php
if ($clientType === $slaClient) {
?>
<table id='dyntbl' class='table border text-nowrap text-md-nowrap table-striped mb-0'>
<thead>
<tr>
<th class="text-center">Device Brand</th>
<th class="text-center">Device Model</th>
<th class="text-center">Serial Number</th>
<th class="text-center" style="width:10%">SLA Device</th>
<th><button type="button" class=" btn text-success" data-name='add'><i class="fe fe-plus-circle" data-id='ad' style="font-size:1.6em;"></i></button></th>
</tr>
</thead>
<tbody class="field_wrapper " id="table_body">
<tr>
<td>
<select class="form-control form-select " data-bs-placeholder="Select" name="brand[]" required="" id="brand" onchange="checkDeviceStatus()">
<?php
$readALL = "SELECT * FROM productbrands WHERE deleted = 0";
$displayAll = mysqli_query($conn,$readALL);
while($rowFetchAll = mysqli_fetch_array($displayAll)){
$brandName = $rowFetchAll['brandName'];
$brandid = $rowFetchAll['brandID'];
?>
<option value="<?=$brandid?>">
<?=$brandName?>
</option>
<?php } ?>
</select>
</td>
<td>
<select class="form-control form-select " data-bs-placeholder="Select" name="model[]" required="" id="model" onchange="checkDeviceStatus()">
<?php
$readALL1 = "SELECT * FROM productmodels WHERE deleted = 0";
$displayAll1 = mysqli_query($conn,$readALL1);
while($rowFetchAll1 = mysqli_fetch_array($displayAll1)){
$modelName = $rowFetchAll1['modelName'];
$modelid = $rowFetchAll1['modelID'];
?>
<option value="<?=$modelid?>">
<?=$modelName?>
</option>
<?php } ?>
</select>
</td>
<td><input type="text" name="serialNo" class="form-control" id="serialNo" onchange="checkDeviceStatus()"></td>
<!-- The input field below is to get value from AJAX -->
<td><input type="text" name="deviceLevel" class="form-control" readonly="" id="deviceLevel">
<!-- End of Input field -->
</td>
<input type="text" name="" id="client" value="<?=$clientID?>" hidden="">
<td><button type="button" class=" btn text-danger" data-name="del"><i class="fe fe-minus-circle" style="font-size:1.6em;"></i></button></td>
</tr>
</tbody>
</table>
<?php } ?>
<?php
if ($clientType === $nonSla) {
?>
<table id='dyntbl' class='table border text-nowrap text-md-nowrap table-striped mb-0'>
<thead>
<tr>
<th>Product Model Non-SLA</th>
<th>Serial Number Non-SLA</th>
<th>Device Status Non-SLA</th>
<th><button type="button" class=" btn text-success" data-name='add'><i class="fe fe-plus-circle" data-id='ad' style="font-size:1.6em;"></i></button></th>
</tr>
</thead>
<tbody class="field_wrapper " id="table_body">
<tr>
<td><input type="text" name="" class="form-control"></td>
<td><input type="text" name="" class="form-control"></td>
<td><input type="text" name="" class="form-control"></td>
<td><button type="button" class=" btn text-danger" data-name="del"><i class="fe fe-minus-circle" style="font-size:1.6em;"></i></button></td>
</tr>
</tbody>
</table>
<?php } ?>
</div>
</div>
</form>
</div>
The main problem is that when you clone, you will get duplicated id's. An Id must always be unique.
I would suggest that you change the ID to class or something else and do something like this.
function checkDeviceStatus(obj) {
var dModel = $(obj).closest("tr").find(".model").val();
var dBrand = $(obj).closest("tr").find(".brand").val();
var dserial = $(obj).closest("tr").find(".serialNo").val();
var client = $(obj).closest("tr").find(".client").val();
console.log("dserial:"+dserial);
//Removed ajax for demo.
}
And then add this to your onchange="checkDeviceStatus()" like onchange="checkDeviceStatus(this)"
Demo
function restrictAlphabets(e) {
var x = e.which || e.keyCode;
if ((x >= 48 && x <= 57))
return true;
else
return false;
}
// Below Clones Table
document.querySelector('button[data-name="add"]').addEventListener('click', e => {
let tbody = e.target.closest('table').querySelector('tbody');
let clone = tbody.firstElementChild.cloneNode(true);
clone.querySelector('button[data-name="del"]').hidden = false;
clone.querySelectorAll('input, select').forEach(n => {
n.value = '';
});
tbody.appendChild(clone);
});
document.querySelector('form table#dyntbl').addEventListener('click', e => {
e.stopPropagation();
if (e.target != e.currentTarget && (e.target.dataset.name == 'del' || e.target.parentNode.dataset.name == 'del')) {
let tbody = e.target.closest('table').querySelector('tbody');
if (tbody.childNodes.length > 3) tbody.removeChild(e.target.closest('tr'))
}
});
// Below begins AJAX Function
function checkDeviceStatus(obj) {
var dModel = $(obj).closest("tr").find(".model").val();
var dBrand = $(obj).closest("tr").find(".brand").val();
var dserial = $(obj).closest("tr").find(".serialNo").val();
var client = $(obj).closest("tr").find(".client").val();
console.log("dserial:"+dserial);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-xl-8 col-md-12">
<form>
<div class="card">
<div class="card-header">
<h3 class="card-title"><strong class="text-success" style="cursor: pointer;"><?=ucwords($clientName)?></strong> REP'S INFORMATION</h3>
</div>
<div class="card-body">
<div class="row">
<div class="col-sm-4 col-md-4">
<div class="form-group">
<label class="form-label">Reps Name<span class="text-red">*</span></label>
<input type="text" name="" class="form-control" required="">
</div>
</div>
<div class="col-sm-4 col-md-4">
<div class="form-group">
<label class="form-label">E-Mail<span class="text-red">*</span></label>
<input type="email" name="" class="form-control" required="">
</div>
</div>
<div class="col-sm-4 col-md-4">
<div class="form-group">
<label class="form-label">Phone No.<span class="text-red">*</span></label>
<input type="text text-dark" class="form-control" name="client_contact2" required="" id="client_contact2" onkeypress="return restrictAlphabets(event)" onpaste="return false;" ondrop="return false;" autocomplete="off" required="">
<input type="date" name="" value="<?=date(' Y-m-d ')?>" hidden="">
</div>
</div>
</div>
</div>
</div>
<div class="card">
<div class="card-header">
<h3 class="card-title">ADD DEVICE(S) INFORMATION</h3>
</div>
<div class="card-body">
<?php
if ($clientType === $slaClient) {
?>
<table id='dyntbl' class='table border text-nowrap text-md-nowrap table-striped mb-0'>
<thead>
<tr>
<th class="text-center">Device Brand</th>
<th class="text-center">Device Model</th>
<th class="text-center">Serial Number</th>
<th class="text-center" style="width:10%">SLA Device</th>
<th><button type="button" class=" btn text-success" data-name='add'><i class="fe fe-plus-circle" data-id='ad' style="font-size:1.6em;">ADD</i></button></th>
</tr>
</thead>
<tbody class="field_wrapper " id="table_body">
<tr>
<td>
<select class="form-control form-select brand" data-bs-placeholder="Select" name="brand[]" required="" id="" onchange="checkDeviceStatus(this)">
<?php
$readALL = "SELECT * FROM productbrands WHERE deleted = 0";
$displayAll = mysqli_query($conn,$readALL);
while($rowFetchAll = mysqli_fetch_array($displayAll)){
$brandName = $rowFetchAll['brandName'];
$brandid = $rowFetchAll['brandID'];
?>
<option value="<?=$brandid?>">
<?=$brandName?>
</option>
<?php } ?>
</select>
</td>
<td>
<select class="form-control form-select model" data-bs-placeholder="Select" name="model[]" required="" id="" onchange="checkDeviceStatus(this)">
<?php
$readALL1 = "SELECT * FROM productmodels WHERE deleted = 0";
$displayAll1 = mysqli_query($conn,$readALL1);
while($rowFetchAll1 = mysqli_fetch_array($displayAll1)){
$modelName = $rowFetchAll1['modelName'];
$modelid = $rowFetchAll1['modelID'];
?>
<option value="<?=$modelid?>">
<?=$modelName?>
</option>
<?php } ?>
</select>
</td>
<td><input type="text" name="serialNo" class="form-control serialNo" onchange="checkDeviceStatus(this)"></td>
<!-- The input field below is to get value from AJAX -->
<td><input type="text" name="deviceLevel" class="form-control" readonly="" id="deviceLevel">
<!-- End of Input field -->
</td>
<input type="text" name="" class="client" value="<?=$clientID?>" hidden="">
<td><button type="button" class=" btn text-danger" data-name="del"><i class="fe fe-minus-circle" style="font-size:1.6em;"></i></button></td>
</tr>
</tbody>
</table>
<?php } ?>
<?php
if ($clientType === $nonSla) {
?>
<table id='dyntbl' class='table border text-nowrap text-md-nowrap table-striped mb-0'>
<thead>
<tr>
<th>Product Model Non-SLA</th>
<th>Serial Number Non-SLA</th>
<th>Device Status Non-SLA</th>
<th><button type="button" class=" btn text-success" data-name='add'><i class="fe fe-plus-circle" data-id='ad' style="font-size:1.6em;"></i></button></th>
</tr>
</thead>
<tbody class="field_wrapper " id="table_body">
<tr>
<td><input type="text" name="" class="form-control"></td>
<td><input type="text" name="" class="form-control"></td>
<td><input type="text" name="" class="form-control"></td>
<td><button type="button" class=" btn text-danger" data-name="del"><i class="fe fe-minus-circle" style="font-size:1.6em;"></i></button></td>
</tr>
</tbody>
</table>
<?php } ?>
</div>
</div>
</form>
</div>
I have found solution to it.
The classes as you all suggested wasn't Ideal instead this is what I did.
the ids has to be unique so I created a variable for it to be handling the additional ids.
This worked perfectly for me.
Php is now working fine and Ajax is also working fine.
$(document).ready(function() {
var num = 1;
var c = `
<tr id="row_num" >
<td>
<select class="form-control form-select brand" data-bs-placeholder="Select" name="brand[]" required="" id="brand_num" onchange="checkDeviceStatus(_num)">
<?php
$readALL = "SELECT * FROM productbrands WHERE deleted = 0";
$displayAll = mysqli_query($conn,$readALL);
while($rowFetchAll = mysqli_fetch_array($displayAll)){
$brandName = $rowFetchAll['brandName'];
$brandid = $rowFetchAll['brandID'];
?>
<option value="<?=$brandid?>"><?=$brandName?></option>
<?php } ?>
</select>
</td>
<td>
<select class="form-control form-select model" data-bs-placeholder="Select" name="model[]" required="" id="model_num" onchange="checkDeviceStatus(_num)">
<?php
$readALL1 = "SELECT * FROM productmodels WHERE deleted = 0";
$displayAll1 = mysqli_query($conn,$readALL1);
while($rowFetchAll1 = mysqli_fetch_array($displayAll1)){
$modelName = $rowFetchAll1['modelName'];
$modelid = $rowFetchAll1['modelID'];
?>
<option value="<?=$modelid?>"><?=$modelName?></option>
<?php } ?>
</select>
</td>
<td><input type="text" name="serialNo" class="form-control serialNo" id="serialNo_num" onkeyup="checkDeviceStatus(_num)"></td>
<!-- The input field below is to get value from AJAX -->
<td>
<input type="text" name="deviceLevel" class="form-control " readonly="" id="deviceLevel_num">
<!-- End of Input field -->
</td>
<td><button type="button" onclick="DeleteRow(_num)" class=" btn text-danger remove" data-name="del"><i class="fe fe-minus-circle" style="font-size:1.6em;"></i></button></td>
</tr>
`;
$(".addRow").click(function(e) {
var cc = c;
// e.preventDefault();
cc = cc.replace('_num', num);
cc = cc.replace('_num', num);
cc = cc.replace('_num', num);
cc = cc.replace('_num', num);
cc = cc.replace('_num', num);
cc = cc.replace('_num', num);
cc = cc.replace('_num', num);
cc = cc.replace('_num', num);
cc = cc.replace('_num', num);
$(".table_body").append(cc);
num++;
console.log(num);
});
});
<tbody class="field_wrapper showRow table_body" id="table_body">
<tr id="row0">
<td>
<select class="form-control form-select" data-bs-placeholder="Select" name="brand[]" required="" id="brand0" onchange="checkDeviceStatus(0)">
<?php
$readALL = "SELECT * FROM productbrands WHERE deleted = 0";
$displayAll = mysqli_query($conn,$readALL);
while($rowFetchAll = mysqli_fetch_array($displayAll)){
$brandName = $rowFetchAll['brandName'];
$brandid = $rowFetchAll['brandID'];
?>
<option value="<?=$brandid?>">
<?=$brandName?>
</option>
<?php } ?>
</select>
</td>
<td>
<select class="form-control form-select " data-bs-placeholder="Select" name="model[]" required="" id="model0" onchange="checkDeviceStatus(0)">
<?php
$readALL1 = "SELECT * FROM productmodels WHERE deleted = 0";
$displayAll1 = mysqli_query($conn,$readALL1);
while($rowFetchAll1 = mysqli_fetch_array($displayAll1)){
$modelName = $rowFetchAll1['modelName'];
$modelid = $rowFetchAll1['modelID'];
?>
<option value="<?=$modelid?>">
<?=$modelName?>
</option>
<?php } ?>
</select>
</td>
<td><input type="text" name="serialNo" class="form-control " id="serialNo0" onkeyup="checkDeviceStatus(0)"></td>
<!-- The input field below is to get value from AJAX -->
<td><input type="text" name="deviceLevel" class="form-control " readonly="" id="deviceLevel0">
<!-- End of Input field -->
</td>
<input type="text" name="" id="client" value="<?=$clientID?>" hidden="">
<td><button type="button" class=" btn text-success addRow" data-name='add'><i class="fe fe-plus-circle " data-id='ad' style="font-size:1.6em;"></i></button></td>
</tr>
</tbody>

Dynamic Loading of Data Via Ajax

I am new to AJAX and I am trying to load some data when clicked on a button to a modal, But the expected result is not being rendered and there are no errors thrown in the Developers Console.
The code AJAX code is as follows,
<script>
$(document).ready(function()
{
$(document).on('click','.edit_data', function(){
var employee_id = $(this).attr("User_ID");
$.ajax({
url:"fetchuser.php",
method:"POST",
data:{employee_id:employee_id},
dataType:"json",
success:function(data){
$('#floatingInputname').val(data.floatingInputname);
$('#add_data_Modal').modal('show');
}
});
});
});
</script>
And the fetchuser.php code is as follows,
<?php
include 'config.php';
if (isset($_POST['employee_id'])) {
$query = "SELECT * FROM gusers WHERE User_ID = '".$_POST['employee_id']."'";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_array($result);
echo json_encode($row);
}
?>
The HTML table structure where I have a button to be clicked for the modal with the data to load is as follows,
<table class="table" id="myTable" style="width: 100%;">
<thead>
<th scope="col">Image</th>
<th scope="col">Name</th>
<th scope="col">NIC</th>
<th scope="col">Contact</th>
<th scope="col">Role</th>
<th scope="col"></th>
</thead>
<tbody>
<?php
$queryguser = "SELECT * FROM `gusers`";
$gusersresult = mysqli_query($conn,$queryguser);
if ($gusersresult->num_rows>0) {
while ($gusersrow = mysqli_fetch_assoc($gusersresult)) {
?>
<tr>
<td scope="row">
<?php
if (empty($gusersrow['Image'])|| $gusersrow['Image']==null) {
?>
<img src="img/question (1).png" alt="No Profile" style="width:75px; height:75px; border-
radius:20px;">
<?php
}
else{
echo '<img src="data:image;base64,'.base64_encode($gusersrow['Image']).'" alt="Profile Image"
style="width:75px; height:75px; border-radius:50%;">';
}
?>
</td>
<td contenteditable="true" data-old_value="<?php $gusersrow['User_Name']; ?>"
onBlur="inlineedit(this,'User_Name','<?php echo $gusersrow["User_ID"]; ?>')"
onClick="highlightEdit(this);">
<?php echo $gusersrow["User_Name"]; ?>
</td>
<td contenteditable="true" data-old_value="<?php $gusersrow['User_NIC']; ?>"
onBlur="inlineedit(this,'User_NIC','<?php echo $gusersrow["User_ID"]; ?>')"
onClick="highlightEdit(this);">
<?php echo $gusersrow["User_NIC"]; ?>
</td>
<td contenteditable="true" data-old_value="<?php $gusersrow['User_Contact']; ?>"
onBlur="inlineedit(this,'User_Contact','<?php echo $gusersrow["User_ID"]; ?>')"
onClick="highlightEdit(this);">
<?php echo $gusersrow["User_Contact"]; ?>
</td>
<td contenteditable="true" data-old_value="<?php $gusersrow['Role']; ?>"
onBlur="inlineedit(this,'Role','<?php echo $gusersrow["User_ID"]; ?>')"
onClick="highlightEdit(this);">
<?php echo $gusersrow["Role"]; ?>
</td>
<td scope="row" class="text-center"><input type="button" name="edit" value="Edit" id="<?php echo
$gusersrow["User_ID"]; ?>" class="btn btn-info btn-xs edit_data" /></td>
</tr>
<?php
}
}else{
echo "No Records Found";
}
?>
</tbody>
</table>
And the Modal i want the data to be displayed
<div class="modal fade" id="exampleModaluserupdate" tabindex="-1" aria-labelledby="exampleModalLabel"
aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Update User Data</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form action="" method="post">
<div class="row" style="width:100%;">
<div class="col-md-6">
<div class="form-floating mb-3">
<input type="text" class="form-control" id="floatingInputname">
<label for="floatingInputname">Name</label>
</div>
</div>
<div class="col-md-6">
<div class="form-floating mb-3">
<input type="text" class="form-control" id="floatingInputnic">
<label for="floatingInputnic">NIC</label>
</div>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
Further to this i have the following JS scripts loaded in my <head> tag
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>

Column cannot be null by using Auth

I'm inserting multiple data into the database. There is an error for my column report_by which is SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'report_by' cannot be null (SQL: insert into 'complaints' ('defect_id', 'image', 'description', 'report_by') values (1, C:\wamp64\tmp\php8E2E.tmp, leak, ?)) Inside column report_by should be the id of the user by using Auth. It's to tell that the complaint made by which users.
users table
id
role
email
typable_id
typable_type
complaints table
id
defect_id
image
description
report_by
ComplaintController.php
<?php
namespace App\Http\Controllers;
use Auth;
use App\Complaint;
use Illuminate\Http\Request;
class ComplaintController extends Controller
{
public function index()
{
return view('buyers.complaint');
}
public function create(Request $request)
{
if (count($request->defect_id) > 0) {
foreach($request->defect_id as $item=>$v) {
$data = array(
'defect_id' => $request->defect_id[$item],
'image' => $request->image[$item],
'description' => $request->description[$item],
'report_by' => $request->user_id,
);
Complaint::insert($data);
}
}
return redirect('/report-form')->with('success','Your report is submitted!');
}
}
complaint.blade.php
<div class="panel-heading">
<h3 class="panel-title"><strong>Make New Report</strong></h3>
</div>
<div class="panel-body">
<div>
<div class="panel">
<table class="table table-bordered">
<thead>
<tr>
<th><center>Type of Defect</center></th>
<th><center>Image</center></th>
<th><center>Description</center></th>
<th><center>Action</center></th>
</tr>
</thead>
<tbody>
<tr>
<td width="20%">
<form action="/report-create" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<input type="text" class="hidden" id="user_id" value="{{Auth::user()->id}}">
<select class="form-control" name="defect_id[]">
<option value="" selected>Choose Defect</option>
#foreach(App\Defect::all() as $defect)
<option value="{{$defect->id}}">{{$defect->name}}</option>
#endforeach
</form>
</td>
<td width="15%">
<input type="file" class="form-control-file" name="image[]">
</td>
<td width="45%">
<input type="text" class="form-control" name="description[]">
</td>
<td width="10%">
<button type="button" class="btn btn-info btn-sm" id="add-btn"><i class="glyphicon glyphicon-plus"></i></button>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<center><button type="submit" class="btn btn-primary">Submit</button></center>
</div>
Here my javascript inside the blade
#section('footer')
<script>
$(document).ready(function () {
$('#add-btn').on('click',function () {
var html = '';
html += '<tr>';
html += ' <td><input type="text" class="hidden" id="user_id" value="{{Auth::user()->id}}"><select class="form-control" name="defect_id[]"><option value="" selected>Choose Defect</option>#foreach(App\Defect::all() as $defect)<option value="{{$defect->id}}">{{$defect->name}}</option>#endforeach</td>';
html += ' <td><input type="file" class="form-control-file" name="image[]"></td>';
html += ' <td><input type="text" class="form-control" name="description[]"></td>';
html += ' <td><button type="button" class="btn btn-danger btn-sm" id="remove-btn"><i class="glyphicon glyphicon-minus"></i></button></td>';
html += '</tr>';
$('tbody').append(html);
})
});
$(document).on('click','#remove-btn',function () {
$(this).closest('tr').remove();
});
</script>
#stop

How to post the values into database when at least one check box is checked

My view with checkbox validation script. I want that when I select one checkbox the data related to it should be stored.
<form action="<?php echo site_url('need_assesment/insertservice'); ?>" [enter image description here][1]method="post">
<div class="col-lg-11 col-md-5 col-sm-8 col-xs-9 bhoechie-tab-container">
<div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 bhoechie-tab-menu">
<div class="list-group">
<?php foreach($member_details as $members){?>
<a href="<?php echo base_url('index.php/need_assesment/req_questions/'.$m_id."/".$members->id);?>" class="list-group-item active text-center">
<br/><?php echo $members->member_name; ?>
</a>
<?php }?>
</div>
</div>
<div class="col-lg-10 col-md-9 col-sm-9 col-xs-9 bhoechie-tab">
<!-- flight section -->
<div class="bhoechie-tab-content active">
<center>
<table style="width:100%" id="zero_config" class="table table-striped table-bordered">
<col width="45%">
<col width="30%">
<col width="25%">
<col width="10%">
<tr>
<td>
Requirements
</td>
<td>
Schemes & Solution
</td>
<td>
Process
</td>
<td>
Fees
</td>
</tr>
<?php foreach ($requirement_details as $details) { ?>
<tr>
<td>
<?php echo $details->Question ?>
</td>
<td>
<input type="hidden" value="<?php echo $member_details[0]->id ?>" name="member_id[]">
<input type="hidden" value="<?php echo $m_id ?>" name="membership_id[]">
<input type="hidden" value="1" name="service_id[]">
b
</td>
<td>
c
</td>
<td>
<label class="checkbox-inline" data-id="1" ><input type="checkbox" name="a-1" value="A1">Rs=100</label><br><br>
</td>
</tr>
<?php } ?>
</table>
<div class="act"><p><b>TOTAL =</b></p>
<p><b>GRAND TOTAL=</b></p>
</div>
</center>
</div>
<!-- train section -->
</div>
</div>
<div class="card-body">
<input type="submit" name="submit" value="PROCEED" id="checkBtn" style="margin-left:40%;" class="btn btn-success">
</div>
</div>
</div>
</div>
</form>
</script>
<script type="text/javascript">
$(document).ready(function () {
$('#checkBtn').click(function() {
checked = $("input[type=checkbox]:checked").length;
if(!checked) {
alert("You must check at least one checkbox.");
return false;
}
});
});
</script>
Controller
public function insertservice(){
$mid=$this->input->post('membership_id');
$data =array();
$id=$this->input->post('member_id');
$service_id=$this->input->post('service_id');
for ($i = 0; $i < count($this->input->post('member_id')); $i++) {
$data[$i] = array(
'member_id'=>$id[$i],
'service_id'=>$service_id[$i]
);
}
$this->db->insert_batch('selected_services',$data);
$this->req_questions($mid[0],$data[0]['member_id']);
}
Image:

How to multiple update rows with modal bottstrap

hello guys can you help me with my problem, i want to update multiple rows with modal bootstrap
So when I check the line and press the update button, it will appear modal bootstrap and I will update from there
I'm having trouble finding the script, can you help me finish my code?
this is the explanation
I checked the line
After that I press "Pindah Department" or in english "Move Departemen"
this basically updates quickly, just you check the line and press the "Move Departement" button, then bootstrap capital appears and you will select the value in the dropdown to update the line
this is my view :
<div class="row">
<div class="col-md-12">
<div class="panel panel-info">
<div class="panel-heading">Data Siswa Departemen ......</div>
<div class="panel-body">
<table id="emp_id" class="table table-bordered dt-responsive" cellspacing="0" width="100%">
<thead>
<tr>
<th width="1%" align="center"><input type="checkbox" onClick="toggle(this)" id="checkAll" name="checkAll" /></th>
<th width="1%" align="center">No.</th>
<th width="20%" align="center">Nama Lengkap</th>
<th width="5%" align="center">No Induk</th>
<th width="10%" align="center">Jenis Kelamin</th>
<th width="5%" align="center">PIN</th>
<th width="20%" align="center">Departemen</th>
</tr>
</thead>
<tbody>
<?php
foreach($data as $d){
?>
<tr>
<td>
<input class="childbox" width="1%" type="checkbox" name="msg[]" align="center" value="" data-userid="<?php echo $d['emp_id'] ?>"/>
</td>
<td width="1%" align="center"><?php echo $d['emp_id']; ?></td>
<td class="data-check"><?php echo $d['first_name']; ?></td>
<td class="data-check"><?php echo $d['nik']; ?></td>
<td class="data-check"><?php echo $d['gender']=='0' ? 'Laki-Laki' : 'Perempuan'; ?></td>
<td class="data-check"><?php echo $d['pin']; ?></td>
<td class="data-check"><?php echo $d['dept_name']; ?></td>
</tr>
<?php } ?>
</tbody>
<tfoot>
<tr>
<th width="1%" align="center"><input type="checkbox" onClick="toggle(this)" id="checkAll" name="checkAll" /></th>
<th width="1%" align="center">No.</th>
<th width="20%" align="center">Nama Lengkap</th>
<th width="5%" align="center">No Induk</th>
<th width="10%" align="center">Jenis Kelamin</th>
<th width="5%" align="center">PIN</th>
<th width="20%" align="center">Departemen</th>
</tr>
</tfoot>
</table>
</div>
<div class="panel-footer">
<button class="btn btn-success" onclick="edit_book(<?php echo $d['emp_id'];?>)"> Move Departemen</button>
</div>
</div><!--end panel-->
<script src="<?php echo base_url() ?>assets/baru/jquery-3.2.1.min.js"></script>
<script src="<?php echo base_url() ?>assets/baru/jquery.dataTables.min.js"></script>
<script src="<?php echo base_url() ?>assets/baru/dataTables.bootstrap4.min.js"></script>
<script type="text/javascript">
$(document).ready( function () {
$('#emp_id').DataTable( {
"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]]
} );
$("input[name='checkAll']").click(function() {
var checked = $(this).attr("checked");
$("#emp_id tr td input:checkbox").attr("checked", checked); });
} );
function toggle(id) {
checkboxes = document.getElementsByName('msg[]');
for(var i=0, n=checkboxes.length;i<n;i++) {
checkboxes[i].checked = id.checked;
}
}
function save()
{
var url;
if(save_method == 'add')
{
url = "<?php echo site_url('proses/book_add')?>";
}
else
{
url = "<?php echo site_url('proses/book_update')?>";
}
// ajax adding data to database
$.ajax({
url : url,
type: "POST",
data: prepareData(),
dataType: "JSON",
success: function(data)
{
//if success close modal and reload ajax table
$('#modal_form').modal('hide');
location.reload();// for reload a page
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error adding / update data');
}
});
}
</script>
<!--modal-->
<div class="modal fade" id="modal_form" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Update Departemen</h4>
</div>
<div class="modal-body form">
<form action="#" id="form" class="form-horizontal">
<input type="hidden" value="" name="emp_id"/>
<div class="form-body">
<div class="form-group">
<label class="control-label col-md-3">Departemen</label>
<div class="col-md-9">
<select name="dept_id_auto" class="form-control pull-right">
<?php
foreach($groups as $c)
{
echo '<option value="'.$c['dept_id_auto'].'">'.$c['dept_name'].'</option>';
}
?>
</select>
</div>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" id="btnSave" onclick="save()" class="btn btn-primary">Save</button>
<button type="button" class="btn btn-info" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
</div><!-- end cols-->
</div><!--end row-->
I have trouble finding the script that will be called in the "Move Departement" button
This is my controller :
public function pindah_departemen()
{
// MASUKKAN PARAMETER DATA DISINI, BIASANYA HASIL DARI QUERY
$data = array(
'title' => 'Pindah Departemen',
'data' => $this->Pindah_dept_model->GetSiswa($this->input->get('filter_departemen'))
);
$data['groups'] = $this->Pindah_dept_model->getAllGroups();
$this->template->load('template','proses/pindah_departemen', $data);
}
public function book_update()
{
$data = array(
'dept_id_auto' => $this->input->post('dept_id_auto'),
);
$this->Pindah_dept_model->update_departemen(array('emp_id' => $this->input->post('emp_id')), $data);
echo json_encode(array("status" => TRUE));
}
public function ajax_edit($id)
{
$data = $this->Pindah_dept_model->get_by_id($id);
echo json_encode($data);
}
This is my Model :
class Pindah_dept_model extends CI_Model
{
var $table = 'emp';
public function GetSiswa($dep=NULL)
{
$this->db->select(array('emp_id', 'first_name', 'nik', 'gender', 'pin', 'dept_name'))
->from('emp AS e')
->join('dept AS d','d.dept_id_auto = e.dept_id_auto', 'left');
if(!empty($dep)) $this->db->where('d.dept_id_auto', $dep);
$data = $this->db->order_by('emp_id','ASC')->get();
return $data->result_array();
}
public function getAllGroups()
{
$query = $this->db->query('SELECT dept_id_auto,dept_name FROM dept');
return $query->result_array();
}
public function get_by_id($id)
{
$this->db->from($this->table);
$this->db->where('emp_id',$id);
$query = $this->db->get();
return $query->row();
}
public function update_departemen($where, $data)
{
$this->db->update($this->table, $data, $where);
return $this->db->affected_rows();
}
please guys help me finish my code, i'm looking for a way out for my code for 2 weeks and still no results
Thanks Before
I don't know if I'm late or not.
You can try do something like below.
HTML
<label>Value 1 <input name='checkme[]' type='checkbox' value='1'></label><br>
<label>Value 2 <input name='checkme[]' type='checkbox' value='2'></label><br>
<label>Value 3 <input name='checkme[]' type='checkbox' value='3'></label><br>
<label>Value 4 <input name='checkme[]' type='checkbox' value='4'></label><br>
<br>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modal_form" >Open Sesame</button>
<div class="modal fade" id="modal_form" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Update Departemen</h4>
</div>
<form action="#" id="form" class="form-horizontal">
<div class="modal-body form">
<input type="hidden" value="" name="emp_id"/>
<div class="form-body">
<div class="form-group">
<label class="control-label col-md-3">Departemen</label>
<div class="col-md-9">
<select name="dept_id_auto" class="form-control pull-right">
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">* This is supposed to be hidden value</label>
<div class="col-md-9">
<input name='list_check'>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" id="btnSave" class="btn btn-primary">Save</button>
<button type="button" class="btn btn-info" data-dismiss="modal">Cancel</button>
</div>
</form>
</div>
</div>
</div>
Add hidden input, and we will get the checked value and store here later.
JAVASCRIPT
$(document).ready(function(){
$('#modal_form').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget); // Button that triggered the modal
var recipient = button.data('whatever'); // Extract info from data-* attributes
// If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
// Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
var vals = $('input:checkbox[name="checkme[]"]').map(function() {
return this.checked ? this.value : undefined;
}).get();
var modal = $(this);
modal.find('.modal-title').text("Open Sesame : Most code in here from bootstrap documentation. XD"); // just for fun.
modal.find(".modal-body input[name='list_check']").val(vals);
});
$("form").on("submit",function(x){
var val_submit = $(this).serialize(); // data to be send via ajax
alert("POST DATA : "+val_submit+"\n This is the value you will submit. The rest you can figure it out. Use explode() to split the list_check. And update like you want it.");
x.preventDefault();
});
});
After the button to open the modal. We will get the value from checkbox name checkme[]. This data need to be explode later in your php.
And try submit it.
I'm not going to take all your code. you need to figure out the rest by yourself. I just prepare example code that can be use. Maybe just like you want it. :)
JSFiddle Example : https://jsfiddle.net/synz/pyb7rjdo/

Categories

Resources