Current location: Hot Scripts Forums » Programming Languages » PHP » how to use ajax with php?


how to use ajax with php?

Reply
  #1 (permalink)  
Old 05-11-09, 07:07 PM
gnagis gnagis is offline
New Member
 
Join Date: May 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Question how to use ajax with php?

im trying to get a ajax getpage with and ID added to the file to fetch and then show it on the div that has the correct id... however im very new to this stuff and i cant manage to get my ids to be sent to the ajax code... what am i doing wrong ?

my JS code:

Code:
<script type="text/javascript"> 
var xmlHttp; 
function showhistory(url,idst) 
{ 
xmlHttp=GetXmlHttpObject(); 
if (xmlHttp==null) 
{ 
alert ("Din browser stödjer inte ajax!"); 
return; 
} 
xmlHttp.onreadystatechange=stateChanged; 
xmlHttp.open("GET","ajaxsql.php?gid="+url,true); 
xmlHttp.send(null); 
} 
function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") 
{ 
document.getElementById("idst").innerHTML=xmlHttp.responseText; 
} 
} 
function GetXmlHttpObject() 
{ 
var xmlHttp=null; 
try 
{ 
// Firefox 
xmlHttp=new XMLHttpRequest(); 
} 
catch (e) 
{ 
//IE 
try 
  { 
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); 
  } 
catch (e) 
  { 
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); 
  } 
} 
return xmlHttp; 
} 
</script>
the code to call the script is:

Code:
<a href=#self onclick=\"showhistory({$guestbook_load[$i]->uid},'{$gbidt}');\">Visa SQL svar</a>


and when using bugzilla it says "idst not defined" but i dont understand what im missing?
im guessing its realy easy but right now im totaly confused :S
Reply With Quote
  #2 (permalink)  
Old 05-11-09, 08:38 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
function stateChanged() is where you are using your var idst, yet your passing it to showhistory without sending it through to stateChanged();

In my experience the stateChanged function doesn't like arguments passed to it using the xmlHttp.onreadystatechange function.

I use one division for all ajax calls, and then move that to the appropriate location on each page.

if your interested, here is my ajax code:

use: getPage('fileWithoutExstention','null') for a page passing no variables;
use: getPage('fileWithoutExstention','cmd=command') for page with variables;

will return results to a <div id="results">

Code:
function getPage(file,str)
	{
		xmlHttp=GetXmlHttpObject();
			if (xmlHttp == null)
				{
					alert ("Your browser does not support AJAX!");
					return;
				}
		url="ajax/"+file+".php";
		if (str != null)
			{
				url = url+"?"+str;
			}
		
		xmlHttp.onreadystatechange=pageChanged;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
		
	}
function pageChanged()
	{ 
if (xmlHttp.readyState==4)
{ 
document.getElementById("results").innerHTML=xmlHttp.responseText;
}
else 
{
document.getElementById("results").innerHTML="Processing...";
}
}


function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}
Reply With Quote
  #3 (permalink)  
Old 05-12-09, 02:48 AM
gnagis gnagis is offline
New Member
 
Join Date: May 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
thanx!
but the problem is still that i need it to open in a "dynamic" div (as the divs are created with uniq ids)

i tryed adding this on your code:
dividt="2_"+str;

and then
document.getElementById("dividt").innerHTML=xmlHtt p.responseText;

but this still gives me the same error as befor...
"not defined" on the "dividt"

Last edited by gnagis; 05-12-09 at 03:10 AM.
Reply With Quote
  #4 (permalink)  
Old 05-12-09, 03:29 AM
gnagis gnagis is offline
New Member
 
Join Date: May 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
ops solved it! thanx!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Ajax - getting variables from the php code. zippoxer JavaScript 6 03-24-09 01:12 AM
Ajax....changing values of php variables sushi4664 JavaScript 1 08-09-07 12:04 AM
2 profitable script sites for sale cms-master.com General Advertisements 3 07-03-07 10:17 AM
Sample scirpt php & AJAX jsanders PHP 3 07-24-06 05:24 AM
PHP Downside--Solutions? Amulet PHP 10 07-15-05 08:26 AM


All times are GMT -5. The time now is 08:35 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.