How can I load server data into bootstrap modal? - javascript

My bootstrap modal component submit button calls server and retrieves the data. Now I need to show the data into another modal component.
My code:
index.html
calling code for firstmodal:
Edit Button Employee
<div id="myEditModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myEditModal" data-dismiss="modal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<div class="modal-body">
<form class="form-horizontal" role="form">
<br/><br/>
<div class="control-group">
<label class="control-label" for="value">Employee Id:</label>
<div class="controls">
{{input type="text" class="input-medium" value=id }}
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary" {{action "editIdAction" target="controller" }}>Save</button>
</div>
</div>
</div>
</div>
in app.js:
editIdAction: function () {
var id = this.get('id');
var controller = this;
data = $.ajax({
type: "POST",
dataType: "json",
url: "/editid?id=" + id,
async: false}).responseJSON;
console.log(data)
controller.set("id", "");
this.transitionToRoute('editprofile', data)
}
Now how can I forward route to modal.
And I create another modal with id = editprofile, but it is not working. the modal is not popup.
I search in google and I found modal to modal interaction.
But I didn't find response modal to modal.
Could someone help?

Related

How to ask for user input and return results in the same pop up box?

Flow is as below:
1. User clicks on a button and a pop up box will be displayed
2. User will be required to enter a pincode in the pop up box
3. Server will search the database and return the results
4. Display the results in the same pop up box as step 1.
I am currently facing difficulty in step 4 which is displaying the results in the same pop up box.
html code for pop up box:
<!-- Modal -->
<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">Enter Pincode</h4>
</div>
<div class="modal-body">
<p>Please request for the pincode from the customer.</p>
<form class="form-horizontal" method="POST" action="/users/home/dashboard" role="form">
<div class="form-group">
<label class="col-sm-2 control-label">Pincode:</label>
<div class="col-sm-10">
<input type="number" class="form-control"
name="pinCode" placeholder="Pincode from customer"/>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Confirm</button>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
When user clicks on submit, I will run the node.js code below:
var sql = "SELECT * FROM customers WHERE pincode = ?";
db(function(err,conn){
conn.query(sql,[pinCode],function(err,results){
if(err){
console.log("Error at dashboard:" + err);
//res.render('dashboard',{data:results});
}else{
console.log(results);
}
});
});
The question is how can I return the results from server which contains customer details in the same pop up box ?
Instead POSTing using form do it using AJAX. On .success get PIN and change modal text using data returned.
Example (add some ID to your form, for example "form-horizontal1", and call POST on button click:
$.post( "/users/home/dashboard", $("#form-horizontal1").serialize(), function( data))
.done(function(data) {
$(".modal-body").html(data);
});

load content to modal after form submit

I want to load content to a result div that is located inside a modal-body after form is submitted and after modal is shown but i can't figure out the problem. when i want to load content to a result div outside modal it works, but when i use the result div inside a modal it doesn't.
this is my code so far:
html:
<div class="col-lg-12">
<div class="row">
<div id="form-content">
<form method="post" id="reg-form" autocomplete="off" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div id="monetarylayout" class="form-group">
<input class="form-control number" type="text" value="0" name="subjectvalue" id="txt_subjectvalue">
</div>
<div class="form-group">
<button class="btn btn-primary">submit</button>
</div>
<input class="form-control number" type="text" value="0" name="coinquantity" id="txt_coinquantity" style="display:none"> <!--just to make the form post work-->
</form>
</div>
<!-- Modal -->
<div id="myModal" class="modal fade" 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>
<div class="modal-body">
<p>Some text in the modal.</p>
<div id="result"></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
jquery:
<script type="text/javascript">
$(document).ready(function() {
// submit form using $.ajax() method
$('#reg-form').submit(function(e){
e.preventDefault(); // Prevent Default Submission
$.ajax({
url: 'testcontroller.php',
type: 'POST',
data: $(this).serialize() // it will serialize the form data
})
.done(function(data){
$('#myModal').modal(), function() {
// do something when the modal is shown
$('#result').fadeIn('slow').html(data);
}
})
.fail(function(){
alert('Ajax Submit Failed ...');
});
});
});
</script>
i figured out myself. i should have added these attributes to my form button:
<button class="btn btn-primary" data-toggle="modal" data-target="#myModal">submit</button>
and edited this function as follows:
.done(function (data) {
$('#result').fadeIn('slow').html(data);
})

css selectors, set property for all except two tags

i have a modal and i try to implement nested comments
now i want to send the parent comment id to child comment in modal
button that show modal:
<button class="btn btn-xs btn-light" data-target="#commentModal" data-toggle="modal" data-parent="3">پاسخ</button>
<div class="modal fade" id="commentModal" tabindex="-1" role="dialog" aria-labelledby="commentModal" aria-hidden="true" dir="rtl">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
<h4 id="modal-label-3" class="modal-title">ارسال نظر</h4>
</div>
<div class="modal-body">
<div class="respond-form" id="respond" style="padding-top: 0">
<form action="/comment" class="form-gray-fields">
{{ csrf_field() }}
<input type="hidden" name="parent_id" value="0">
<br>
<div class="row">
<div class="col-md-12">
<div class="form-group text-center">
<button class="btn btn-block" type="submit">ثبت دیدگاه</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
i want to send value of data-parent in button, to "vlaue" attribute of input in modal
i see this for bootstrap modals:
$('#commentModal').on('show.bs.modal' , function (event) {
let button = $(event.relatedTarget);
let parentId = button.data('parent');
let modal = $(this);
modal.find("[name='parent_id']").val(parentId);
});
but i do not use bootstrap and following code not working for me!
How about something like this?
$(".btn").on("click", function(){
$(".modal-body").find("[name=parent_id]").val($(this).data("parent"));
// Code to show modal
});

Bootstrap modal: dynamically load value in input field

I have a Bootstrap modal in a Laravel app that I'm trying to dynamically load. The idea is to fill the input fields in the modal with data coming from my database so I can update the values. I'm almost there....
I have the following in my view file:
Open modal
The 'data-id' and 'data-title' are passed to a JS function that looks as follows:
$(function() {
$('#edit-auction-modal').on("show.bs.modal", function (e) {
$("#auctionLabel").html('Edit auction with id '+ $(e.relatedTarget).data('id'));
$("#auctionTitle").html($(e.relatedTarget).data('title'));
});
});
This JS function captures the id and title successfully. In the below modal view, I can print out the id in the panel-title because I'm telling in JS to put the data in the id="auctionLabel".
<div class="row">
<div id="edit-auction-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="panel panel-info">
<div class="panel-heading">
<div class="panel-title" id="auctionLabel"></div>
</div>
</div>
<div class="modal-body edit-content" style="padding-top:10px">
<form method="POST" action="{!! route('admin_auctions_update') !!}">
{{ csrf_field() }}
<div class="form-group">
<label for="auction_name" class="control-label">Auction name</label>
<input name="auction_name" id="name" class="form-control" placeholder="Auction name" value="{{$auction->auction_name}}">
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-success">Update auction</button>
</div>
</div>
</div>
</div>
</div>
My question is how I need to change the Javascript function in order to pre-fill the input fields with the value of the title. In other words, I want the value of the input box to contain the 'title' that I captured in the JS file.
Try this :
$('#edit-auction-modal').on("show.bs.modal", function (e) {
$("#auctionLabel").html('Edit auction with id '+ $(e.relatedTarget).data('id'));
$("#auctionTitle").html($(e.relatedTarget).data('title'));
$('#edit-auction-modal ').find('input#input-id').val($(e.relatedTarget).data('title'))
});
You can use .val( value ) to set/change the input value in jQuery.
http://api.jquery.com/val/#val2

Use Handlebar template with bootstrap modal

I have a bootstrap modal with handlebar script-
<script id="data-template" type="x-handlebars-template">
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
×
</button>
<h4 class="modal-title" id="myModalLabel">Add Data</h4>
</div>
<div class="modal-body">
<form class="form-horizontal" id="form" enctype="multipart/form-data" action="upload.php" method="POST">
<fieldset>
<div class="control-group">
<label class="control-label" for="input01">Message</label>
<div class="controls">
<textarea class="input-xlarge" id="input01" name="text" value="{{value}}"></textarea>
</div>
</div>
</fieldset>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary" name="submit" value="submit">
Save
</button>
<button name="cancel" value="cancel" class="btn btn-default" data-dismiss="modal">
Cancel
</button>
</div>
</form>
</div>
</div>
</div>
</script>
now on a button click I am opening the modal as well as onClick method is calling getDataWithId() method to fill data in the modal-
<b>+</b> Add
and this my getDataWithId() method.
function getDataWithId(id){
var newData;
var theTemplateScript = $("#data-template").html();
var theTemplate = Handlebars.compile (theTemplateScript);
$.get("http://localhost:8092/data_service.php?method=news&id="+id,
function(data, textStatus, jqXHR) {
newData= $.parseJSON(JSON.stringify(data.data.posts));
$(".modal").append (theTemplate(newData));
}).fail(function(jqXHR, textStatus, errorThrown) {
});
}
Data is not showing on the input field though modal is opening perfectly.

Categories