Dynamic Bootstrap Modal with php and jQuery - php

I have multiple information boxes, with short info echoed from the database. at the bottom of each box is "more information" when click this triggers a modal.
The issue: at the moment when "more information" is clicked it displays the same information regardless of which info box was clicked.
The objective: When more information is clicked i need the respective information to be displayed. Some how referencing that row number X needs to be displayed when "more information" is click on that information box.
I tried is, but it didn't work:
jQuery(document).ready(function($) {
$('.moreinfo').click(function(event) {
var a = $(this);
// Get ID
var currentID = parseInt(a.data('id'));
$.ajax({
type : 'post',
url : 'ajax.php', // in here you should put your query
dataType: 'json',
data: {'vac_id': currentID}, // here you pass your id via ajax .
// in php you should use $_POST['post_id'] to get this value
success : function(json)
{
// Change the ID in modal
var m = $('#moreinfo'), mBody = m.find('.panel-body');
// Update new content
$('#vac_id').text(json.vac_id);
$('#vac_post_date').text(json.vac_post_date);
$('#vac_job_title').text(json.vac_job_title);
$('#vac_comp_name').text(json.vac_comp_name);
$('#job_description').text(json.job_description);
$('#vac_ess_one').text(json.vac_ess_one);
$('#vac_ess_two').text(json.vac_ess_two);
$('#vac_ess_three').text(json.vac_ess_three);
$('#vac_ess_four').text(json.vac_ess_four);
$('#vac_ess_five').text(json.vac_ess_five);
$('#vac_ess_six').text(json.vac_ess_six);
$('#vac_ess_seven').text(json.vac_ess_seven);
$('#vac_ess_eight').text(json.vac_ess_eight);
$('#vac_ess_nine').text(json.vac_ess_nine);
$('#vac_ess_ten').text(json.vac_ess_ten);
$('#vac_des_one').text(json.vac_des_one);
$('#vac_des_two').text(json.vac_des_two);
$('#vac_des_three').text(json.vac_des_three);
$('#vac_des_four').text(json.vac_des_four);
$('#vac_des_five').text(json.vac_des_five);
$('#vac_des_six').text(json.vac_des_six);
$('#vac_des_seven').text(json.vac_des_seven);
$('#vac_des_eight').text(json.vac_des_eight);
$('#vac_des_nine').text(json.vac_des_nine);
$('#vac_des_ten').text(json.vac_des_ten);
$('#vac_deadline').text(json.vac_deadline);
$('#apply_link').text(json.apply_link);
m.modal('show');
}
});
event.preventDefault();
});
});
Here you can see the modal and modal trigger: FIDDLE
Any help is genuinely appreciated.

Here's how I used to create dynamic modal/dialog:
var modal = $('#mymodal');
modal.find('.modal-title').text(newTitle); // Change modal title (optional)
modal.find('.modal-body').html(somethingNew); // Change modal content
modal.show();
For example: https://jsfiddle.net/3ey8tyz3/

I suppose when you load modal for the first time it gets cached and every time returns the same content.
You might need to clear the modal first and then reload on the basis of your id.
You can do-
$('#myModal').on('loaded.bs.modal',function(e){
}).on('hidden.bs.modal',function(e){
$(this).removeData('bs.modal');
});
Wrap this into a function and call it on document.ready

Related

jquery $.post data to popup window from a popup window

I'm trying to post data to a new window (using window.open) from a popup that was opened with the window.open method. What I'm trying to do is pass the selected option value to the newly opened window and load data using $_POST.
Here's what I tried:
$(document).on('click', '.openForm', function()
{
var select = $(this).data('select'),
val = $('#'+ select).val();
$.post('/path/to/page.php', {id: val}, function(res)
{
window.open('/path/to/page.php');
});
});
and currently page.php has a var_dump of $_POST which returns empty. It also opens the page in a new tab instead of a new window - I think this is to do with giving the open windows unique names?
I'm not sure how to get it so $.post works with posting to the same page and opening it with the sent data - any links or solutions you know of that could work?
Thanks :)
Modify your script like this:
<script type="text/javascript">
$(document).on('click', '.openForm', function()
{
var select = $(this).data('select'),
val = $('#'+ select).val();
$.post('/path/to/page.php', {id: val}, function(res)
{
console.log(res);
var myWindow = window.open("", "MyWindow", "width=600,height=600");
myWindow.document.write(res);
//window.open('test_json.php',1,'width=600, height=600');
});
});
</script>
1) var_dump of $_POST returns empty. Because your both request $.post('/path/to/page.php') and window.open('/path/to/page.php'); will be different. 1st treated as post and after completion of it window.open('/path/to/page.php'); will be arise which will be get request.
2) To open window on same not in new tab you have to pass its width and height as 3rd parameter in window.open() method.

RangeError: Maximum call stack size exceeded. Ajax Request on Click

I'm trying to build a filter for a table of data that is collected from the database with an AJAX request (in Wordpress). There are 2 dropdown menus and a submit button. The problem is when I click on the submit button my browser freezes and I get the error: RangeError: Maximum call stack size exceeded.
What's causing this?
The code loads the table when the page is loaded (including the dropdown menus and the submit button). Then once it's loaded it loads the 'click submit button' function.
<script>
(function($
jQuery(document).ready(function($){
var amount = 10; //get amount of results to show per page
$.post("<?php echo get_stylesheet_directory_uri(); ?>/ajax_table.php", {
manual_filter: 1,
//sector_id : '<?php echo $sector_id; ?>', //send the data to the page using this format
amount : amount //send the data to the page
}, function(data) {
// data will hold the output from the script.php
$("#table").html(data); //update the div with the output
$("#submit").click(function (){
var sector = document.getElementById("sector_dropdown");
var year = document.getElementById("year_dropdown");
$.post("<?php echo get_stylesheet_directory_uri(); ?>/ajax_table.php", {
manual_filter: '1',
sector_id: sector,
year: year,
amount : amount //send the data to the page using this format
}, function(data) {
// data will hold the output from the script.php
$("#table").html(data); //update the div with the output
});
});
});
});
})(jQuery);
</script>
<div id="table">
</div>
The problem is when I click on the submit button my browser freezes
and I get the error: RangeError: Maximum call stack size exceeded.
What's causing this?
$("#table").html(data); loads duplicate elements having id #submit ? When #submit clicked, all duplicate #submit elements click handlers called ? Results in adding more duplicate #submit elements with click events attached , calling click handlers of all duplicate #submit elements .
Note, ids should be unique within document .
click event could be delegated to single handler attached to #submit parent element table outside of $.post() complete callback.
I fixed it in the end. It wasn't to do with the #submit button, it was to do with the dropdown menu.
This is what got it to work:
var sectorALL = document.getElementById("sector_dropdown");
var sector = sectorALL.options[sectorALL.selectedIndex].value;

Need some guidance on creating a jEditable custom input

I'm using the jEditable plugin and the following code to toggle between On and Off for a series of settings on a page.
$(document).ready(function() {
$('.editable_select').editable(''http://someexamplepage.com/save.php', {
indicator: '<img src="/images/spinner.gif">',
data : " {'✓':'✓','✗':'✗'} ",
tooltip : 'Click to Edit',
type : 'select',
onblur : 'submit',
style : 'inherit'
});
});
And then this in the html:
<b class="editable_select" id="setting1" style="display:inline">✓</b>
When the checkmark indicating On is clicked, it produces a dropdown menu with checkmark for On and the X for Off in it, which the user can then select. What I would prefer is that clicking the check/X not open a dropdown, but instead send the current On or Off setting to the save.php file. I could then just write the save.php file to return the opposite value, so that clicking just toggles between the two without opening any kind of edit window. I tried the following code:
$(document).ready(function() {
$('.editable_select').editable('http://someexamplepage.com/save.php', {
indicator: '<img src="/images/spinner.gif">',
tooltip : 'Click to Edit',
onclick : 'submit',
style : 'inherit'
});
});
But clicking the text still opens a little editing window, which I don't want. I'm still new to JavaScript and jQuery, so any help is greatly appreciated!
I wouldn't use a plugin for this, but rather a very simple bit of jQuery to run on the ready event.
I would include the current state as a data attribute on the DOM, so change your tags to:
<b class="editable_select" id="[ID FOR ITEM]" data-state="checked">ઙ</b>
Then do something like:
$(function(){
var update_dom = function(data){
var item_id = data.id;
var state;
data.state === 'checked' ? state = '✓' : state = '✗';
$('#'+item_id).data('state', data.state).html(state);
};
var selected = function(evt){
var item_id = $(this).attr('id');
var state = $(this).data('state');
$.ajax({
url: PATH_TO_SCRIPT,
data: {id: item_id, state: state},
type: "POST",
success: update_dom
});
}
$('.editable_select').click(selected);
});
This binds the click event to everything that has a class of editable_select. When you click on it, it'll call the selected function, which wil get the data from the DOM, and call your script. When your script is complete, it should send back a response (JSON would be good here), and, the success handler will update the DOM and display the state back to the user.

Php return data from database into a table which contains an onlclick button function

Basically i have php code which retrieves data from a database into a table, i have placed a button with an onclick function in each row of the table. the buttons aren't working cause its php(server side).
Could someone point me in the right direction to do this? retrieve data from Db put into a table with a button in each row with an onclick event.
thanks
Just use an anchor in each row and assign a class name for example :
<a class="delete_btn" href="index.php?action=fetchdta&id=1">Delete</a>
then use this jquery code on top of your page in a script tag :
<script>
jQuery(document).ready(function ($) {
$('.delete_btn').click(function(){
var urlt = $(this).attr('href');
$.ajax({
url: urlt,
success: function(data) {
//do Everything you want
}
});
return false;
});
</script>

Hyperlink Click Popups Up JQuery Dialog And Returns Called PHP

I'm very sure a variation of this has been asked before, and my asking stems from my elementary knowledge of how it all works. I have been reading and reading and reading, and i'm turning to stackoverflow for some help on my question/problem.
Let me describe to everyone what i'd like. When a table is build on my webpage via a php include call, that table contains a column called 'Info', and in each row of that column for the table, is a hyperlink in created, called info. What i'd like to do is, when that hyper link is clicked, i'd like to return in a popup of some kind alert popup, jquery dialog, etc the data that the info link would return if navigating to that page. So in theory, i'm eliminating the navigation and providing that information to the user in a friendly dialog.
So on the index page where everything is happening i have code as follows:
include buildtable.php
Then in the buildtable.php i'm looping through a query and building a table. the column associated with the link look like:
echo '<td> Info </td>';
Now the above works if i navigate to that page; however, i'd like to try to display this in a dialog.
My questions are as follows:
1) should i keep the construction of the link as above, or should i change it to something like:
echo '<td> Info & nbsp;</td>';
2) the jquery dialog is confusing me, and i'm not even sure if i'm on the right track...
function getInfo(){
var $dialog = $('<div class="blah"></div>')
var $link = $this.val();)
.dialog({
autoOpen: false,
title: 'Information'
$.ajax {(
url:'myphpfile.php',
dataType: 'html',
data: 'link=' $link,
success: function( data ){
$('#<div id section on calling page>').html( data );
}//end success
}); //end ajax
});//end dialog
}//end Function
Any help and/or good tutorial links would be most appreciated.
use this link
echo '<td> <a id="infopopup" href="anotherpage.php?CheckoutId='.$DataArrDT[5].'&DbSchemaID='.$DataArrDT[2].'&DbId='.$DataArrDT[3].'">Info</a> </td>';
and use this javascript
$('#infopopup').click(function() {
var url = this.href;
var dialog = $("#dialog");
if ($("#dialog").length == 0) {
dialog = $('<div id="dialog" style="display:hidden"></div>').appendTo('body');
}
// load remote content
dialog.load(
url,
{},
function(responseText, textStatus, XMLHttpRequest) {
dialog.dialog();
}
);
//prevent the browser to follow the link
return false;
});`

Categories