So have a datatable wherein if i click a cell, it redirects me to another page. The problem is I need to store the clicked data for the next page. I wanted to store /tablez.cell(this).data()/ inside a session but I'm not sure if it's possible. Here's my code so far:
var tablez= $('#table').DataTable();
$('#table tbody').on( 'click', 'td', function () {
console.log(tablez.cell(this).data());
window.location.href = "FH_Suppliers_Add.php";
});
Related
I have the following href link
Click Here to View data
and the pop is showing on page reload, but I want to show it only on click.
$('.modal-ajax').modal();
Now the page is redirect into url. I want to show the pop up
I didnt wrote any js code. i tried this now
$('a').click( function(e) {
e.preventDefault();
$('.modal-ajax').modal();
return true;
} );
but it raises $('.modal-ajax').modal(); is not a function error
You could simply put a comment on the javascript code that trigger the modal :
// $('.modal-ajax').modal();
Then add a click event listener for the element you wish to be clicked :
$('a.modal-ajax').click(function(event) {
event.preventDefault();
$('.modal-ajax').modal();
});
Please try this
Click Here to View data
$('.modal-ajax').on('click', function() {
var url = $(this).attr('data-id');
//place this value wherever you want in modal.
$('#openModal').show();
});
Need help..
I have an index.php page. On that page, I have a link to page-2.php.
On page-2.php,
i also have a link to other page. May I know how to load the link on page-2.php in a same div on index.php page.
I'm using jQuery load method do to this. I'm able to load page-2.php inside a div on index.php. Below are the codes that I made.
$(document).ready(function() {
$('a#staff_info').click(function(e){ //link to page-2.php
e.preventDefault();
var page = $(this).attr('href');
$('#content').load(page);
$('a#upd_staff').click(function(){ //link on page2.php
var page_info = $('a#upd_staff').attr('href');
$('#content').load(page_info);
});
});
});
the above code will direct me to a new page not on a same div
Ajax is asynchronous.
$('#content').load(page);
The above starts loading page two
$('a#upd_staff').click(function(){ //link on page2.php
Then you try to bind your event handler to content from page 2 before it is has finished loading.
So when you click on the link, there is no JS bound to it, so it acts as a normal link.
Use deferred event handlers (so they listen on the document and check what element was clicked when the click happens).
$(document).ready(function() {
function ajaxLoad(event) {
event.preventDefault();
var page = $(this).attr('href');
$('#content').load(page);
}
$(document).on("click", "a#staff_info, a#upd_staff", ajaxLoad);
});
when you click on $('a#staff_info') this -- I can see that you use e.preventDefault(); for preventing the normal action of the anchor tag but when you click on $('a#upd_staff') this you did not use e.preventDefault(); -- that's why this redirect to page2.php. Use e.preventDefault(); - this for next one click may be this will help you
You can use get() for add directly on your div:
$(document).ready(function() {
$('a#staff_info').click(function(e){ //link to page-2.php
e.preventDefault();
var page = $(this).attr('href');
$.get(page, function(data, status){
//action on page loaded, data is your page result
$('#content').html(data)
});
$('a#upd_staff').click(function(){ //link on page2.php
var page_info = $('a#upd_staff').attr('href');
$('#content').load(page_info);
});
});
});
I have a PHP script (ajax.php) which loads content from a MySQL db. That script refreshes new content every N seconds via AJAX. The AJAX content has three links of which one is to archive the row in MySQL. When a user clicks the archive button, I want a jQueryUI dialog to open confirming deletion. Pressing cancel will abort, while OK will trigger an AJAX submission to delete the row from MySQL. Deleting row from table with modal form confirmation (jQuery UI)? is an example of the dialog half of what I want to do. I need this to work along with the ajax/refresh.
Because of the link itself being loaded by AJAX, I cannot run the dialog (or the delete script). How do I load the dialog script and perform the AJAX delete submission? I'm trying to use the GlobalEval but I do not understand the documentation. The examples I've found seem to be for setting variables only. The following script is what I'm trying to build it from:
var id = $('.refreshN').data('id');
var table = $('.refreshN').data('table');
var rtime = $('.refreshN').data('time');
$('.refreshN').load('ajax.php?id='+id+'&table='+table).fadeIn("slow");
var autoLoad = setInterval(
function ()
{
$('.refreshN').load('ajax.php?id='+id+'&table='+table).fadeIn(5000);
var script = $(function (){
$('.modalLink').click(function () {
$('#dialog').dialog('open');
return false;
});
});
eval(script);
}, rtime); // refresh page every N seconds
Define the variable script with an normal javascript function and add () to execute it.
Try this instead of your current js:
var id = $('.refreshN').data('id');
var table = $('.refreshN').data('table');
var rtime = $('.refreshN').data('time');
$('.refreshN').load('ajax.php?id='+id+'&table='+table).fadeIn("slow");
var autoLoad = setInterval(
function ()
{
$('.refreshN').load('ajax.php?id='+id+'&table='+table).fadeIn(5000);
}, rtime); // refresh page every N seconds
$(document).on('click','.modalLink',function(e){
e.preventDefault();
$('#dialog').dialog('open');
})
I'm using the code below to load the results from a database query in a PHP page:
click me
$('.item > a').click(function(){
var url = $(this).attr('href');
$('.item-popup').fadeIn('slow');
$('.item-content').load(url);
return false;
});
All works fine right now, but the next bit of functionality is a problem. Inside results.php which ajax loads into .item-content, I have another link that is supposed to update and increment click counts for that link, also without refreshing. The functional PHP bits all work fine. My only problem is the jQuery/AJAX aspect of things.
Maybe I'm going about it the wrong way, but what I really want to do is have a page with a container that loads the result of of a database query from a PHP page, but also in that container, I have a link/button whose click count I want to be able to save and update all without refreshing.
EDIT
I guess the most important question I need answering is: When the ajax on index.php loads the content of results.php into the container in index.php, do browsers treat the newly loaded ajax content as part of the parent page (index.php) or is it still treated as a different page loaded into the container like an iFrame?
If say for example it is click event then you need to write
$('input element').on('click',function() {
// write code over here
})
Dont know for sure if you want this, When returning the data in the load function you will have to add a link like this in the resultant HTML which will be clickable:
Now in javascript you need to catch the click event of the link like this:
<script type="text/javascript">
$(function(){
$(".item-content").on("click", ".clickable", function(){
var counter = $(this).data('counter');
var id = $(this).data('id');
$.ajax({
url : //your url here,
data : {'id' : id, 'counter' : counter },
type : 'POST',
success : function(resp){
//update the counter of the current link
$(this).data('counter', parseInt( $(this).data('counter') )+1 );
//whatever here on successfull calling of ajax request
},
error : function(resp){
}
});
});
});
</script>
I use a page with Jquery tabs and if i submit one of the forms in the tabs only that tab is submitted and refreshed with this jquery code:
$(document).on("submit", "#plaatsen_stap3", function(event) {
/* stop form from submitting normally */
event.preventDefault();
$.ajax({
type:"GET",
url:"../plaatsen_advertentie/plaatsen_advertentie_stap3.php",
cache: false,
data: $("#plaatsen_stap3").serialize(),
success:function(data){
$("#tab2").html(data);
}
});
});
But in the case that there has to be payed i want to reload the page with the payment page. I want to do that AFTER the div is reloaded with the data, because i need to put a payment row in the DB with the data from the GET. Is location an option? If i use that now only the div (tab2) is loaded with the payment page....
So:
1.push submit
2.submit the form and load page/script in div by Ajax
3.check in php script (within the div) if payment is needed
4.if yes,add row with payment data in database and reload entire page with payment page (with some Get data in the url (last inserted id)
success:function(data){
$("#tab2").html(data);
location.href = "/yourpage.php";
}
Since you wanna do once the HTML is generated, give some time like about 5 seconds?
success:function(data){
$("#tab2").html(data);
setTimeout(function(){location.href = "/yourpage.php";}, 5000);
}
This would work for your use case. This cannot be done from server side.
I think load() is what you are looking for. http://api.jquery.com/load/
The code below is intended as a guideline, and I'm not even sure it's working (I have not tested it). But I hope it will be of some help.
I would do something like this:
//Step 1
$(document).on("submit", "#plaatsen_stap3", function(event) {
/* stop form from submitting normally */
event.preventDefault();
$.ajax({
type:"GET",
url:"../plaatsen_advertentie/plaatsen_advertentie_stap3.php",
cache: false,
data: $("#plaatsen_stap3").serialize(),
success:function(data){
//Step 2 - submit the form and load page/script in div by Ajax
//Make sure array[] is an actual array that is sent to the test.php-script.
$("#tab2").load("test.php", { 'array[]' , function() {
//Step 3 - check in php script (within the div) if payment is needed (Do this from test.php - don't check the actual div but check values from array[])
//Step 4 - if yes,add row with payment data in database and reload entire page with payment page (with some Get data in the url (last inserted id)
//Do this from test.php and use header-redirect to reload entire page
$("tab2").html(data); //Do this when test.php is loaded
}
} );
}
});
});