How to send Personal Message using AJAX, PHP and MySQL? - javascript

I am trying to make a script, to send PM to user which is selected from MySQL.
I have users list:
http://i.stack.imgur.com/9fRZV.png
When I click PM button, I get Javascipt sceen:
http://i.stack.imgur.com/4bJ9A.png
How to fill-in Javascript screen fields with values from users list automatically and send values to other PHP file (with AJAX)?
Code of users list table:
<!-- Table -->
<table class="table table-condensed">
<tr class="bg_h">
<th>Username</th>
<th>Email</th>
<th>Rank</th>
<th>Active?</th>
<th>Registration date</th>
<th>IP</th>
<th>PM</th>
</tr>
<?php
$sql = "SELECT * FROM users ORDER BY user_id ASC";
$query = $dbh->prepare($sql);
$query->execute();
$list = $query->fetchAll();
foreach ($list as $row) {
?>
<tr class="">
<td><?php echo $row['user_name']; ?></td>
<td><?php echo $row['user_email']; ?></td>
<td>
<?php
PHP code of getting user rank.
?>
</td>
<td>
<?php
PHP code to chech is user active.
?>
</td>
<td>
<?php echo $row['user_registration_datetime']; ?>
</td>
<td>
<?php echo $row['user_registration_ip']; ?>
</td>
<td>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#send-pm">#PM</button>
<div class="modal fade" id="send-pm" tabindex="-1" role="dialog" aria-labelledby="send-pm-label" aria-hidden="true">
<div class="modal-dialog">
<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="send-pm-label">Send personal message</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label for="name" class="control-label">Receiver name</label>
<textarea style="height:35px;" name="name" id="name" class="form-control" ></textarea>
</div>
<div class="form-group">
<label for="email" class="control-label">Email</label>
<textarea style="height:35px;" name="email" id="email" class="form-control" ></textarea>
</div>
<div class="form-group">
<label for="message" class="control-label">Message</label>
<textarea style="height:250px;" name="message" id="message" class="form-control" ></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" id="submit" class="btn btn-primary">SEND</button>
</div>
</div>
</div>
</div>
</td>
</tr>
<?php
}
?>
</table>

First of all, you don`t need a modal dialog for every row in table. Take it out from the for-each loop.
Also, add a class to every #PM button for easier selection. Aftewards, bind some actions to #PM button click event. Here is the code, be sure jquery is loaded.
foreach ($list as $row) {
?>
<tr class="">
<td><?php echo $row['user_name']; ?></td>
<td><?php echo $row['user_email']; ?></td>
<td>
<?php
//PHP code of getting user rank.
?>
</td>
<td>
<?php
//PHP code to chech is user active.
?>
</td>
<td>
<?php echo $row['user_registration_datetime']; ?>
</td>
<td>
<?php echo $row['user_registration_ip']; ?>
</td>
<td>
<button type="button" class="btn btn-primary data-grab-trigger" data-toggle="modal" data-target="#send-pm">#PM</button>
</td>
</tr>
<?php
}
?>
</table>
<div class="modal fade" id="send-pm" tabindex="-1" role="dialog" aria-labelledby="send-pm-label" aria-hidden="true">
<div class="modal-dialog">
<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="send-pm-label">Send personal message</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label for="name" class="control-label">Receiver name</label>
<textarea style="height:35px;" name="name" id="name" class="form-control" ></textarea>
</div>
<div class="form-group">
<label for="email" class="control-label">Email</label>
<textarea style="height:35px;" name="email" id="email" class="form-control" ></textarea>
</div>
<div class="form-group">
<label for="message" class="control-label">Message</label>
<textarea style="height:250px;" name="message" id="message" class="form-control" ></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" id="submit" class="btn btn-primary">SEND</button>
</div>
</div>
</div>
</div>
<script>
$(function (){
$('.data-grab-trigger').on('click', function (e) {
_email = $(this).closest('tr').find('td').eq(0).text(); //get user email from the row where button was clicked
_name = $(this).closest('tr').find('td').eq(1).text(); //get user name from the row where button was clicked
$("#email").val(_email);
$("#name").val(_name);
});
});
</script>

Related

how to post data from a php consult to a modal and generate a UPDATE

I am trying to pass data from an external query to a modal to be able to generate an update, but I notice that the variable of said query is not being passed to me, could you help me in what I am wrong, I am starting, my modal
<div id="content">
<button type="button" class="btn btn-danger" id= "hide" onclick="hide()">Close</button>
<form action="addcredits.php" method="POST" id="form1">
<input class="form-control" placeholder="Ingresa el total de efectivo" name="username" id="username">
<input type="button" name="btn" value="Ingresar" id="submitBtn" data-toggle="modal" data-target="#confirm-submit" class="btn btn-default" />
<div class="modal fade" id="confirm-submit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
Confirmar
</div>
<div class="modal-body">
¿La cantidad ingresada es correcta?
<table class="table">
<tr>
<th>Efectivo</th>
<td id="uname"></td>
</tr>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<input class="form-control" type="text" name="business_name" value="<?php echo $consult["id_students"]; ?>" placeholder="<?php echo $consul['id_students']; ?>">
<p><?php echo $consult["id_students"]; ?></p>
<p><?php echo $consult["id_students"]?></p>
Submit
</div>
</div>
</div>
</div>
</form>
and this is my extern consult
<?php
include("conection.php");
echo
'
<div class="container-fluid ">
<div class="row">
<div class="col-lg-12">
<table class="table table-hover">
<tr >
<th scope="col">Grupo</th>
<th scope="col">Nombre</th>
<th scope="col">Créditos</th>
</tr>
';
$accion = mysqli_real_escape_string($con,$_POST['accion']);
if($accion == 4)
{
$mi_busqueda = mysqli_real_escape_string($con,$_POST['mi_busqueda']);
$resultados = mysqli_query($con,"SELECT * FROM students WHERE
student_qr LIKE '%$mi_busqueda%'");
while($consult = mysqli_fetch_array($resultados))
{
echo
'
<tr >
<td>'.$consult["student_group"].'</td>
<td>'.$consult["student_name"]." " .$consult["student_secondname"]. " ". $consult["student_lastname"]. " ".'</td>
<td>'.$consult["student_credits"].'</td>
<td><button type="button" class="btn btn-warning" id= "show" onclick="show()" >Añadir Creditos</button></td>
</tr>
</div>
</div>
</div>
';
}
}
?>
I tried doing an echo but I don't see results, I suppose that the data remains from the external query, I don't know how I could generate said query on the same page by calling the modal instead of sending the data with javascript through JSON

Populate modal with a mysql query reult data

i have an html page that shows data populated from mysql database.
I give to user choice to edit records. I would like to use a modal form that, when pressed edit, it open up and display data result from mysql db and realated to a certain "id".
Can you help me to achieve this?
Reading on web i've understood that i have to use jquery, but i cannot find a way.
For now i tried as following:
<form method="post">
<div class="modal fade" id="edit_grp" tabindex="-1" role="dialog" aria-labelledby="edit_grpLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="edit_grpLabel">Modifica Interno</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label for="recipient-name" class="col-form-label">Sequenza Chiamata</label>
<input type="text" class="form-control" name="edit_grp_pos" id="edit_grp_pos" value="<?php echo $row[0]['grp_pos']; ?>">
</div>
<div class="form-group">
<label for="message-text" class="col-form-label">Nome</label>
<input type="email" class="form-control" name="edit_grp_name" id="edit_grp_name" value="<?php echo $row[0]['grp_name']; ?>">
</div>
<div class="form-group">
<label for="message-text" class="col-form-label">Interno</label>
<input type="tel" class="form-control" name="edit_grp_phone" id="edit_grp_phone" value="<?php echo $row[0]['grp_phone']; ?>">
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" name="submit" value="submit" id="submit_edit_grp" class="btn btn-primary" onClick="closeAll()">Modifica</button>
</div>
</div>
</div>
</div>
</form>
EDIT 1:
I'am having some tests.
For now 've changed code like this:
`
<form method="post">
<div class="modal fade" id="edit_grp" tabindex="-1" role="dialog" aria-labelledby="edit_grpLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="edit_grpLabel">Modifica Interno</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body" id="phone_details">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" name="submit" value="submit" id="submit_edit_grp" class="btn btn-primary" onClick="closeAll()">Modifica</button>
</div>
</div>
</div>
</div>
</form>
`
`
$(document).on('click', '.view_phone_data', function(){
var data_id = $(this).attr("id");
$.ajax({
url:"select.php",
method:"POST",
data:{data_id:data_id},
success:function(data){
$('#phone_details').html(data);
$('#edit_grp').modal('show');
}
});
});
`
and select.php
`
<?php
if(isset($_POST["data_id"]))
{
$output = '';
$connect = mysqli_connect("localhost","administrator","psw","db");
$query = "SELECT * FROM gruppioxe WHERE id = '".$_POST["data_id"]."'";
$result = mysqli_query($connect, $query);
$output .= '
<div class="table-responsive">
<table class="table table-bordered">';
while($row = mysqli_fetch_array($result))
{
$output .= '
<form>
<div class="form-group">
<label for="recipient-name" class="col-form-label">Ordine Chiamata</label>
<input type="tel" class="form-control" id="'.$row["grp_pos"].'" name="'.$row["grp_pos"].'">
</div>
<div class="form-group">
<label for="message-text" class="col-form-label">Nome</label>
<input type="text" class="form-control" id="'.$row["grp_name"].'" name="'.$row["grp_name"].'">
</div>
<div class="form-group">
<label for="message-text" class="col-form-label">Interno</label>
<input type="tel" class="form-control" id="'.$row["grp_phone"].'" name="'.$row["grp_phone"].'">
</div>
</form>
';
}
echo $output;
}
?>
`
But still no result..
Solved!!!
i've changed select.php as following:
`
$output .= '
<form>
<div class="form-group">
<label for="recipient-name" class="col-form-label">Ordine Chiamata</label>
<input type="tel" class="form-control" id="grp_pos" name="grp_pos" value="'.$row["grp_pos"].'">
</div>
<div class="form-group">
<label for="message-text" class="col-form-label">Nome</label>
<input type="text" class="form-control" id="grp_name" name="grp_name" value="'.$row["grp_name"].'">
</div>
<div class="form-group">
<label for="message-text" class="col-form-label">Interno</label>
<input type="tel" class="form-control" id="grp_phone name="grp_phone" value="'.$row["grp_phone"].'">
</div>
</form>
';
}
echo $output;
`

Insert data to my CRUD table

I have this AJAX code that will insert data to my CRUD table. But when I click the button that will save/insert the data to my table it will not show. All I want here is to insert my data using AJAX to my table without refreshing or using the form element in html.
Here is my code with my modal and the body of my table:
$(document).ready(function() {
// Add
$('#btn_add').click(function() {
var id = $('#brand_id').val();
var bname = $('#bname').val();
var bstatus = $('#bstatus').val();
$.ajax({
url: 'add_brand.php',
method: 'POST',
data: {
bname: bname,
bstatus: bstatus
},
success: function(data) {
$('#' + id).children('td[data-target=brand_name]').text(bname);
$('#' + id).children('td[data-target=brand_status]').text(bstatus);
}
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="modal fade" id="modal-default">
<div class="modal-dialog">
<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">Add a brand</h4>
</div>
<div class="modal-body">
<input type="hidden" id="brand_id" />
<div class="form-group">
<label for="bname">Brand Name</label>
<input type="text" class="form-control" id="bname" name="bname" placeholder="Brand Name" />
</div>
<div class="form-group">
<label for="bstatus">Status</label>
<select class="form-control select2" style="width: 100%;" id="bstatus" name="bstatus">
<option>Available</option>
<option>Not Available</option>
</select>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default pull-left" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" id="btn_add">Save Changes</button>
</div>
</div>
</div>
</div>
<tbody>
<?php
include("db_connect/connect.php");
$query = mysqli_query($con, "SELECT * FROM brand");
while ($row = mysqli_fetch_array($query)) { ?>
<tr id="<?php echo $row['brand_id']; ?>">
<td data-target="brand_name"><?php echo $row["brand_name"]; ?></td>
<td data-target="brand_status"><?php echo $row["brand_status"]; ?></td>
<td>
<a data-role="edit" data-id="<?php echo $row['brand_id']; ?>" class="btn btn-success"><i class="fa fa-edit"></i> Edit</a>
<i class="fa fa-trash-o"></i> Delete
</td>
</tr>
<?php
}
?>
</tbody>

Bootstrap Modal Submit form

I've been searching all over and trying different combinations. I will try to explain exactly what I need. I have a table populated with SQL data, last column is an Edit button to open a bootstrap modal. I've been able to populate the table and create the edit button to pass the row id into the modal for the query on the modal populate all inputs with actual data on DB. Everything is working. But now I can't even make a POST on the form, I hit the button and nothing happens.
<?php
require 'style/header.php';
require 'core/db_connect.php';
?><div class="main">
<div class="row">
<table class= "table table-striped table-bordered table-hover">
<thead>
<tr>
<th colspan="1" rowspan="1" style="width: 180px;" tabindex="0">BI/CC</th>
<th colspan="1" rowspan="1" style="width: 220px;" tabindex="0">Name</th>
<th colspan="1" rowspan="1" style="width: 288px;" tabindex="0">Supplier Number</th>
<th colspan="1" rowspan="1" style="width: 40px;" tabindex="0">Actions</th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT bicc, name, supplier_number ";
$query .= "FROM ext_work_risk ";
$result = sqlsrv_query($dbhandle, $query);
$i=0;
while($fetch = sqlsrv_fetch_array($result))
{
if($i%2==0) $class = 'even'; else $class = 'odd';
echo'<tr class="'.$class.'">
<td>'.$fetch['bicc'].'</td>
<td>'.$fetch['name'].'</td>
<td>'.$fetch['supplier_number'].'</td> <td><a class="modalButton" data-bicc="'.$fetch['bicc'].'"><button type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#modal_edit" data-container="body">Edit</button></a></td>
</tr>';
}
?>
</tbody>
</table>
</div>
</div>
<div id="modal_edit" class="modal fade" style="font-weight: normal;">
<div class="modal-dialog">
<div class="modal-content">
</div>
</div>
</div>
<?php require 'style/footer.php' ?>
<!-- Script Part -->
<script type="text/javascript">
$('.modalButton').click(function(){
var bicc = $(this).attr('data-bicc');
$.ajax({url:"modal/ajax_ext_risk_modal_edit.php?bicc="+bicc,cache:false,success:function(result){
$(".modal-content").html(result);
}});
});
</script>
Now the "modal/ajax_ext_risk_modal_edit.php" file:
<?php
$bicc = $_GET['bicc'];
//DB connect settins
require '../core/db_connect.php';
$query = "SELECT * ";
$query .= "FROM ext_work_risk WHERE bicc='$bicc'";
$result = sqlsrv_query($dbhandle, $query);
$fetch = sqlsrv_fetch_array($result);
?>
<!-- Modal -->
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Edit Co-Worker</h4>
</div>
<div class="modal-body">
<p>To edit just type the new date in the format <b>YYYY/MM/DD</b> and click save.
<form class="form-horizontal" role="form" method="POST" action="../core/process_ext_risk_modal.php">
<div class="form-group">
<label class="control-label col-sm-4">BI/CC:</label>
<div class="col-sm-7">
<input class="form-control" id="bicc" name="bicc" readonly="readonly" value="<?php echo $fetch['bicc']; ?>">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-4">Name:</label>
<div class="col-sm-7">
<input class="form-control" id="name" readonly="readonly" value="<?php echo $fetch['name']; ?>">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-4">Sup. Number:</label>
<div class="col-sm-7">
<input class="form-control" id="supplier_number" readonly="readonly" value="<?php echo $fetch['supplier_number']; ?>">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-4">LOTO:</label>
<div class="col-sm-7">
<input class="form-control" id="loto" name="loto" <?php $loto = $fetch['loto']->format('Y/m/d'); if ($loto == "2000/01/01") {echo "placeholder='Please insert date'";} else {echo "value='$loto'";} ?>>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-4">Lift Platform:</label>
<div class="col-sm-7">
<input class="form-control" id="lift_platform" <?php $lift_platform = $fetch['lift_platform']->format('Y/m/d'); if ($lift_platform == "2000/01/01") {echo "placeholder='Please insert date'";} else {echo "value='$lift_platform'";} ?>>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-default btn-success" type="submit" name="submit" value="Submit">Save</button>
</div>
When I hit Save button nothing happens. Here is the ../core/process_ext_risk_modal.php file:
<?php
include("db_connect.php");
if(isset($_POST["save"])) {
$id = $_POST['bicc'];
$data = $_POST['loto'];
if(sqlsrv_query($dbhandle, "update ext_work_risk set loto='$data' where bicc='$id'"))
echo 'success';
}
?>
This last file is just for testing of course I will be Updating much more data on the form submit.
Thank you
I just found the problem thanks to Fred -ii tips:
So here is the previous code block:
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-default btn-success" type="submit" name="submit" value="Submit">Save</button>
</div>
And now the corrected one:
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button class="btn btn-default btn-success" type="submit" name="submit" value="Submit">Save</button>
</div>
</form>
Also changing if(isset($_POST["save"])) to if(isset($_POST["submit"])). Main problem being having submit outside form and two types defined on the same button.

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

Categories

Resources