I've a table which contains two columns. I created a jQuery context menu with two entries: edit and delete.
If I perform a right click on a row and click on edit button, I'd like to open my bootstrap modal and pass data into it.
Here's my context menu code:
<script type='text/javascript'>
window.onload=function(){
$(function() {
$(".context-menu-one").contextMenu({
selector: 'td',
callback: function(key, options) {
switch (key) {
case 'edit':
var content = $(this).data('id');
$('#myModal').modal('show');
break;
case 'delete':
break;
}
},
items: {
"edit": {name: "Modifier", icon: "edit"},
"delete": {name: "Supprimer", icon: "delete"},
}
});
$('.context-menu-one').on('click', function(e){
console.log('clicked', this);
})
});
}
</script>
And here's my modal code:
<div class="modal fade" id="myModal" 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">Test</h4>
</div>
<form class="my-form">
<div class="modal-body">
<label>SDA
<input type="text" id="sda" name="sda" value="" required="required" class="form-control col-md-7 col-xs-12">
</label>
<label>Service
<input type="text" id="service" name="service" value="" required="required" class="form-control col-md-7 col-xs-12">
</label>
<label id="label-id"></label>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Clode</button>
<button type="button" class="btn btn-primary" id="add">Add</button>
</form>
</div>
</div>
</div>
Here's PHP code to retrieve data:
$reponse = $bdd->query("SELECT * FROM table");
while ($donnees = $reponse->fetch()) {
echo '
<tr>
<td data-id="'.$donnees[0].'">' . $donnees[1] . '</td>
<td data-id="'.$donnees[0].'">' . $donnees[2]. '</td>
</tr>
';
}
Can someone tell me which is the best way to do this?
I've found a solution, here's updated code:
<script type='text/javascript'>
window.onload=function(){
$(function() {
$(".context-menu-one").contextMenu({
selector: 'td',
callback: function(key, options) {
switch (key) {
case 'edit':
sda = $(this).parent().find("td:eq(0)").text();
service = $(this).parent().find("td:eq(1)").text();
$("#sda").val(sda);
$("#service").val(service);
$('#myModal').modal('show');
break;
case 'delete':
break;
}
},
items: {
"edit": {name: "Modifier", icon: "edit"},
"delete": {name: "Supprimer", icon: "delete"},
}
});
$('.context-menu-one').on('click', function(e){
console.log('clicked', this);
})
});
}
</script>
Related
I have a modal I opened with Ajax and I can pull the information from the database. My problem is this: I can only pull data into the input. Any div, p tags or ckeditor etc. I can't pull data into it. What can I do about this? In the meantime, I do not have problems withdrawing data from the database. I can pull the data. I'm looking at Console. But I can only pull it into the input.
index.php
<div class="modal fade edit" id="edit-modal">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title pl-3" id="exampleModalScrollableTitle">Edit Test</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true" class="text-white">×</span>
</button>
</div>
<form method="post" id="frmedit" enctype="multipart/form-data">
<div class="modal-body pre-scrollable">
<input type="text" name="id" id="id" class="form-control">
<p id="asd"></p>
<div class="form-group form-float mt-3">
<div class="form-line">
<input type="text" class="form-control" name="title" id="title">
<label class="form-label">Title</label>
</div>
</div>
<div class="form-group form-float">
<label class="form-label">CKeditor</label>
<textarea name="test1"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn bg-grey waves-effect" data dismiss="modal">Close</button>
<input type="submit" id="update" class="btn bg-purple waves-effect" value="Save">
</div>
</form>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function () {
$(document).on('click' , '.bn-edit' ,function(){
var id = this.id;
console.log("id:"+id+",type:test");
$.ajax({
type: "GET",
url: "read.php?id="+id+"&type=test",
success:function(response){
$("#edit-modal").modal('show');
$.each(response, function(key, value) {
console.log("key:"+key+",value:"+value);
})
$("#title").val(title);
$("#test1").html(test1);
$("#asd").val(asd);
$("#id").val(id);
}
});
});
});
function loaddata() {
$.ajax({
url: 'read.php',
type: 'post',
data: {"type":"test_all"},
success:function(response){
$("#container").html(response);
}
});
}
$(function () {
CKEDITOR.replace('test1');
CKEDITOR.config.height = 125;
});
read.php:
$id = $_REQUEST["id"];
switch($_REQUEST["type"]) {
case "test":
if(isset($_REQUEST["id"])) {
$result = $conn->read("test", " id='$id' ", "", "", "");
if(!empty($result)) {
$responseArray["title"] = $result[0]["title"];
$responseArray["asd"] = $result[0]["asd"];
$responseArray["test1"] = $result[0]["test1"];
$responseArray["id"] = $result[0]["id"];
echo json_encode($responseArray);
}
}
break;
case "test_all":
$result = $conn->verioku("id,title,asd", "test", "id");
break;
default:
break;
}
The problem solved. I added just this code:
CKEDITOR.instances.ic_ozet.setData(response.ic_ozet);
Thank you so much for your comments.
$(document).on('click' , '.bn-edit' ,function(){
var id = this.id;
console.log("id:"+id+",type:test");
$.ajax({
type: "GET",
url: "read.php?id="+id+"&type=test",
success:function(response){
$("#edit-modal").modal('show');
$.each(response, function(key, value) {
console.log("key:"+key+",value:"+value);
})
$("#title").val(title);
CKEDITOR.instances.test1.setData(response.test1);
$("#asd").html(asd);
$("#id").val(id);
}
});
});
Hi I am totally a newbie in php and i found a select2 to automatically populate dropdown, I would like to use it inside modal, first select2 inside its modal is working but the second one isnt working, here is my code for the FIRST ONE (Working)
HTML:
<div id="prevprojmodal" class="modal fade" role="dialog" data-keyboard="false" data-backdrop="static">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
<select class="substat form-control" style="width:500px" name="substat" style="min-width: 100%"></select>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary">Save</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
JS
<script type="text/javascript">
$('.substat').select2({
allowClear: true,
placeholder: 'Type to search for previous project',
ajax: {
url: 'ajax/getdata_previousproject.php',
dataType: 'json',
delay: 250,
processResults: function (data) {
return {
results: data
};
},
cache: true
}
});
$('.substat').on('change', function() {
var data = $(".substat option:selected").text();
$("#projectprevioustest").val(data);
})
</script>
And this is the code of the second one, but NOT WORKING:
HTML
<!-- Add Team Compo Modal -->
<div class="modal fade" id="addteamcompomodal">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Add a member to Team</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
<div class="form-group">
<label> Staff Name </label>
<select class="staffname form-control" name="staffname" id="staffname" placeholder="Staff Name" style="width: 100%; padding: 0;"></select>
</div>
<div class="form-group">
<label> Percent Involvement </label>
<input type="text" name="teampi" id="teampi" placeholder="Percent Involvement" class="form-control">
</div>
<div class="form-group">
<label> Specific Duties & Responsibilities </label>
<input type="text" name="teamsdnr" id="teamsdnr" placeholder="Specific Duties & Responsibilities" class="form-control">
</div>
<div class="form-group">
<label> Specific Deliverables </label>
<input type="text" name="teamsd" id="teamsd" placeholder="Specific Deliverables" class="form-control">
</div>
<div class="form-group">
<label> Project Role </label>
<select required="" name="teampr" class="form-control" style="font-size: 12px;">
<?php
$resultatc = $pdo->prepare("SELECT * FROM teamroles");
$resultatc->execute();
$checkRtc = $resultatc->rowCount();
if ($checkRtc > 0) {
echo "<option disabled='disabled' value = '' selected>SELECT</option>";
foreach ($resultatc->fetchAll(PDO::FETCH_BOTH) as $disptc) {
for ($z = 0; $z < count($checkRtc); $z++) {
$disp_idtc = $disptc['tr_id'];
$disp_statustc = $disptc['tr_abb'];
}
echo "<option value='" . $disp_idtc . "'>" . utf8_encode($disp_statustc) . "</option>";
}
}
?>
</select>
</div>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal" onclick="addRecord()">Save</button>
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- Add Team Compo Modal END-->
JS
<script type="text/javascript">
$(document).ready(function() {
$('.staffname').select2({
dropdownParent: $('#addteamcompomodal')
allowClear: true,
placeholder: 'Type to Search for Staff',
ajax: {
url: 'ajax/getdata_staffteamcomp.php',
dataType: 'json',
delay: 250,
processResults: function (data) {
return {
results: data
};
},
cache: true
}
});
</script>
What should I be doing wrong? Thank you in advance.
I would like to check bootstrap modal form validation before call ajax request , i have used following script for it but validation is not work proper and also after filled form and submit form submitHandler is not called or ajax call is not work.
Script
jQuery(document).ready(function(){
jQuery('.modal').on('show.bs.modal', function (e) {
jQuery("#agent_form").validate({
rules: {
vname: {
required: true,
minlength: 8
},
action: "required"
},
messages: {
vname: {
required: "Please enter some data",
minlength: "Your data must be at least 8 characters"
},
action: "Please provide some data"
},
submitHandler: function(form) {
var ajaxurl = jQuery("#ajaxurl").val();
var name =jQuery('#inputName').val();
var phone_number =jQuery('#phone_number').val();
var useId =jQuery("#data_id").val();
jQuery.ajax({
url : ajaxurl,
type : 'post',
data : {
action : 'add_new_agent',
ag_id : useId,
ag_name : name,
phone_number:phone_number
},
success : function( response ) {
if(response =='Success'){
jQuery('#success_message').fadeIn().html(response);
jQuery("#success_message").addClass("alert alert-success");
setTimeout(function(){
jQuery("#success_message").addClass("alert alert-success");
jQuery('#success_message').fadeOut("Slow");
jQuery('#myModal').modal('hide');
}, 2000);
}
}
});
return false;
}
});
});
});
HTML Code...
<div class="modal fade" id="myModal" role="dialog">
<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">Modal Header</h4>
<div id="error_message" class="ajax_response1" style="float:left"></div>
<div id="success_message" class="ajax_response1" style="float:left"></div>
</div>
<!-- Modal Body -->
<div class="modal-body">
<form role="form" id="agent_form">
<input id="ajaxurl" name="action" value="<?php echo admin_url( 'admin-ajax.php' );?>" type="hidden">
<input type="hidden" name="data_id" id="data_id">
<div class="form-group">
<label for="inputName">Name</label>
<input name="vname" type="text" class="form-control" id="inputName" placeholder="Enter your name" required/>
</div>
<div class="form-group">
<label for="inputEmail">Phone Number</label>
<input type="phone_number" class="form-control" id="phone_number" placeholder="Enter your email"/>
</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-primary submitBtn" id="add_agent" >SUBMIT</button>
</div>
</div>
</div>
</div>
Validation is not work and also submitHandler not call.
There are few mistakes in your code,
Submit button should be inside the "form", not outside
button type should be "submit" not button
I have edited your code, use the following
jQuery(document).on('show.bs.modal','#myModal', function (e) {
jQuery("#agent_form").validate({
rules: {
vname: {
required: true,
minlength: 8
},
action: "required"
},
messages: {
vname: {
required: "Please enter some data",
minlength: "Your data must be at least 8 characters"
},
action: "Please provide some data"
},
submitHandler: function(form) {
var ajaxurl = jQuery("#ajaxurl").val();
var name =jQuery('#inputName').val();
var phone_number =jQuery('#phone_number').val();
var useId =jQuery("#data_id").val();
jQuery.ajax({
url : ajaxurl,
type : 'post',
data : {
action : 'add_new_agent',
ag_id : useId,
ag_name : name,
phone_number:phone_number
},
success : function( response ) {
if(response =='Success'){
jQuery('#success_message').fadeIn().html(response);
jQuery("#success_message").addClass("alert alert-success");
setTimeout(function(){
jQuery("#success_message").addClass("alert alert-success");
jQuery('#success_message').fadeOut("Slow");
jQuery('#myModal').modal('hide');
}, 2000);
}
}
});
return false;
}
});
});
<!-- The below scripts and styles are for demo purpose, no need to include in your code -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.js"></script>
<!-- The above scripts and styles are for demo purpose, no need to include in your code -->
<button data-toggle="modal" data-target="#myModal">demo</button>
<div class="modal fade" id="myModal" role="dialog">
<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">Modal Header</h4>
<div id="error_message" class="ajax_response1" style="float:left"></div>
<div id="success_message" class="ajax_response1" style="float:left"></div>
</div>
<!-- Modal Body -->
<div class="modal-body">
<form role="form" id="agent_form">
<input id="ajaxurl" name="action" value="<?php echo admin_url( 'admin-ajax.php' );?>" type="hidden">
<input type="hidden" name="data_id" id="data_id">
<div class="form-group">
<label for="inputName">Name</label>
<input name="vname" type="text" class="form-control" id="inputName" placeholder="Enter your name" required/>
</div>
<div class="form-group">
<label for="inputEmail">Phone Number</label>
<input type="phone_number" class="form-control" id="phone_number" placeholder="Enter your email"/>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary submitBtn" id="add_agent" >SUBMIT</button>
</div>
</form>
</div>
</div>
</div>
</div>
This is a little different from the method of submitting a form via static form value in modal. Using the same method is not working because AJAX not submitting the form value from the shown modal. So,
html
load form
modal
<div class="modal fade" id="xModal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="xModalLabel">loading...</h4>
</div>
<div class="modal-body">...</div>
<div class="modal-footer">
<button type="button" class="btn btn-link waves-effect">SAVE CHANGES</button>
<button type="button" class="btn btn-link waves-effect" data-dismiss="modal">CLOSE</button>
</div>
</div>
</div>
</div>
jquery
//clear modal
$('body').on('hidden.bs.modal', '.modal', function () {
$(this).removeData('bs.modal');
});
//submit form
$('#xModal').on('shown.bs.modal',function (e) {
e.preventDefault();
var form=$(this).find('form').serialize();
$('#save_btn').on('click',function(){
$.ajax({
url:'inc/data.php',
type:'POST',
data:form,
success : function(data){
console.log(data);
}
});
});
});
data.php
<?
print_r($_POST);
?>
remote_form.html
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="xModalLabel">Reg Form</h4>
</div>
<div class="modal-body">
<form>
<input type="text" name="fname" /><br />
<input type="text" name="lname" /><br />
<input type="text" name="email" />
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-link waves-effect" id="save_btn">SAVE</button>
</div>
</div>
Thanks, any comment is welcome!
Delegate your click event, move the event outside the on('shown.bs.modal') event
$(document).on('click', '#save_btn',function(){
var form=$(this).closest('form').serialize();
$.ajax({
url:'inc/data.php',
type:'POST',
data:form,
success : function(data){
console.log(data);
}
});
});
You can set ids to input fields and then getting data from that id as follows.
<input type="text" name="fname" id="fname"/>
and on the script do like this.
//submit form
$('#xModal').on('shown.bs.modal',function (e) {
e.preventDefault();
$('#save_btn').on('click',function(){
var fname = $("#fname");
$.ajax({
url:'inc/data.php',
type:'POST',
data:{
fname = fname
},
success : function(data){
console.log(data);
}
});
});
});
I am using php mysql to insert to the database and that was working, however the bootstrap modal won't let me submitting another item. The button still says inserting and the last inserted data is still showing in the inputs.
I want to insert multiple times as this becomes part of a larger edit form.
This is put outside of the edit form submit button:
<div id="add_data_Modal" class="modal fade" tabindex="-1" role="dialog">
<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 New Item</h4>
</div>
<div class="modal-body">
<form method="post" id="insert_form">
<input type="hidden" name="job_id" id="job_id" value="<?php echo $job_id;?>">
<input type="hidden" class="form-control" id="job_mat_qty" name="job_mat_qty" value="1" />
<div class="form-group">
<label for="job_mat_desc" class="col-sm-2 control-label">Description</label>
<div class="col-sm-10">
<textarea class="form-control editors" id="job_mat_desc" name="job_mat_desc" autocomplete="off" placeholder="Enter item title and / or description" />
</textarea>
</div>
</div>
<div class="form-group">
<label for="job_mat_cost" class="col-sm-2 control-label">Price</label>
<div class="col-sm-10">
<div class="input-group input-group"> <span class="input-group-addon"><?php echo CURRENCY ?></span>
<input type="text" class="form-control calculate invoice_product_price txt" autocomplete="off" id="job_mat_cost" name="job_mat_cost" aria-describedby="sizing-addon1" placeholder="0.00" />
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-10"> </div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<input type="submit" name="insert" id="selected" value="Insert" class="btn btn-success" />
</div>
</form>
</div>
</div>
</div>
$(document).on('click', '.delete-rows', function(){
var id = $(this).attr("id");
if (confirm("Are you sure you want to delete this Item?")) {
$.ajax({
type: "POST",
url: "job_remove_material_new.php",
data: ({
id: id
}),
cache: false,
success: function(html) {
$(".delete_mem" + id).fadeOut('slow');
}
});
} else {
return false;
}
});
$('#insert_form').on("submit", function(event){
event.preventDefault();
if($('#job_mat_desc').val() == "")
{
alert("Description is required");
}
else
{
$.ajax({
url:"add_material.php",
method:"POST",
data:$('#insert_form').serialize(),
cache: false,
beforeSend:function(){
$('#insert').val("Inserting");
},
success:function(data){
$('#insert_form')[0].reset();
$('#add_data_Modal').modal('hide');
$('#invoice_table').html(data);
}
});
$(function () {
$(document).on("hidden.bs.modal", "#add_data_Modal", function () {
$('#insert_form')[0].reset();
});
});
}
});
Sorry I have no clue how to add this code correctly for this forum so please be kind....
Thanks
You should try like this
$('#insert_form').on("submit", function(event){
event.preventDefault();
if($('#job_mat_desc').val() == "")
{
alert("Description is required");
}
else
{
$.ajax({
url:"add_material.php",
method:"POST",
data:$('#insert_form').serialize(),
cache: false,
beforeSend:function(){
$('#insert').val("Inserting");
},
success:function(data){
//$('#insert_form')[0].reset();
$('#insert_form input[type="text"],textarea').val(''); // here is change
$('#add_data_Modal').modal('hide');
$('#invoice_table').html(data);
}
});
In my case it will work successfully so I hope it will help you