Is it possible to echo a jQuery-function from php? - php

I need to load content into a div using a php-script triggered in another div but I'm at my wit's end..
Something like this:
echo"<script>$('#posters').load($query->execute($sql));</script>";

Something Like this?
This gets a php file (sql-query.php) in and then changes the content in the div #update upon a button click.
$(document).ready(function () {
$('.btn').click(function(){
$.get("sql-query.php", function (result) {
$('#update').html(result);
});
});
});

Related

call jquery click function from other page

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.

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.

Issue with calling JavaScript in Jquery

I am trying to call a function that is in javascript inside Jquery once a link is clicked. When I try and do this nothing happens.
Code:
function run(){
var url = '/pcg/popups/grabnotes.php';
showUrlInDialog(url);
}
$("#NotesAccessor").click(function () {
run();
});
PHP:
..
echo "<a href='#' id='NotesAccessor'>Click to access</a>";
..
Not to sure why, I check my calling in the debug and nothing gets called. From the run function it goes to a Jquery UI dialog that will open up. It just does not get to that point.
If you could give me a hand I would appreciate it!
Probably your DOM isn't loaded yet when you define the click event for $("#NotesAccessor").
Wrap your instruction in a $(document).ready handler:
$(document).ready(function() {
$("#NotesAccessor").click(function () {
run();
});
});
Your problem could be because NotesAccessor is an anchor element. You can make NotesAccessor to be a div and with css you make look just like an anchor.
In the other hand you can call to a JavaScript function from an anchor directly something like:
<a href:'javascript:run()' >Click to access</a>

jquery click function with ajax content

My End goal is to have ajax displayed content be clickable to shove whatever it's value is into a text box.
Right now I have the ajax loading but when I click on it, it doesn't work. As of now I have jquery assigned the 'a' tag so when it's clicked it puts the static text "works" in the textarea with the id=email-body.
$(document).ready(function() {
// Expand Panel
$('a').click(function(){
$('#email-body').val($('#email-body').val()+'works');
});
});
This works just fine for a tags that aren't part of the ajax content but it doesn't work for the ajax content. Is there a special pull system I should use to have jquery effect ajax content?
Also static text and clicking on an a tag isn't really want I want.
Right now my ajax is doing the following:
foreach($search_found as $search_row){
$hint=$hint . '<a>'.ucwords($search_row['email_module_name']).'</a>';
}
echo $hint;
Is there a way I can have it be:
AJAX:
<div class="module" value="'.ucwords($search_row['email_module_name']).'">
'.ucwords($search_row['email_module_name']).'
</div>
JQUERY:
$(document).ready(function() {
// Expand Panel
$('.module').click(function(){
$('#email-body').val($('#email-body').val()+'$this->value');
});
});
What you want is to delegate the events, to do this you can use .on()
$(document).ready(function() {
// Expand Panel
$(documnet).on('click', 'a', function(){
$('#email-body').val($('#email-body').val()+'works');
});
});
You need to use the .on function:
$('div').on('click', '.module', function(){
$('#email-body').val($('#email-body').val()+'$this->value');
});
This allows the 'click' function to attach to the elements that are brought in via ajax.
You can use .on() to bind an event to current and future elements.
$(document).on("click", "a", function(){
$('#email-body').val($('#email-body').val()+'works');
});
Or,
$(document).on("click", ".module", function(){
$('#email-body').val($('#email-body').val()+'$this->value');
});
$('.module').live('click', function(){
$('#email-body').val($('#email-body').val()+'$this->value');
});
I figured it out and came back on to see someone posted .on
I tried this and it did not work. What did work was .live

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