July 17, 2012

jquery .load() not working onclick

Question by loriensleafs

I’m trying to have a site set up so when the page loads there is a div that dynamically pulls it’s content from a .html page I have set up. I have a bunch of thumbnails at the top and when you click one I want the content from a different .html document to replace what every was in that div that was dynamically loaded into the first time.

To do this I’m trying to use the jquery .load() feature. What I tried to do was set up a function:

<script type="text/javascript"> 
$(document).ready(function space_load() {

$('#SPACE_TOTAL').load('http://www.klossal.com/portfolio/space.html');
}
</script>

and then tried to launch it using:

onclick="space_load();"

this didn’t work and I was wondering if anyone could help me with this. The other thing I was wondering is if this were to work, would it replace the content that was previously loaded into there? I might be getting a head of myself and it just does this on it’s own.

Answer by Starx

First, your code has invalid structure

$(document).ready(function() {    
    function space_load() {
        $('#SPACE_TOTAL').load('http://www.klossal.com/portfolio/space.html');
    }    
    space_load(); //Now call the function
});

However, you can trim it down do this

$(function() {
    $('#SPACE_TOTAL').load('http://www.klossal.com/portfolio/space.html');
});

But, since you want this on click of an element. This is what you need:

$(function() {
    $("#yourlinkid").click(function() {
        $('#SPACE_TOTAL')
           .html('<img src="preloader.gif" />')
           .load('http://www.klossal.com/portfolio/space.html');
    });
});
March 2, 2012

jquery function that load html code into a div and then load other content into div inside html loaded

Question by Raphael D.G

I’d like to know what’s the way to load an html page (page1.html) into a div in webpage active (index.html) and then load another html page (page2.html) into a div that will be inside of page loaded (page1.html). I mean.

index.html

<div id="content"></div>
<a class="link" href="#">load</a>

script

$(document).ready(function() {
    $('a.link').live('click', function() {
        $('#content').load('page1.html', function(){
            $('#content2').load('page2.html');
        });
    });
});

page1.html

<div id="content2"></div>

It’s works fine for only 1 click, at the second click it loads page2.html for 0,5 seconds and then loads page1.html.

What’s the problem ???

Thank you

Answer by Starx

If I am correct, you are trying to load onto the second secontainer, after first container is loaded

Add a simple class on your markup

<div id="content" class="toload"></div>

&

<div id="content2" class="toload"></div>

Now, here is the magical jQuery you need

$(document).ready(function() {
    $('a.link').live('click', function() {
        $('.toload').load('page1.html', function(){
            $(this).removeClass('toload');  //Remove the class so that next time it does not get affected
        });
    });
});

How to run multiple sql queries using php without giving load on mysql server?

Question by aslamdoctor

I have a script that reads an excel sheet containing list of products. These are almost 10000 products. The script reads these products & compares them with the products inside mysql database, & checks

  • if the product is not available, then ADD IT (so I have put insert query for that)

  • if the product is already available, then UPDATE IT (so I have put update query for that)

Now the problem is, it creates a very heavy load on mysql server & it shows a message as “mysql server gone away..”.

I want to know is there a better method to do this excel sheet work without making load on mysql server?

Answer by Starx

Ok, here is quick thought

Instead of running the query, after every check, where its present or not, add on to your sql as long as you reach the end and then finally execute it.

Example

$query = ""; //creat a query container
if($present) {
    $query .= "UPDATE ....;"; //Remember the delimeter ";" symbol
} else {
    $query .= "INSERT ....;";
}
//Now, finally run it
$result = mysql_query($query);

Now, you make one query at the last part.


Update: Approach this the another way

Use the query to handle it.

INSERT INTO table (a,b,c) VALUES (1,2,3)
  ON DUPLICATE KEY UPDATE c=c+1;

UPDATE table SET c=c+1 WHERE a=1;

Reference

...

Please fill the form - I will response as fast as I can!