how to echo the data of a table row in a modal? - javascript

what I want to do is that the table( in perfiles.php) is related to the modal, when pressing the edit button in a row, the modal opens with their respective data already loaded from the DB and echo in each input
if you need more details please tell me, do not class it as bad, cause im not the best to clarify my questions and i try
//perfiles.php
<?php
include 'api/conexion.php';
$perfil = mysqli_query($conexion, "SELECT * FROM perfil where usuario = '$_SESSION[usuario]'");
?>
<table id="datatables" class="table table-striped table-no-bordered table-hover" cellspacing="0" width="100%" style="width:100%">
<thead>
<tr>
<th class="text-center">#</th>
<th>Nombre</th>
<th>Cuit</th>
<th>Tipo Persona</th>
<th class="text-right">Cierre de ejercicio</th>
<th class="text-right">Acciones</th>
</thead>
<tbody>
<?php while($reg = mysqli_fetch_array($perfil)) { ?>
<tr id="<?php echo " tr_ ".$reg['id']; ?>">
<td class="row_factura" data-id="<?php echo $reg['usuario'] ?>">
<?php echo $reg['id']; ?>
</td>
<td class="row_factura" data-id="<?php echo $reg['usuario'] ?>">
<?php echo $reg['nombre']; ?>
</td>
<td class="row_factura" data-id="<?php echo $reg['usuario'] ?>">
<?php echo $reg['cuit']; ?>
</td>
<td class="row_factura" data-id="<?php echo $reg['usuario'] ?>">
<?php echo $reg['tipo_persona']; ?>
</td>
<td class="row_factura text-right" data-id="<?php echo $reg['usuario'] ?>">
<?php echo $reg['cierre_ejercicio']; ?>
</td>
<td class="td-actions text-right">
<button type="button" rel="tooltip" class="btn btn-info" data-original-title="" title="ver/editar perfil" data-toggle="modal" data-target="#modal_ajustes_perfil" href="#">
<i class="material-icons">person</i>
</button>
<button type="button" rel="tooltip" class="btn btn-success" data-original-title="" title="ver/editar impuestos" data-toggle="modal" data-target="#modal_ajustes_impuestos" href="#">
<i class="material-icons">edit</i>
</button>
<button id="<?php echo $reg['id'] ?>" type="button" rel="tooltip" class="btn btn-danger" onclick="mod('<?php echo $reg['id']; ?>', 'perfiles');" data-original-title="" title="eliminar perfil">
<i class="material-icons">close</i>
</button>
</td>
</tr>
<?php } ?>
</tbody>
</table>
//modal
<div class="modal fade" id="modal_ajustes_perfil" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="card">
<div class="card-header card-header-icon" data-background-color="blue">
<i class="material-icons">perm_identity</i>
</div>
<div class="card-content">
<h4 class="card-title">Datos del Perfil -
<small class="category">Completar perfil</small>
</h4>
<form>
<div class="row">
<div class="col-md-6">
<div class="form-group label-floating">
<label class="control-label">Nombre</label>
<input type="text" class="form-control" disabled>
</div>
</div>
<div class="col-md-6">
<div class="form-group label-floating">
<label class="control-label">Cuit</label>
<input type="text" class="form-control" disabled>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group label-floating">
<label class="control-label">Tipo persona</label>
<input type="email" class="form-control" disabled>
</div>
</div>
<div class="col-md-6">
<div class="form-group label-floating">
<label class="control-label">Cierre del ejercicio</label>
<input type="text" class="form-control" disabled>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group label-floating">
<label class="control-label">Dirección</label>
<input type="text" class="form-control" disabled>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group label-floating">
<label class="control-label">Email</label>
<input type="text" class="form-control">
</div>
</div>
<div class="col-md-6">
<div class="form-group label-floating">
<label class="control-label">Telefono</label>
<input type="text" class="form-control">
</div>
</div>
</div>

If you have issues in Getting all rows for each username from mysql Database? I have re written the code, Kindly replace it.
//perfiles.php
<?php
include 'api/conexion.php';
$perfil = mysqli_query($conexion, "SELECT * FROM `perfil` WHERE `usuario` = '$_SESSION[usuario]'");
?>
<table id="datatables" class="table table-striped table-no-bordered table-hover" cellspacing="0" width="100%" style="width:100%">
<thead>
<tr>
<th class="text-center">#</th>
<th>Nombre</th>
<th>Cuit</th>
<th>Tipo Persona</th>
<th class="text-right">Cierre de ejercicio</th>
<th class="text-right">Acciones</th>
</thead>
<tbody>
<?php while($row = mysqli_fetch_array($perfil, MYSQLI_ASSOC)) {
foreach($row as $reg){
?>
<tr id="<?php echo " tr_ ".$reg['id']; ?>">
<td class="row_factura" data-id="<?php echo $reg['usuario'] ?>">
<?php echo $reg['id']; ?>
</td>
<td class="row_factura" data-id="<?php echo $reg['usuario'] ?>">
<?php echo $reg['nombre']; ?>
</td>
<td class="row_factura" data-id="<?php echo $reg['usuario'] ?>">
<?php echo $reg['cuit']; ?>
</td>
<td class="row_factura" data-id="<?php echo $reg['usuario'] ?>">
<?php echo $reg['tipo_persona']; ?>
</td>
<td class="row_factura text-right" data-id="<?php echo $reg['usuario'] ?>">
<?php echo $reg['cierre_ejercicio']; ?>
</td>
<td class="td-actions text-right">
<button type="button" rel="tooltip" class="btn btn-info" data-original-title="" title="ver/editar perfil" data-toggle="modal" data-target="#modal_ajustes_perfil" href="#">
<i class="material-icons">person</i>
</button>
<button type="button" rel="tooltip" class="btn btn-success" data-original-title="" title="ver/editar impuestos" data-toggle="modal" data-target="#modal_ajustes_impuestos" href="#">
<i class="material-icons">edit</i>
</button>
<button id="<?php echo $reg['id'] ?>" type="button" rel="tooltip" class="btn btn-danger" onclick="mod('<?php echo $reg['id']; ?>', 'perfiles');" data-original-title="" title="eliminar perfil">
<i class="material-icons">close</i>
</button>
</td>
</tr>
<?php } } ?>
</tbody>
</table>
Below is for Modal... You need to provide your Db Structure to Edit the rest of things.. what are the fields to be filled for what with the screenshot
//modal
<?php
include 'api/conexion.php';
$perfil = mysqli_query($conexion, "SELECT * FROM `perfil` WHERE `usuario` = '$_SESSION[usuario]'");
?>
<?php while($row = mysqli_fetch_array($perfil, MYSQLI_ASSOC)) { ?>
<div class="modal fade" id="modal_ajustes_perfil" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="card">
<div class="card-header card-header-icon" data-background-color="blue">
<i class="material-icons">perm_identity</i>
</div>
<div class="card-content">
<h4 class="card-title">Datos del Perfil -
<small class="category">Completar perfil</small>
</h4>
<form>
<div class="row">
<div class="col-md-6">
<div class="form-group label-floating">
<label class="control-label">Nombre</label>
<input type="text" class="form-control" value="<?php echo $reg['nombre']; ?>" disabled>
</div>
</div>
<div class="col-md-6">
<div class="form-group label-floating">
<label class="control-label">Cuit</label>
<input type="text" class="form-control" value="<?php echo $reg['cuit']; ?>" disabled>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group label-floating">
<label class="control-label">Tipo persona</label>
<input type="email" class="form-control" value="<?php echo $reg['tipo_persona']; ?>" disabled>
</div>
</div>
<div class="col-md-6">
<div class="form-group label-floating">
<label class="control-label">Cierre del ejercicio</label>
<input type="text" class="form-control" value="<?php echo $reg['cierre_ejercicio']; ?>" disabled>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group label-floating">
<label class="control-label">Dirección</label>
<input type="text" class="form-control" disabled>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group label-floating">
<label class="control-label">Email</label>
<input type="text" class="form-control" value="<?php echo $reg['email']; ?>">
</div>
</div>
<div class="col-md-6">
<div class="form-group label-floating">
<label class="control-label">Telefono</label>
<input type="text" class="form-control" value="<?php echo $reg['telefono']; ?>">
</div>
</div>
</div>
<?php } ?>

Related

How to fetch and edit/update all the data comes from Database in Bootstrap Modal Popup, even my Fetch Data Table is CONCATENATED?

I have a problem in Fetching all the Data by its ID coming from Database in Bootstrap Modal Popup.
I have a Fetch Table in my Program. This is my code:
<div class="table-responsive">
<?php
require_once("include/connect.php"); //To connect Database
try{
$sql="SELECT patient_id, CONCAT(patient_lname, ', ', patient_fname, ' ', patient_mi) AS Fullname, age, gender, status, address FROM tbl_patientinfo ORDER BY patient_lname"; //Patient Name is CONCATENATED (Fullname)
$result=$con->prepare($sql);
$result->execute();
?>
<table class="table table-hover" id="table-1">
<thead>
<tr>
<th class="text-center">
#
</th>
<th>Patient Name</th>
<th>Age</th>
<th>Gender</th>
<th>Status</th>
<th>Address</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php while($row=$result->fetch()){ ?>
<tr>
<td><?php echo $row['patient_id']; ?></td>
<td><?php echo $row['Fullname']; ?></td> //Concatenated Last Name, First Name, and MI
<td><?php echo $row['age']; ?></td>
<td><?php echo $row['gender']; ?></td>
<td><?php echo $row['status']; ?></td>
<td><?php echo $row['address']; ?></td>
<td>
<button type="button" class="btn btn-icon btn-primary" data-toggle="modal" data-target="#editpatient"><i class="far fa-edit"></i></button> //Modal Button
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php
} catch (PDOException $th){
echo "Error: ".$th->getMessage();
}
$con=null;
?>
</div>
I want to fetch all the data from the Database (written in my $sql on the codes above) in Bootstrap Modal Popup using Javascript or JQuery code. This is my code in Javascript or JQuery:
<!-- SCRIPT FOR FETCH RECORD IN MODAL -->
<script>
$(document).ready(function () {
$('.edit-btn').on('click', function () {
$('#editpatient').modal('show');
$tr = $(this).closest('tr');
var data = $tr.children("td").map(function () {
return $(this).text();
}).get();
console.log(data);
$('#patient_id').val(data[0]);
$('#patient_lname').val(data[1]);
$('#patient_fname').val(data[2]);
$('#patient_mi').val(data[3]);
$('#dob').val(data[4]);
$('#age').val(data[5]);
$('#gender').val(data[6]);
$('#status').val(data[7]);
$('#address').val(data[8]);
$('#cnum').val(data[9]);
$('#company').val(data[10]);
}); });
</script>
<!-- END OF SCRIPT FOR FETCH RECORD IN MODAL -->
And this is my Codes in Modal:
<!-- EDIT MODAL PATIENT -->
<div class="modal fade" id="editpatient" tabindex="-1" role="dialog" aria-labelledby="formModal"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="formModal">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form class="">
<div class="form-group">
<form method="POST" action="save-patient.php">
<input type="text" class="form-control" name="txt-patient_id" id="patient_id" placeholder="ID">
<div class="form-group">
<label>Last Name</label>
<div class="input-group">
<div class="input-group-prepend">
<div class="input-group-text">
<i class="fas fa-user"></i>
</div>
</div>
<input type="text" class="form-control" name="txt-patient_lname" id="patient_lname" placeholder="Last Name">
</div>
</div>
<div class="form-group">
<label>First Name</label>
<div class="input-group">
<div class="input-group-prepend">
<div class="input-group-text">
<i class="fas fa-user"></i>
</div>
</div>
<input type="text" class="form-control" name="txt-patient_fname" id="patient_fname" placeholder="First Name" required="true">
</div>
</div>
<div class="form-group">
<label>Middle Name</label>
<div class="input-group">
<div class="input-group-prepend">
<div class="input-group-text">
<i class="fas fa-user"></i>
</div>
</div>
<input type="text" class="form-control" name="txt-patient_mi" id="patient_mi" placeholder="Middle Name">
</div>
</div>
<div class="form-group">
<label>Date Picker</label>
<input type="text" class="form-control datepicker" name="txt-dob" id="dob" placeholder="Date of Birth" required="true">
</div>
<div class="form-group">
<label>Age</label>
<div class="input-group">
<div class="input-group-prepend">
<div class="input-group-text">
<i class="fas fa-plus"></i>
</div>
</div>
<input type="text" class="form-control" name="txt-age" id="age" placeholder="Age" required="true">
</div>
</div>
<div class="form-group">
<label>Gender</label>
<select class="form-control" name="txt-gender" id="gender" required="true">
<option value="" readonly>--- Please Select ---</option>
<option value="Male">MALE</option>
<option value="Female">FEMALE</option>
</select>
</div>
<div class="form-group">
<label>Civil Status</label>
<select class="form-control" name="txt-status" id="status" required="true">
<option value="" readonly>--- Please Select ---</option>
<option>Single</option>
<option>Married</option>
<option>Widowed</option>
<option>Separated</option>
<option>Divorced</option>
</select>
</div>
<div class="form-group">
<label>Address</label>
<div class="input-group">
<div class="input-group-prepend">
<div class="input-group-text">
<i class="fas fa-home"></i>
</div>
</div>
<input type="text" class="form-control" name="txt-address" id="address" required="true">
</div>
</div>
<div class="form-group">
<label>Contact Number</label>
<div class="input-group">
<div class="input-group-prepend">
<div class="input-group-text">
<i class="fas fa-phone"></i>
</div>
</div>
<input type="text" class="form-control" name="txt-cnum" id="cnum" required="true">
</div>
</div>
<div class="form-group">
<label>Company(Optional)</label>
<div class="input-group">
<div class="input-group-prepend">
<div class="input-group-text">
<i class="fas fa-building"></i>
</div>
</div>
<input type="text" class="form-control" name="txt-company" id="company">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save</button>
</div>
</form>
</div>
</div>
</div>
</div>
<!-- End of EDIT MODAL PATIENT -->
My Problem is I want to fetch all data coming from database (means, not concatenated) by its ID but when the Modal shows, the data shows a concatenated data coming from the FETCH DATA TABLE.
This is my sample FETCH DATA TABLE
And this is my Problem
Please help me to fix my problem. I already do this a month ago and until now, I can't solve this problem. I'm still new in this Programming Languages. I need help from others like you Guys. I hope you everybody can help me to fix this. Thank you so much <3

Append values are not fetching

The div appended value is not getting in controller but the appended thing is visible on the view page correctly and i had gone through several ways but couldn't reach to the correct point.
Here is my view section please have a look
<script type="text/javascript">
var maxAppends = 0;
function add_field(id)
{
var update_new = $('<div class="form-group" style="margin-bottom: 0px">\n\
<label for="field-1" class="col-sm-4 control-label">Documentsss</label>\n\
<div class="col-sm-5">\n\
<div class="fileinput fileinput-new" data-provides="fileinput">\n\
<span class="btn btn-default btn-file"><span class="fileinput-new" >Select file</span><span class="fileinput-exists" >Change</span><input type="file" name="files[]" ></span> <span class="fileinput-filename"></span>×</div></div>\n\<div class="col-sm-2">\n\<strong>\n\
<i class="fa fa-times"></i> Remove</strong></div>');
maxAppends++;
$(".update_new_"+id).append(update_new);
}
</script>
<div class=" table-responsive div1" id="EmpprintReport">
<table id="myTable" class="table table-bordered table-striped table2excel">
<thead>
<tr>
<th>Document Name</th>
<th>Documents</th>
<th>Number</th>
<th>Expiry Date</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
if(!empty($details))
{
foreach ($details as $value) {
?>
<tr>
<td nowrap><?= $value->service_name;?></td>
<td nowrap>
<?php if (!empty($value->documents)): ?>
<div class="form-group">
<!-- <label class="col-sm-4 control-label"><?= ('Documents') ?>
: </label> -->
<div class="col-sm-8">
<?php
$uploaded_file = json_decode($value->documents);
if (!empty($uploaded_file)):
foreach ($uploaded_file as $sl => $v_files):
if (!empty($v_files)):
?>
<p class="form-control-static">
<a href="<?php echo base_url() . 'uploads/documents/' . $v_files; ?>"
target="_blank"
style="text-decoration: underline;"><?= $sl + 1 . '. ' . ('view') . ' ' . ('other_document') ?></a>
</p>
<?php
endif;
endforeach;
endif;
?>
</div>
</div>
<?php endif; ?>
</td>
<td nowrap><?= $value->service_number;?></td>
<td nowrap><?= $value->expiry_date;?></td>
<td>
<textarea id="<?php echo $value->id;?>" hidden> <?php echo $value->documents;?></textarea>
<a href="" data-toggle="modal" data-target="#edit_<?php echo $value->id;?>" id="Button3" id="Button3" class="btn btn-sm btn-warning" style="margin-top:5px;">
<i class="far fa-edit text-white" title="Edit"></i></a>
<a data-toggle="modal" data-target="#confirm_delete" name="delete" id="title" c-d-id="<?php echo $value->id;?>" clientid="<?= $id; ?>" class="btn btn-sm btn-danger deletedocument" style="margin-top:5px;">
<i class="fa fa-trash text-white" data-toggle="tooltip" data-placement="bottom" title="Delete"></i></a></td>
</tr>
<div class="modal fade edit" id="edit_<?php echo $value->id;?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" style="padding:20px !important;">
<div class="modal-dialog modal-md" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="myModalLabel">Edit Document</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
</div>
<div class="modal-body">
<?php
$result=$this->Admin_model->get_services_by_id($value->id);
//var_dump($value->id);
?>
<form method="post" class="form_<?php echo $value->id;?>" action="<?php echo base_url();?>admin/edit-client-registration-details/<?= $value->id; ?>/<?= $id; ?>" enctype="multipart/form-data">
<div class="form-group">
<div class="control-group">
<label>Service Name</label>
<input type="text" name="service_name" class="form-control" placeholder="Enter service name" style="width: 100%" value="<?= $value->service_name; ?>" /> </div>
<div class="control-group">
<label>Number</label>
<input type="text" name="service_no" class="form-control" placeholder="Enter number" style="width: 100%" value="<?= $value->service_number; ?>" />
</div>
<div class="control-group">
<label>Expiry Date</label>
<input type="text" name="date" id="date_<?= $value->id; ?>" class="form-control input-sm datepick" data-bind="datePicker: StartDate" value="<?= $value->expiry_date; ?>" />
</div>
<div id="update_new_<?php echo $value->id;?>" class="update_new_<?php echo $value->id;?> item-row">
<div class="form-group mb0" style="margin-bottom: 0px">
<label for="field-1"
class="col-sm-4 control-label">Other Document</label>
<div class="col-sm-5">
<div class="fileinput fileinput-new" data-provides="fileinput">
<?php
if (!empty($value->documents)) {
$uploaded_file = json_decode($value->documents);
}
if (!empty($uploaded_file)):foreach ($uploaded_file as $v_files_image): ?>
<div class="">
<input type="hidden" name="fileName[]"
value="<?php echo $v_files_image ?>">
<span class=" btn btn-default btn-file">
<span class="fileinput-filename"> <?php echo $v_files_image ?></span>
×
</span>
<strong>
<a href="javascript:void(0);" class="RCF"><i
class="fa fa-times"></i> Remove</a></strong>
<p></p>
</div>
<?php endforeach; ?>
<?php endif; ?>
<!-- Here i want the appended result and the result is coming but the values selected here is not passing to the controller while submitting-->
</div>
<div id="msg_pdf" style="color: #e11221"></div>
</div>
<div class="col-sm-3">
<strong><a href="javascript:void(0);" id="update_more" onclick="add_field('<?php echo $value->id;?>');" u_id="<?php echo $value->id;?>" class="addCF item-row-click update_more"><i
class="fa fa-plus"></i> Add More
</a></strong>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<img src="<?php echo base_url();?>assets/resources/ajax-loader.gif" id="ajaxIndicator" style="display: none; margin-right: 10px" />
<input type="submit" id="btnSubmit" class="btn btn-primary" style="font-weight: bold" value="Edit Details" />
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<!-- <button type="button" class="btn btn-primary">Save changes</button> -->
</div>
</form>
</div>
</div>
</div>
</div>
<?php
}
}
?>
</tbody>
<tfoot>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</tfoot>
</table>
</div>

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:

populate field with data from DB in Modal Bootstrap 3 using Codeigniter and jquery

im using bootstrap 3 framework and codeigniter 3, i want to edit a row from table but i want to display row in thier field in modal
Code Html of table
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Nom et prénom</th>
<th>Age</th>
<th>Sexe</th>
<th>Assurance</th>
<th>Téléphone</th>
<th>E-mail</th>
<th></th>
</tr>
</thead>
<tbody>
<?php
foreach ($query->result() as $row)
{
?>
<tr>
<td><?php echo $row->no_dossier_pt; ?></td>
<td><?php echo $row->nom_pt .' '. $row->prenom_pt ?></td>
<?php
$date = new DateTime($row->datenaissance_pt);
$now = new DateTime();
$interval = $now->diff($date);
?>
<td><?php echo $interval->y .' ans'; ?></td>
<td><?php echo $row->sexe_pt; ?></td>
<td><?php echo $row->assurance_pt; ?></td>
<td><?php echo $row->telephone_pt; ?></td>
<td><?php echo $row->email_pt; ?></td>
<td>
<a type="button" href="<?php echo base_url() ?>patient/patient_delete/<?php echo $row->id; ?>" class="btn btn-danger">Supprimer</a>
<a type="button" data-toggle="modal" href="#editpatient" data-id="<?php echo $row->id; ?>" class="btn btn-warning" >Editer</a>
<?php $this->load->view('template/patient_update'); ?>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<?php
$this->load->view('template/footer');
?>
but i can't understand how can i pass 'id' of this row to the controller and return the specific data row from controller to thier field in Modal form,
this is code of button
<a type="button" data-toggle="modal" href="#editpatient" data-id="<?php echo $row->id; ?>" class="btn btn-warning" >Editer</a>
and my Modal form Code
<!-- Modal -->
<div class="modal fade" id="editpatient" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" 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">×</span></button>
<h4 class="modal-title" id="myModalLabel"> Information patient 2</h4>
</div>
<!-- formulaire -->
<?php echo form_open('patient/patient_update'); // action to the controller?>
<div class="modal-body">
<!-- hidden input montinned with class sr-only -->
<label class="sr-only" =""></label>
<input type="text" name="no_dossier_pt" class="sr-only" >
<div class="form-group">
<input type="text" class="form-control" id="nom_pt" name="nom_pt" id="nom_pt" placeholder="Nom ...">
</div>
<div class="form-group">
<input type="text" class="form-control" id="prenom_pt" name="prenom_pt" id="prenom_pt" placeholder="Prénom ....">
</div>
<div class="form-group">
<label for="exampleInputFile">Sexe : </label>
<input type="radio" name="sexe_pt" id="radio-choice-1" value="homme" checked="checked" />
<label for="radio-choice-1"> Homme </label>
<input type="radio" name="sexe_pt" id="radio-choice-2" value="femme" />
<label for="femme"> Femme </label>
<input type="radio" name="sexe_pt" id="radio-choice-3" value="enfant" />
<label for="enfant">Enfant</label>
</div>
<div class="form-group ">
<div class="input-group">
<div class="input-group-addon">
<i class="fa fa-calendar">
</i>
</div>
<input class="form-control" id="date" name="date" placeholder="Date de naissance" type="text"/>
</div>
</div>
<div class="form-group">
<label for="exampleInputFile">Assurance : </label>
<input type="radio" name="assurance_pt" id="radio-choice-1" value="oui" checked="checked" />
<label for="radio-choice-1"> Oui </label>
<input type="radio" name="assurance_pt" id="radio-choice-2" value="non" />
<label for="femme"> Non </label>
</div>
<div class="form-group">
<input type="text" class="form-control" name ="telephone_pt" placeholder="Téléphone ...">
</div>
<div class="form-group">
<input type="text" class="form-control" name ="email_pt" placeholder="Email ...">
</div>
<div class="form-group">
<textarea class="form-control" cols="40" name="note_pt" rows="3" placeholder="Note sur ce patient ..."></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Fermer</button>
<button type="submit" class="btn btn-primary">Enregistrer</button>
</div>
<?php echo form_close(); ?>
</div>
</div>
</div>
<!-- Modal -->
Controller Methode to display
public function patient_selectbyid()
{
$data = array();
$id = $this->input->post('pt_id');
$this->load->model('patient_model');
$data = $this->studentModel->getStudent($id);
echo $data;
}
Code jquery
$(document).ready(function() {
var id_edit = $(this).data('id');
var base_url = <? php echo base_url('patient/patient_selectbyid'); ?> ;
$('.editStudent').click(function() {
$.ajax({
url: base_url,
type: 'POST',
data: {
'pt_id': id_edit
},
dataType: 'JSON';
success: function(result) {
$('.modal-body #nom_pt').val(result[0].nom_pt);
$('.modal-body #prenom_pt').val(result[0].prenom_pt);
}
});
});
});
I hope someone can help me coding this, thanks you
thanks brother for your recommandations , i had resolve the problem by using event listner as you say, i just sent data DB using data attribute in the button like this:
<button type="button" class="btn btn-warning" data-toggle="modal" data-target="#editPatient" data-id="<?php echo $row->id ?>" data-nom="<?php echo $row->nom_pt ?>" data-prenom="<?php echo $row->prenom_pt ?>" data-nod="<?php echo $row->no_dossier_pt ?>" data-sexe="<?php echo $row->sexe_pt; ?>" data-dn="<?php echo $row->datenaissance_pt; ?>" data-assur="<?php echo $row->assurance_pt; ?>" data-tele="<?php echo $row->telephone_pt; ?>" data-email="<?php echo $row->email_pt; ?>" data-note="<?php echo $row->note_pt; ?>" >
Modifier
the modal like this
<!-- Modal -->
<div class="modal fade" id="editPatient" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" 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">×</span></button>
<h4 class="modal-title" id="myModalLabel"> Information patient </h4>
</div>
<div class="modal-body">
<!-- formulaire -->
<?php echo form_open('patient/patient_update'); ?>
<label class="sr-only" =""></label>
<input type="text" name="id" class="sr-only" id="id_hidd" >
<div class="form-group">
<input type="text" class="form-control" id="nom_pt" name="nom_pt" placeholder="Nom ...">
</div>
<div class="form-group">
<input type="text" class="form-control" id="prenom_pt" name="prenom_pt" placeholder="Prénom ....">
</div>
<div class="form-group">
<label for="exampleInputFile">Sexe : </label>
<input type="radio" name="sexe" id="homme" value="homme" checked="checked" />
<label for="homme"> Homme </label>
<input type="radio" name="sexe" id="femme" value="femme" />
<label for="femme"> Femme </label>
<input type="radio" name="sexe" id="enfant" value="enfant" />
<label for="enfant">Enfant</label>
</div>
<div class="form-group ">
<div class="input-group">
<div class="input-group-addon">
<i class="fa fa-calendar">
</i>
</div>
<input class="form-control" id="date" name="date" placeholder="Date de naissance" type="text"/>
</div>
</div>
<div class="form-group">
<label for="exampleInputFile">Assurance : </label>
<input type="radio" name="assurance" id="assur" value="oui" checked="checked" />
<label for="radio-choice-1"> Oui </label>
<input type="radio" name="assurance" id="nonassur" value="non" />
<label for="femme"> Non </label>
</div>
<div class="form-group">
<input type="text" class="form-control" id="telephone_pt" name ="telephone_pt" placeholder="Téléphone ...">
</div>
<div class="form-group">
<input type="text" class="form-control" id ="email_pt" name ="email_pt" placeholder="Email ...">
</div>
<div class="form-group">
<textarea class="form-control" cols="40" id="note_pt" name="note_pt" rows="3" placeholder="Note sur ce patient ..."></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Fermer</button>
<button type="submit" class="btn btn-primary">Enregistrer</button>
</div>
<?php echo form_close(); ?>
</div>
</div>
</div>
script that recive data and displayed on Modal form like this:
<script type="text/javascript">
$('#editPatient').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget) // Button that triggered the modal
var id = button.data('id')
var prenom = button.data('prenom')
var nom = button.data('nom')
var nod = button.data('nod')
var sexe = button.data('sexe')
var dn = button.data('dn')
var tele = button.data('tele')
var email = button.data('email')
var note = button.data('note')
if (sexe == 'femme'){
var id_sexe = "femme"
document.getElementById(id_sexe).setAttribute("checked", "checked")
}
if (sexe == 'homme'){
var id_sexe = "homme"
document.getElementById(id_sexe).checked = true
}
if (sexe == 'enfant'){
var id_sexe = "enfant"
document.getElementById(id_sexe).checked = true
}
if (assur == 'oui'){
var id_assur = "assur"
document.getElementById(id_assur).checked = true
}
if (assur == 'non'){
var id_assur = "nonassur"
document.getElementById(id_assur).checked = true
}
var modal = $(this)
modal.find('.modal-title').text('Modifier patient ('+nod+')')
modal.find('.modal-body #id_hidd').val(id)
modal.find('.modal-body #nom_pt').val(nom)
modal.find('.modal-body #prenom_pt').val(prenom)
modal.find('.modal-body #date').val(dn)
modal.find('.modal-body #telephone_pt').val(tele)
modal.find('.modal-body #email_pt').val(email)
modal.find('.modal-body #note_pt').val(note)
})
</script>
thanks for your help if you have any other suggestion or question i'll be gratfull

how to populate the fields in my bootstrap modal from clicking a row in the view table in codeigniter

I have this bootstrap3 modal that is called when I click a certain row in a table and shows the details of a student supposedly ready for editing. But the problem is the details doesn't appear in the fields where I put the value via ajax. By the way I am using Codeigniter as my php framework.
I am looking for solutions for days now but still can't find the right answer for my problem...
This is my Controller
public function getStudent(){
$data = array();
$id = $this->input->post('stud_id');
$this->load->model('studentModel');
$data = $this->studentModel->getStudent($id);
echo $data;
}
Model
public function getStudent($id){
$query = array();
$this->db->select('*');
$this->db->from('stud_bio');
$this->db->where('stud_id', $id);
$query = $this->db->get();
return $query->result();
}
View
<div class="container">
<div class="row">
<div class="table table-responsive">
<table class="table table-hover table-striped table-condensed table-bordered">
<thead class="header">
<tr>
<th>Name</th>
<th>Gender</th>
<th>Marital Status</th>
<th>Name of Spouse</th>
<th>Number of Children</th>
<th>Date of Birth</th>
<th>Contact Number</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php foreach($data as $row){ ?>
<tr data-toggle="modal" href="#editStudent" data-id="<?php echo $row->stud_id?>" class="editStudent">
<td><?php echo $row->name?></td>
<td><?php if($row->gender == 1){echo "Male";}else{echo "Female";} ?></td>
<td><?php if($row->marital_status == 1){echo "Single";}else{echo "Married";}?></td>
<td><?php echo $row->spouse_name ?></td>
<td><?php echo $row->no_of_children ?></td>
<td><?php echo $row->DOB ?></td>
<td><?php echo $row->contactNo ?></td>
<td><?php echo $row->email ?></td>
<?php }?>
</tbody>
</table>
</div>
</div>
Modal
<div class="container">
<div class="row">
<div class="modal fade" id="editStudent" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<?=form_open(base_url('admin/studentController/editStudent'))?>
<div class="form-horizontal">
<div class="modal-header"><!--HEADER-->
<button class="btn btn-primary close" data-dismiss="modal" aria-hidden="true">×</button>
<h4>Edit Student</h4>
</div><!--end of HEADER-->
<div class="modal-body"><!--BODY-->
<!--Name-------------------------------------------->
<div class="form-group">
<label for="edit_fullname" class="col-sm-2 control-label">Fullname</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="edit_fullname" name="edit_fullname" placeholder="Name">
</div>
<label class="radio-inline has-error">
<?php echo form_error('edit_fullname','*<span3 class="help-inline">','</span3>'); ?>
</label>
</div><!--end of NAME -->
<!--Gender------------------------------------------>
<div class="form-group">
<label class="col-sm-2 control-label">Gender</label>
<div class="col-sm-4">
<label for="edit_male" class="radio-inline">
<input type="radio" name="edit_genderRadio" value="1" id="edit_male"/>Male
</label>
<label for="edit_female" class="radio-inline">
<input type="radio" name="edit_genderRadio" value="2" id="edit_female"/>Female
</label>
</div>
<label class="radio-inline has-error">
<?php echo form_error('edit_genderRadio','*<span3 class="help-inline">','</span3>'); ?>
</label>
</div>
<!--Civil Status----------------------------------->
<div class="form-group">
<label class="col-sm-2 control-label">Civil Status</label>
<div class="col-sm-4">
<label for="edit_S" class="radio-inline">
<input type="radio" name="edit_civilRadio" value="1"<?=set_radio('civilRadio', '1')?> id="edit_S"/>Single
</label>
<label for="edit_M" class="radio-inline">
<input type="radio" name="edit_civilRadio" value="2"<?=set_radio('civilRadio', '2')?> id="edit_M"/>Married
</label>
</div>
<label class="radio-inline has-error">
<?php echo form_error('edit_civilRadio','*<span3 class="help-inline">','</span3>'); ?>
</label>
</div>
<!--If Married Spouse name--------------------------------->
<div class="form-group">
<label for="edit_spouseName" class="col-sm-2 control-label">Spouse Name</label>
<div class="col-sm-4">
<input type="text" name="edit_spouseName" id="edit_spouseName" class="form-control" placeholder="Spouse name"/>
</div>
<label class="radio-inline has-error">
<?php echo form_error('edit_spouseName','*<span3 class="help-inline">','</span3>'); ?>
</label>
</div>
<!--Children #------------------------------------------------->
<div class="form-group">
<label for="edit_numChildren" class="col-sm-2 control-label">Number of Children</label>
<div class="col-md-2">
<input type="text" class="form-control" name="edit_numChildren" id="edit_numChildren" placeholder="- - -"/>
</div>
<label class="radio-inline has-error">
<?php echo form_error('edit_numChildren','*<span3 class="help-inline">','</span3>'); ?>
</label>
</div>
<!--Date of birth--------------------------------------------------->
<div class="form-group">
<label for="edit_DOB" class="col-sm-2 control-label">Date of Birth</label>
<div class="col-sm-4">
<div class='input-group date' data-date-format="yyyy/mm/dd">
<input type='text' class="form-control" readonly="" name="edit_DOB" id="edit_DOB" />
<span class="input-group-addon">
<i class="fa fa-calendar"></i>
</span>
</div>
</div>
<label class="radio-inline has-error">
<?php echo form_error('edit_DOB','*<span3 class="help-inline">','</span3>');?>
</label>
</div>
<!--Email----------------------------------------------------------->
<div class="form-group">
<label for="edit_email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-4">
<input type="email" id="edit_email" name="edit_email" class="form-control" placeholder="Enter email.."/>
</div>
<label class="radio-inline has-error">
<?php echo form_error('edit_email','*<span3 class="help-inline">','</span3>');?>
</label>
</div>
<!--Contact Number--------------------------------------------------------->
<div class="form-group">
<label for="edit_contactNo" class="col-sm-2 control-label">Contact No.</label>
<div class="col-sm-4">
<input type="text" id="edit_contactNo" name="edit_contactNo" class="form-control" placeholder="Contact No." />
</div>
<label class="radio-inline has-error">
<?php echo form_error('edit_contactNo','*<span3 class="help-inline">','</span3>');?>
</label>
</div>
</div> <!--end of BODY-->
<div class="modal-footer"><!--FOOTER-->
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Save</button>
</div><!--end of FOOTER-->
</div><!--end of form-horizontal -->
<?=form_close()?>
</div>
</div>
</div>
</div>
Script
<script type="text/javascript">
$(document).ready(function(){
var id_edit = $(this).data('id');
var base_url = <?php echo base_url('admin/studentController/getStudent');?>;
$('.editStudent').click(function (){
$.ajax({
url: base_url,
type: 'POST',
data: { 'stud_id': id_edit},
dataType: 'JSON';
success: function(result){
$('.modal-body #edit_fullname').val(result[0].name);
$('.modal-body .edit_genderRadio').val(result[0].gender);
$('.modal-body .edit_civilStatus').val(result[0].marital_status);
$('.modal-body #edit_spouseName').val(result[0].spouse_name);
$('.modal-body #edit_numChildren').val(result[0].no_of_children);
$('.modal-body .edit_DOB').val(result[0].DOB);
$('.modal-body #edit_email').val(result[0].email);
$('.modal-body #edit_contactNo').val(result[0].contactNo);
}
});
});
});
</script>

Categories

Resources