pass a value from mysql db to bootstrap modal - php

i'm new to core php coding here i'm having doubt in calling a db value to bootstrap modal popup
<td class="bgimg"><?php echo $obj["service_name"]; ?></td>
this is a line used in my code to send "service_id" which from a table and it should be displayed in the modal popup, from there, user has to fill the text fields and it should be update to another table with the same id.
here is the code of modal popup
<div id="edit-modal" class = "modal fade" tabindex="-1" role="dialog" style="padding-top: 150px;" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title" id="myModalLabel">Choose your slots</h4>
</div>
<div class="modal-body">
<p class="edit-content">service id:</p>
<form role="form1" name="loginform" method="post" class="login-form">
<div class="form-group">
<table align="center">
<tr>
<td colspan="2"><center>slot 1</center></td>
</tr>
<tr>
<td><input type="date" id="datePicker" name="slot1_dt"> </td>
<td><input type="time" name="slot1_tm"> </td>
</tr>
<tr>
<td colspan="2"><center>slot 2</center></td>
</tr>
<tr>
<td><input type="date" id="datePicker" name="slot2_dt"> </td>
<td><input type="time" name="slot2_tm"> </td>
</tr>
<tr>
<td colspan="2"><center><button type="submit" name="book" class="btn btn-default" value="book" style="font-size: 14px !important;">Book</button></center></td>
</tr>
</table>
<?php
if(isset($_REQUEST["book"]))
{
if($_REQUEST["book"])
{
$service_id=$_REQUEST["serv_id"];
$customer_id=$_REQUEST["cust_id"];
$slot1_dt=$_REQUEST["slot1_dt"];
$slot2_dt=$_REQUEST["slot2_dt"];
$slot1_tm=$_REQUEST["slot1_tm"];
$slot2_tm=$_REQUEST["slot2_tm"];
$slot1=$slot1_dt." ".$slot1_tm;
$slot1 = date("Y-m-d H:i:s",strtotime($slot1));
$slot2=$slot2_dt." ".$slot2_tm;
$slot2 = date("Y-m-d H:i:s",strtotime($slot2));
$insertqry="INSERT INTO `tblappointments`(`customer_id`, `service_id`, `preferred_slot1_date`, `preferred_slot2_date`) VALUES ('$customer_id','$service_id','$slot1','$slot2')";
mysqli_query($con, $insertqry) or die(mysqli_error($con));
}
}
?>
</div>
</form>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
it displays the result well but service_id is not passing to update it in the database
thanks in advance

use modal event listener,
$(document).ready(function(){
$('#edit-modal').on('show.bs.modal', function (e) {
var id = $(e.relatedTarget).attr('id');
alert(id);
$("#service_id").val(id);
});
});
and in modal in <form></form> add hidden input
<input type="hidden" id="service_id" name="serv_id" value="">
SideNote: Don't use $_REQUEST, instead use $_POST and escape the string.

$(document).on("click", "#upload_image", function () {
var myBookId = $(this).data('id');
$(".modal-body #activity_id___").val( myBookId );
console.log(myBookId);
// As pointed out in comments,
// it is superfluous to have to manually call the modal.
// $('#addBookDialog').modal('show');
});
<input type="hidden" name="activity_id___" id="activity_id___" value=""/>

Related

How to send a jQuery ID to Bootstrap Modal to use it in PHP function?

I know there are lots of similar posts but nothing really helps me. I'm trying to run a PHP function in Bootstrap Modal with two parameters, one of the parameter is a row ID that needs to go from jQuery.
I can send it and display it in a div id but how can I convert this parameter for a PHP function?
jQuery
$(document).ready(function() {
$("a").click(function(){
var hrefs = $(this).attr("href");
$("#modal_form").attr('action',hrefs);
var ids = hrefs.split("=").pop();
$("#sid").html(ids);
});
});
Bootstrap Modal
<!-- Update Record Modal -->
<div class="modal fade" id="updateModal" tabindex="-1" aria-labelledby="updateModalLabel" aria-hidden="true">
<div class="modal-dialog modal-xl">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="updateModalLabel">Update Translation</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form action="translation.php" id="modal_form" method="post">
<div class="modal-body">
<div id="sid"></div> /* I'm getting the right ID here*/
<?php selectedTranslation($dbh, $ids); ?> /*I need to run this function*/
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" name="update" class="btn btn-primary">Save changes</button>
</div>
</form>
</div>
</div>
</div>
php function
function getAllTranslationById($dbh, $id){
...
<?php
$sqlSelect = "SELECT * FROM xxxx";
$result = mysqli_query($dbh, $sqlSelect);
while ($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><input type="text" class="form-control" value="<?php echo $row['swrd_text'] ?>" readonly="readonly"></td>
<td><input type="text" class="form-control" value="<?php echo $row['swrd_context'] ?>" ></td>
<td><img alt="Link" src="images/edit.png" title="Update this row"></td>
</tr>
<?php }?>
</table>
<?php }
php function
function selectedTranslation($dbh, $sid){
....
<?php
$sqlSelect = "SELECT * FROM xxxx ";
$result = mysqli_query($dbh, $sqlSelect);
while ($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><input type="text" class="form-control" value="<?php echo $row['swrd_text'] ?>" readonly="readonly"></td>
<td><input type="text" class="form-control" value="<?php echo $row['swrd_context'] ?>" ></td>
</tr>
<?php }?>
</table>
<?php }?>
Its just impossible like the way you want
So what you can do instead?
Use Ajax.
<div class="modal-body">
<div id="sid"></div> /* I'm getting the right ID here*/
<div id="ajaxResult"></div>
</div>
// JS Codes
$(document).ready(function() {
$("a").click(function(){
var sid = ""; // set your `sid` here
$.ajax({
url: "/url_here", //url to your PHP page
type: "post",
data: {id: sid},
success: function(data) {
$("#ajaxResult").html(data); //result html
}
});
});
});

Required form field goes through with error instead of not letting to submit

I have made edit modal for competence levels on my website. Field from the form called effective_date is required input, but after pressing submit button it goes through even if the field is empty.
I want it to stop a user from submitting if the field is empty and show an error that this field is required.
I get this error instead of what I want:
send # jquery.min.js:2395
ajax # jquery.min.js:2310
postObject # forms.js:165
update # update.js:18
(anonymous) # update.js:10
dispatch # jquery.min.js:1265
q.handle # jquery.min.js:1234
forms.js:194 Uncaught ReferenceError: errorHandling is not defined
at Object.error (forms.js:194)
at i (jquery.min.js:823)
at Object.fireWith [as rejectWith] (jquery.min.js:851)
at A (jquery.min.js:2318)
at XMLHttpRequest.<anonymous> (jquery.min.js:2387)
I am talking about this part:
<div class="j-input">
<input type="date" name="effective_date_update"
id="effective_date_update" required>
</div>
Full edit-modal:
<div class="modal fade" id="edit-modal" tabindex="-1" role="dialog"
style="z-index: 1050; display: none;"
aria-hidden="true">
<div class="modal-dialog modal-lg" style="top:1%!important;" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Competence note</h4>
<button id="edit-competence-btn" type="button"
class="btn btn-sm btn-primary waves-effect waves-light f-right">
<i style="margin-right: 0;" class="icofont icofont-edit"></i>
</button>
<button type="button" class="close competence_cancel_btn close-note" data-dismiss="modal"
aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div id='display-competence'>
<div class="table-responsive" style="word-break: break-word;">
<table class="table m-0">
<tbody>
<tr>
<th scope="row">{{trans('personalData.effectiveDate')}}</th>
<td>{{$practitioner_competence_level->effective_date}}</td>
</tr>
<tr>
<th scope="row">{{trans('personalData.expiryDate')}}</th>
<td>{{$practitioner_competence_level->expiry_date}}</td>
</tr>
<tr>
<p style="margin-left: 0.6em; margin-top: 1em; margin-bottom: 2em" id="notesView"></p>
</tr>
</tbody>
</table>
</div>
</div>
<div id='edit-competence' style='display: none'>
<div class="j-wrapper">
<form action="" method="post" class="j-pro j-multistep" id="updateForm">
<div class="j-content"
style="">
<div class="j-row">
<div class="j-unit">
<label class="j-label">{{trans('settings.notes')}}</label>
<div class="j-input">
<textarea name="notes_update" id="notes_update"></textarea>
</div>
</div>
<div class="j-unit">
<label class="j-label">{{trans('personalData.effectiveDate')}}*</label>
<div class="j-input">
<input type="date" name="effective_date_update"
id="effective_date_update" class="effective_date_error form-control required">
</div>
</div>
#isset($practitioner_competence_level->expiry_date)
<div class="j-unit">
<label class="j-label">{{trans('personalData.expiryDate')}}</label>
<div class="j-input">
<input type="date" name="expiry_date_update" id="expiry_date_update">
</div>
</div>
#endisset
<div class="j-unit">
<h6>*{{trans('auth.requiredfield')}}</h6>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
<div class="modal-footer" style="padding-bottom: 1em;">
<button type="button" class="btn btn-default btn-round waves-effect modal-btn notes_cancel_btn"
data-dismiss="modal">{{trans('buttons.close')}}
</button>
<button type="button" id="update_final_btn" class="btn btn-success btn-round modal-btn"
data-dismiss="modal">{{trans('buttons.saveChanges')}}</button>
</div>
</div>
</div>
</div>
Controller for update:
public function updatePractitioner(Request $request, $id)
{
$this->validate($request, [
'effective_date' => 'required',
]
);
$selectSpecialtyOption = $_POST['practitioner_specialty_id_update'];
$fields = $request->all();
$specialtyfields = (['effective_date'=>$request->get('effective_date_specialty'), 'expiry_date'=>$request->get('expiry_date_specialty'), 'practitioner_specialty_id' => $selectSpecialtyOption]);
$primary_key = $this->PractitionerRepository->getIdName();
$primary_key_specialty = $this->PractitionerSpecialtyRepository->getIdName();
$practitioner_specialty_id = PractitionerSpecialty::where('practitioner_id', $id)->value('practitioner_specialty_id');
$fields[$primary_key] = $id;
$this->PractitionerRepository->update($fields);
$specialtyfields[$primary_key_specialty] = $practitioner_specialty_id;
$this->PractitionerSpecialtyRepository->update($specialtyfields);
return back()->with('successUpdate', 'Practitioner has been updated!');
}
Required attribute only work when submitted by <input type="submit" value="Submit">
This one will working properly
<form>
<input type="date" name="effective_date_update" required>
<input type="submit" value="Submit">
</form>
You need to make your own validation if you're not using the <input type="submit" value="Submit">
I Guess You are Submitting through javascript. For this You Have to validate Each field manually I've created a function which will validate All your fields having class validate
function validateForm() {
var validated = true;
$('.validate').each(function() {
if ( $(this).val() === '' )
validated = false;
});
return validated;
}
$("#update_final_btn").click(function(){
if(validateForm())
{
//Form is Valid Submit from here...
}else{
//Form is Not Valid
}
});
Hope it works. :)

Inserting Data w/ session data into database using Codeigniter

Im having trouble in troubleshooting this problem, I want to save the searched data using input function and pass the id of it into the controller but im having a problem about it,
This is my Controller:
function add($id){
$this->point_m->addStudentSection($id);
redirect('admins_view/points/index');
}
This is my Model:
function addStudentSection($id){
$arr['section_id'] = $this->session->userdata('section_id');
$arr['dateadded'] = $this->input->post('dateadded');
$arr['schoolyear'] = $this->input->post('schoolyear');
$arr['semester'] = $this->input->post('semester');
$arr['point'] = $this->input->post('point');
$this->db->where('student_id',$id);
$this->db->insert('section_student',$arr);
}
in case that model above is wrong here's my other model function that i'am also using, also i am using sqlsrv for my database.
function addStudentSection($id){
$arr['section_id'] = $this->session->userdata('section_id');
$arr['dateadded'] = $this->input->post('dateadded');
$arr['schoolyear'] = $this->input->post('schoolyear');
$arr['semester'] = $this->input->post('semester');
$arr['point'] = $this->input->post('point');
$query = $this->db->query("
INSERT INTO [dbo].[section_student]
([student_id]
,[section_id]
,[dateadded]
,[schoolyear]
,[semester]
,[point])
VALUES
('$id'
,'$section_id'
,'$dateadded'
,'$schoolyear'
,'$semester'
,'$point')
");
}
so in this model i want to use also my section_id which is an session
This is my view :
<table class="table">
<tr>
<th>ID Number</th>
<th>Firstname</th>
<th>Middlename</th>
<th>Lastname</th>
<th>Total Points</th>
<th>Action</th>
</tr>
<?php
foreach ($pt as $p) {
?>
<tr>
<!-- echo query with join -->
<td><?php echo $p->idnumber ?></td>
<td><?php echo $p->firstname ?></td>
<td><?php echo $p->middlename ?></td>
<td><?php echo $p->lastname ?></td>
<td><?php echo $p->point ?></td>
<td>
Add
Redeem
</td>
</tr>
<?php
}
?>
</table>
In this table lies all the searched and retrieve datas from my display function as you can see the button to toggle is a modal.
this is my modal:
<!-- Modal -->
<form method="post" action="<?php echo base_url();?>admins/point/add">
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Add Point/s</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<label>Date</label>
<input type="date" name="dateadded" class="form-control">
<label>School year</label>
<select name="schoolyear" class="form-control">
<option value="2018-2019">2018-2019</option>
<option value="2019-2020">2019-2020</option>
</select>
<label>Semester</label>
<select name="semester" class="form-control">
<option value="1st">First Semester</option>
<option value="2nd">Second Semester</option>
<option value="summer">Summer</option>
</select>
<label>Points</label>
<input type="text" name="point" class="form-control">
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</form>
and this is the error response..
this is the error
Hope anyone from the community can help me,.
Because your controller add($id) expected 1 parameter, then in modal view you can't set form action like this:
<form method="post" action="<?php echo base_url();?>admins/point/add">
Change your form action to this:
<form method="post" action="<?php echo base_url();?>admins/point/add/$id">
Change $id with your parameter.
Here the docs.
you should add the id at the end of form action in your view.
or you can remove it from your function in controller and post it as a hidden input
so change your view to
<form method="post" action="<?php echo base_url();?>admins/point/add/$id">
or go to your controller and change it to
function add($id = null){
if($id == null){
$id = $this->input->post('id');
}
}
then ad a new hidden input to your view right before form close tag like this
<?php echo form_hidden('id',$the_id);?>

php foreach loop editing wrong record in DB

Good day,
I've ran into a bit of a problem with my website.
I have a form which pulls a full table from my DB. Beside each record I have an 'Edit' and 'Remove' button (see image 1).
image 1
If I click on the edit button beside record 'Delete me', a modal appears and populates the value of each editable field with the current data available, and I can then edit each value and update the database. This works fine.
The problem I have; when I select one of the records where the 'Name' field is duplicated with a different 'Filling/Size' (for example, if I select 'Filling/Size' 5) it always updates and pre-populates the modal with the data from the first record in the table that shares the same 'Name'. Image 2 shows me clicking the 'Edit' button for record 1-5.
image 2
I've tried creating a foreach loop inside my foreach loop but that just duplicated every record many many times. My code is below. Any input or suggestions would be greatly appreciated.
<div class="card mb-3">
<div class="card-header">
<i class="fa fa-table"></i>Items</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered" id="dataTable" name="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Name</th>
<th>Filling/Size</th>
<th>Category</th>
<th>Description</th>
<th>Price</th>
<th>Edit/Remove</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>Filling/Size</th>
<th>Category</th>
<th>Description</th>
<th>Price</th>
<th>Edit/Remove</th>
</tr>
</tfoot>
<tbody>
<?php foreach ($result as $i => $row) { ?>
<tr>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->filling; ?></td>
<td><?php echo $row->category; ?></td>
<td><?php echo $row->description; ?></td>
<td><?php echo $row->price; ?></td>
<td id="editRemoveButtonsCell">
<button id="editButton" type="button" class="btn btn-outline-success" data-toggle="modal" data-target="#editItemModal<?php echo $row->name;?>">Edit</button>
<button id="removeButton" type="button" class="btn btn-outline-danger" data-toggle="modal" data-target="#removeItemModal<?php echo $row->name;?>">Remove</button></td>
</tr>
<!-- Delete Modal -->
<div class="modal" id="removeItemModal<?php echo $row->name;?>">
<div class="modal-dialog">
<form>
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Delete Item <?php echo $row->name?></h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
Are you sure you want to delete item <?php echo $row->name?> <?php echo $row->filling;?>? <b>This cannot be undone!</b>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<input type="hidden" value="<?php echo $row->name;?>" name="deleteItemName" id="deleteItemName">
<input type="hidden" value="<?php echo $row->filling;?>" name="deleteItemFilling" id="deleteItemFilling">
<button type="button" class="btn btn-cancel" data-dismiss="modal">Close</button>
<input type="submit" name="deleteItem" id="deleteItem" value="Delete" class="btn btn-danger" />
</div>
</div>
</form>
</div>
</div>
<!-- Edit Modal -->
<div class="modal" id="editItemModal<?php echo $row->name;?>" name="editItemModal<?php echo $row->name;?>">
<div class="modal-dialog">
<form>
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Edit Item <?php echo $row->name;?></h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
<div class="form-group">
<label for="itemNameUpdate">Name</label>
<input type="text" class="form-control" id="itemNameUpdate" name="itemNameUpdate" placeholder="Enter Item Name" value="<?php echo $row->name;?>">
<input type="hidden" value="<?php echo $row->name;?>" name="itemNameOriginal" id="itemNameOriginal">
<input type="hidden" value="<?php echo $row->filling;?>" name="itemFillingOriginal" id="itemFillingOriginal">
</div>
<div class="form-group">
<label for="itemFillingUpdate">Filling/Size</label>
<input type="text" class="form-control" id="itemFillingUpdate" name="itemFillingUpdate" placeholder="Enter Filling/Size" value="<?php echo $row->filling;?>">
</div>
<div class="form-group">
<label for="itemCategoryUpdate">Category</label>
<select class="form-control" id="itemCategoryUpdate" name="itemCategoryUpdate">
<?php echo '<option>'.$row->category.'</option>';?>
<?php foreach($categories as $category) {echo '<option>'.$category->name.'</option>';}?>
</select>
</div>
<div class="form-group">
<label for="itemDescriptionUpdate">Description</label>
<textarea class="form-control" id="itemDescriptionUpdate" name="itemDescriptionUpdate" rows="3" placeholder="Item description (optional)"><?php echo $row->description;?></textarea>
</div>
<div class="form-group">
<label for="itemPriceUpdate">Price</label>
<input type="text" class="form-control" id="itemPriceUpdate" name="itemPriceUpdate" placeholder="Enter Price" value="<?php echo $row->price;?>">
</div>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-cancel" data-dismiss="modal">Close</button>
<input type="submit" name="editItem" id="editItem" value="Edit" class="btn btn-success" />
</div>
</div>
</form>
</div>
</div>
<?php } ?>
</tbody>
</table>
</div>
</div>
<div class="card-footer small text-muted">Use the navigation bar to the left to edit the menu.</div>
</div>
</div>
All your code references are to $row->name and I would judge it to be bad practise to reference anything in the code by name. You want to be idenitifying by a unique identifier (typically a number) on each case. So you don't get this sort of ambiguity.
Image 2 shows me clicking the 'Edit' button for record 1-5.
You can't event clearly identify your own rows in this question, without needing to pull in outside data that is only co-incidentally different.
What you want is a unique identifier for each row of your database table.
This is something that is an absolute basic standard and can be read in the MySQL Dev Tutorial Here.
This SO question may also be helpful to you.
So you can update your MySQL (assuming that's your database):
ALTER TABLE users ADD uid SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
ADD INDEX (uid);
(also please see here as to which int type is best for your situation)
Once you have a unique identifier for each row, you need to then use that identifier on your code so, for example :
<input type="hidden" value="<?php echo $row->uid;?>"
name="deleteItemUniqueId" id="deleteItemUniqueId">
And
<div class="modal" id="editItemModal<?php echo $row->uid;?>"
name="editItemModal<?php echo $row->uid;?>">
And similarly apply this to your codebase throughout.
This stops any repetition of HTML ids and stops the code logic having issues with any ambiguity on the part of the identifying data given.

Ajax - Modal - Passing Variable

I know this has been asked a lot but after looking up for this for like 6 hours I can say I've tried everything that with my current knowledge (which isn't much) I can understand so I decided to come here and ask for help from the pros :).
This button is also inside korisnik.php
<button href="#myModal1" type="submit" role="button" data-toggle="modal" data-id="<?php print $row['kime']?>" class="open-myModal1"><span class="glyphicon glyphicon-edit"/></button>
This up top is a button that when clicked opens a modal and should pass a value to it, but it doesn't. After looking through a lot of posts on the net I found out easiest way is to send the variable back through AJAX so I used this:
This is located in bottom part of korisnik.php
$(document).on("click", ".open-myModal1", function ()
{
var kime = $(this).data('id');
$(".modal-body #kime").val( kime );
$.post('korisnik.php', { 'kime': kime });
});
Using $kime = $_POST['kime']; print $kime; in korisnici.php doesn't show the value at all but using <input type="text" name="kime" id="kime" value=""/> does. Problem with the one that works is that I still don't have the value needed inside a variable and can't use it to make a query based on the sent value.
So my question is:
How to make this work with ajax so I can get the value with $_POST['kime']?
or
How can I save the value inside the input field inside a variable?
I hope I've been clear enough with my request. Either one of those works since I get what I want and that is a value saved inside a variable I can use for other things.
Thanks for any help.
EDIT: This is modal Code inside korisnik.php
<div class="modal fade" id="myModal1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button class="close" data-dismiss="modal">×</button>
<center><h4 class="modal-title">EDIT KORISNIKA</h4></center>
</div><!-- end header -->
<hr/>
<div class="modal-body">
<!--<input type="text" name="kime" id="kime" value=""/>-->
<input type='text' name='kime' id='kime' value=''/>
<form class="form-horizontal" action='korisnik.php' method="post">
<?php
$kime = $_POST['kime'];
$query = $db->prepare("SELECT * FROM korisnik WHERE kime = :kime");
$query-> execute(array(':kime' => $kime ));
$result = $query->fetch();
?>
<table class="table table-striped">
<tr><th style="vertical-align:middle"><center>Korisničko Ime</center></th><td><input readonly class="form-control" name="username" value="<?php print $result['kime'] ?>"id="username" type="text"></td></tr>
<tr><th style="vertical-align:middle"><center>Ime</center></th><td><input class="form-control" onkeydown="ime(this)" onkeyup="ime(this)" onblur="ime(this)" onclick="ime(this)" name="name" id="name" value="<?php print $result['ime'] ?>" type="text"></td></tr>
<tr><th style="vertical-align:middle"><center>Prezime</center></th><td><input class="form-control" onkeydown="ime(this)" onkeyup="ime(this)" onblur="ime(this)" onclick="ime(this)" name="surname" id="surname" value="<?php print $result['prezime'] ?>" type="text"></td></tr>
<tr><th style="vertical-align:middle"><center>E-Mail</center></th><td><input class="form-control" name="email" id="email" onkeydown="emailk(this)" onkeyup="emailk(this)" onblur="emailk(this)" onclick="emailk(this)" value="<?php print $result['email'] ?>" type="email"></td></tr>
<tr><th style="vertical-align:middle"><center>Kredit</center></th><td><input class="form-control" name="kredit" id="kredit" onkeydown="alpha(this)" onkeyup="alpha(this)" onblur="alpha(this)" onclick="alpha(this)" value="<?php print $result['kredit'] ?>" type="number"></td></tr>
</table>
</div><!-- end body-->
<hr/>
<div class="modal-footer">
<input type="submit" class="btn btn-success pull-left" name="editk" value="Spremi Promjene" />
</form>
<button class="btn btn-default pull-right" data-dismiss="modal" type="button">Odustani</button>
</div><!-- end footer -->
</div><!-- end modal-content -->
</div><!-- end modal-dialog -->
</div><!-- end modal -->
So in the end after hours of what seemed endless searching and reading I found the solution to this. Instead of sending a variable to modal and doing query inside modal I did query before modal and sent all the values I needed like this.
<script type="text/javascript">
$(document).on("click", ".open-myModal1", function ()
{
var kime = $(this).data('id');
var ime = $(this).data('ime');
var prezime = $(this).data('prezime');
var email = $(this).data('email');
var kredit = $(this).data('kredit');
$(".modal-body #username").val( kime );
$(".modal-body #name").val( ime );
$(".modal-body #surname").val( prezime );
$(".modal-body #email").val( email );
$(".modal-body #kredit").val( kredit );
});
</script>
and the button I used to open modal was like this.
<button href="#myModal1" role="button" data-toggle="modal" data-id="<?php print $row['kime']?>" data-ime="<?php print $row['ime']?>" data-prezime="<?php print $row['prezime']?>" data-email="<?php print $row['email']?>" data-kredit="<?php print $row['kredit']?>" class="open-myModal1"></button>
Modal code:
<div class="modal fade" id="myModal1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button class="close" data-dismiss="modal">×</button>
<center><h4 class="modal-title">EDIT KORISNIKA</h4></center>
</div><!-- end header -->
<hr/>
<div class="modal-body">
<form class="form-horizontal" action='korisnik.php' method="post">
<table class="table table-striped">
<tr><th style="vertical-align:middle"><center>Korisničko Ime</center></th><td><input readonly class="form-control" name="username" value=""id="username" type="text"></td></tr>
<tr><th style="vertical-align:middle"><center>Ime</center></th><td><input class="form-control" onkeydown="ime(this)" onkeyup="ime(this)" onblur="ime(this)" onclick="ime(this)" name="name" id="name" value="" type="text"></td></tr>
<tr><th style="vertical-align:middle"><center>Prezime</center></th><td><input class="form-control" onkeydown="ime(this)" onkeyup="ime(this)" onblur="ime(this)" onclick="ime(this)" name="surname" id="surname" value="" type="text"></td></tr>
<tr><th style="vertical-align:middle"><center>E-Mail</center></th><td><input class="form-control" name="email" id="email" onkeydown="emailk(this)" onkeyup="emailk(this)" onblur="emailk(this)" onclick="emailk(this)" value="" type="email"></td></tr>
<tr><th style="vertical-align:middle"><center>Kredit</center></th><td><input class="form-control" name="kredit" id="kredit" onkeydown="alpha(this)" onkeyup="alpha(this)" onblur="alpha(this)" onclick="alpha(this)" value="" type="number"></td></tr>
</table>
</div><!-- end body-->
<hr/>
<div class="modal-footer">
<input type="submit" class="btn btn-success pull-left" name="editk" value="Spremi Promjene" />
</form>
<button class="btn btn-default pull-right" data-dismiss="modal" type="button">Odustani</button>
</div><!-- end footer -->
</div><!-- end modal-content -->
</div><!-- end modal-dialog -->
</div><!-- end modal -->
Thanks to all who tried to help! Till next time!
I think you mistake in syntax. You should call it like this:
$.ajax({
type: "POST",
url: "korisnik.php",
data:
{
kime: kime
}
});
You are missing a closing curly brace on your on click function.
$.post('korisnik.php', { 'kime': kime });
Above post should also work.
Retrieve it in PHP as follow:
$kime = $_POST['kime'];

Categories