Jump to content

Ajax Problem


[dx]
 Share

Recommended Posts

I hope this is right place to start topic about AJAX, othervise feel free to move topic in plase where it belongs.I have this in index.php file

echo "<div id='trazi_res'></div>";

This is js.js

var xmlhttpfunction trazi(vrijednost) {	xmlhttp = GetXmlHttpObject();	if (xmlhttp==null) {		alert("Vas browser ne podrzava AJAX. Pokusajte sa IE, Firefox...");	}	var url = "trazi.php";	url = url+"?sta="+vrijednost;	xmlhttp.onreadystatechanged = stateChanged;	xmlhttp.open("GET", url, true);	xmlhttp.send(null);}function GetXmlHttpObject() {	if (window.XMLHttpRequest) {		return new XMLHttpRequest();	}	if (window.ActiveXObject) {		return new ActiveXObject("Microsoft.XMLHTTP");	}	return null;}function stateChanged() {	if (xmlhttp.readyState==4) {		document.getElementById("trazi_res").innerHTML = xmlhttp.responseText;	}}

and this is trazi.php

<?PHPecho '<head><script type="text/javascript" src="js.js"></script><style type="text/css">/* <![CDATA[ */ @import url(style.css); /* ]]> */</style></head>';include "db.php";$sta = $_GET['sta'];$select_from = mysql_query("SELECT * FROM stanice WHERE naziv LIKE '%$sta%'");while($trazi = mysql_fetch_array($select_from)) {	echo "<br />" . $trazi['naziv'];}?>

What's wrong with this? :SI can't figure out where is the error but it won't work...Thanks

Edited by Haris S
Link to comment
Share on other sites

Well, I have input box where I type something what I want search. And on the bottom of the page there's <div> where it must be shown. But if I type in input nothing is shown in <div>

Link to comment
Share on other sites

Are you getting any errors? You should be using Firebug to help debug, in addition to showing the errors you can look at the ajax request going out and check the response for errors from PHP. You can also have PHP put all of its errors in an error log instead of trying to print them.error_reporting(E_ALL);ini_set('error_log', dirname(__FILE__) . DIRECTORY_SEPARATOR . 'error.log');ini_set('html_errors', 0);ini_set('log_errors', 1);ini_set('display_errors', 0);That will put all errors in a file called error.log in the same directory.

Link to comment
Share on other sites

Post all of the code that's involved, whatever you type in, whatever triggers the Javascript function, etc. If the trazi.php file is what Javascript is sending the request to, why do you have that returning a head section? Are you replacing the head on the parent page?

Link to comment
Share on other sites

Now I've formated code like this

function stateChange(target) { 	alert(http.status);	if (http.readyState == 4) 	{ 	var response = http.responseText; 		if (response) { 		document.getElementById(target).innerHTML = response;		} 	} }

Added "alert"And keep getting message "404" which means not found, but file is in same dir as js.js and index.php

Link to comment
Share on other sites

Yeah, that was"<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">\n<html><head>\n<title>404 Not Found</title>\n</head><body>\n<h1>Not Found</h1>\n<p>The requested URL /trazi.php was not found on this server.</p>\n</body></html>\n"

Link to comment
Share on other sites

Well, ok. Now getting thisElement referenced by ID/NAME in the global scope. Use W3C standard document.getElementById() instead.[break on this error] undefinedjs.js (line 32)[Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.status]" nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)" location: "JS frame :: http://127.0.0.1/radio/js.js :: stateChange :: line 12" data: no][break on this error] alert(http.status);But, I'm using document.getElementById

Link to comment
Share on other sites

function createRequestObject() { 	var req; 		if(window.XMLHttpRequest){ req = new XMLHttpRequest(); } 		else if(window.ActiveXObject) { req = new ActiveXObject("Microsoft.XMLHTTP"); } 		else { alert('Vas browser ne podrzava AJAX, pokusajte sa Firefox, IE...'); } 	return req; } var http = createRequestObject(); function stateChange(target) { 	if (http.readyState == 4) { 	var response = http.responseText; 		if (response) { 		document.getElementById('target').innerHTML = response;		} 	} } function trazi(vr) {	http.open('get', "trazi.php?sta="+vr, true);	http.onreadystatechange = function(){ stateChange('trazi_res'); }	http.send(null);}

js.js currently

Link to comment
Share on other sites

Since target is a variable, you have to take the quotes off it:document.getElementById('target').innerHTML = response;

Edited by Ingolme
Link to comment
Share on other sites

I've tried that before.With Firebug I'm getting errordocument.getElementById("target") is null
You have to put it without quotation marks:
document.getElementById(target).innerHTML = response;

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...