Jump to content

Ajax&Cache + Connection


Recommended Posts

Hey everyone, Few questions : 1.)When i include a php page via ajax, Am trying to query data from mySQL server, for some reason i need to re-establish connection to mySQL server.However, I already established my connection in my Header file which i include at the start of each file.Here is my example : Header.php - Included in each file.

// Relavent connection code	$connection_request = mysql_connect("localhost","username","Password");		mysql_select_db("database", $connection_request);...// Relavent Ajax included codefunction Cart_QuickDisplay(){if (window.XMLHttpRequest)  {// code for IE7+, Firefox, Chrome, Opera, Safari  xmlhttp=new XMLHttpRequest();  }else  {// code for IE6, IE5  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");  }xmlhttp.onreadystatechange=function()  {  if (xmlhttp.readyState==4 && xmlhttp.status==200)	{	document.getElementById("Display").innerHTML=xmlhttp.responseText;	}  }xmlhttp.open("GET","http://Domain_Name/getuser.php",true); // In this page, i need to make a new connection to mySQL server, although i already made connection in the same page, few code lines ago.xmlhttp.send();}

My_Shooping_Cart.php

<?phpinclude("Header1.php"); // Set connection + define Cart_QuickDisplay() function where we call getuser.php?><!-- Calls getuser.php when onmouseover event is triggered--><a href="My_Shopping_Cart.php" onmouseover="Cart_QuickDisplay()" style="border:0px;"><img src="http://Domain/img/New_Rest/Shopping_Cart-01.jpg" style="margin-top:11px; margin-right:5px; width:16px; height:11px; border:0px; float:right;" /></a>

getuser.php

<?php// Need to establish connection here again in order to query data from mySQL server, why? getuser.php is a called from My_Shopping_Cart.php which already included the header file where there connection is made?>

In getuser.php , I need to make a new connection to mySQL database, allthough its already included inside the header.Diagrem : My_Shopping_Cart.php(Includes Header.php, Calls getuser.php)Header : (Establish connection to mySql Server, Defines the ajax caller function to getuser.php)My question is : why getuser.php which is been called from My_Shopping_Cart.php where header is included (The place where mySQL connection is made), needs to establish the connection once again?2.) : "My shopping cart button" queries my cart products everytime i set my mouseover it via ajax(onmouseover) - just like i intended to, However, Is there a way to check if any changes were made since the last time the client overmoused the button to query the shopping cart porduct list?Think about a cache paradigm, Data first time stored in cache, If 2nd time accessed, Check if data has changed, if so update the cache index with new values, if not, Load unchanged values directly from cache.

Edited by Illasera
Link to post
Share on other sites

You didn't make a connection in the same page, each XMLHttpRequest proccess is an entirely new HTTP request. The client processes the JavaScript and makes the request; it is entirely separate to the page containing the JavaScript code. Think about it...For your second question, you can have a primary request to a page that just responds with an indicator of whether any changes have been made, and if so make a second request for the updated list.

Link to post
Share on other sites
You didn't make a connection in the same page, each XMLHttpRequest proccess is an entirely new HTTP request. The client processes the JavaScript and makes the request; it is entirely separate to the page containing the JavaScript code. Think about it...
Understandable :)
For your second question, you can have a primary request to a page that just responds with an indicator of whether any changes have been made, and if so make a second request for the updated list.
Huh? I lost you there mate, Can you give me an example please in theory?
Link to post
Share on other sites

If you want to increase the response time (very slightly unless the list was very long) then you could have an initial page that simply returns something, say "1" or "0", that indicates whether the items in the cart has changed. If the value is one, then you proceed to make an additional request for the actual updated content, otherwise you just load the previous values.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...