i have basic site loading content via ajax on document ready. problem code not "live" , requires me re-include php includes within loaded page in order access queries contained within it.
i know can work around using 'on' method when comes events dynamically loaded code functional start, how go when page has loaded? seems redundant , sloppy have reinclude php includes on each loaded page.
here's code:
<?php session_start(); require_once('dbconnect.php'); require_once('functions.php'); ?> <!doctype html> <html> <head> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script type="text/javascript" src="x.js"></script> </head> <body> <div id="container"> <div id="content"> </div> </div> </body> </html>
js:
$(document).ready(function() { $('#content').load('x.php', function() { alert("loaded"); }); });
loaded page:
<?php //load work $query = mysql_query("select * work"); if (mysql_num_rows($query) > 0) { while ($work = mysql_fetch_array($query)) { ?> <div class="still_cell"> <img class="still_image" src="<?php print $work['image_path']; ?>" /> <div class="still_info"> info </div> </div> <?php } } ?>
results in various php errors saying query produced no results unless include code connect database nested in loaded file
php unaware of client does.
when browser makes ajax call, appears independent request web server.
key point is, php independently executed each incomming request.
there ways persist data between multiple requests php, like:
- apc (php extension): cache compiled php scripts - can store user data in memory persists between requests.
- files: serialize variables , store them in file.
however, when comes resources (mysql connection, file handles, etc.), considered unsafe try persist them. apc , serialization instance refuse work resource type.
the conclusion: bootstrapping (connecting db, declaring classes, functions etc.) done for each request.
Comments
Post a Comment