call jquery click function from other page - php

Can i get variable if my button from other file..
example page 1 (a.php):
Button
and page 2 (b.php):
$(document).ready(function() {
$('a.php .mybutton').click(function() {
alert("ok");
});
});

If you put your function in an external js file then you can fire it from any page. then it wont be a click function but a regular js function. Refernce the js file at the bottom of any page where you want to use the function.

Related

load dynamic data from remote url into a bootstrap modal pop up

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();
});

AJAX is working on second click only

This is my index.html page where am trying to implement a simple AJAX when clicked on anchor tag the text will be passed to PHP data.php page and that page will display the page being clicked. Yes, the code is working, but when I first click on the link nothing is displayed and from the second time it stars working. Where is the problem actually?
This is my script
<script>
$(document).click(function(){
$("a").click(function(){
//when clicked our ajax will work
$.get('data.php',{'page':$(this).text()},function(data){
$('#result').html(data);
});
});
});
</script>
And this is my PHP for the AJAX
<?php
$anchortext=$_GET['page'];
if(isset($anchortext))
{
echo 'this is a'.$anchortext;
}
?>
You misplaced $(document).click(..) for $(document).ready(....), so only after the first click the click handler is registered to the a element
$(document).ready(function(){
$("a").click(function(){
//when clicked our ajax will work
$.get('data.php',{'page':$(this).text()},function(data){
$('#result').html(data);
});
});
});
Use $(document).ready instead of $(document).click.

Loading json after a button click

I want to load a json file after a user clicks on a button, not sure how to do that.
If I load the file directly it will load using the following.
<script src="data.json" type="text/javascript"></script>
But I want the user to click a button before it loads instead of loading when the page is accessed.
Thanks
Simple: use AJAX. It will load anything you want via Javascript which you can assign timings to. In this example we attach a click event to #myButton and when the event fires, we run a getJSON() call to the URL and handle the data accordingly.
HTML
<button id="myButton">Button</button>
Javascript (jQuery in my example)
$("#myButton").on('click', function() {
$.getJSON("data.json", function(data) {
//Handle my response
alert(data);
});
});
If you want an example not using jQuery then Google "Javascript AJAX".

change PHP session id on link click

I am trying to create an HTML link that changes the PHP session id. Basically I need to call the session_regenerate_id(); when a link is clicked. The problem is that it can't run on the page that I am linking to. There is an upload form that reloads after upload so the function cannot be on the page I am linking to.
Can I do an AJAX post to a separate PHP file?
In your php file called regenerate.php:
<?php session_regenerate_id(); ?>
And then with jquery.
$.POST('url/to/regenerate.php', function() {
// callback
});
Then to run it:
$(function() {
$('#postNew').click(function(e) {
e.preventDefault(); // stops the link from going anywhere
$.POST('url/to/regenerate.php', function() {
// callback
});
});
});

Load links in a CSS frame?

I am using this script to load pages in a css frame (div#maincontent). I want the links in the loaded pages to load in this frame instead of just "going" there. I want to do this using jquery. Here is my attempt:
$('#maincontent a').click(function(e) {
e.preventDefault();
ajaxpage($(this).attr("href"), 'maincontent');
});
Not sure what ajaxpage is, but if you're just loading a page into a div you can simply use load
Something like this:
$('#maincontent a').click(function(e) {
e.preventDefault();
$("#maincontent").load($(this).attr("href"));
});
Edit:
If you want the loaded page to have event handlers for click too, you can use load()'s callback function for this. Like this:
function engageLinks() {
$('#maincontent a').click(function(e) {
e.preventDefault();
$("#maincontent").load($(this).attr("href"), function() {
engageLinks();
} );
});
}
// Call the function the very first time the page is loaded.
// After that the function will call itself.
engageLinks();
Here's an example.
Replace ajaxpage with $('#maincontent').load($(this).attr("href"));

Categories