/    Sign up×
Community /Pin to ProfileBookmark

AJAX Data Filter Does Nothing

I’m trying to filter tabular data using AJAX. I have a text input field that should reduce the table display to only show entries that contain the string entered into the box. Currently, changing the text box does nothing.

This is the PHP script that generates the main page:

[code=php]<?PHP
session_start();

$_SESSION[‘current_entity_ID’] = ‘0’; // delete this later!!

// this is a list of events, that is sortable!!

// include some crap
require(‘../../global/config.php’);
require(‘../../include/functions/global_functions.func’);
require(‘../../include/SQL_connect.php’);
require(‘../../include/ajax/event_list_table.func’);

// start page output
require(‘../../include/header.php’);

// Include the AJAX basic JS function
echo “<script src=’../../include/ajax/ajax_basic.js’></script>”;

// close the head and start the page body
echo “</head><body>”;

/* Start To Build the Main HTML Page */

// main body area
echo “<div id=’border’>”;

// query for table guts
$query = “SELECT * FROM Events JOIN Producers WHERE Events.producer_ID = Producers.producer_KEY AND Events.entity_ID = ‘” . $_SESSION[‘current_entity_ID’] . “‘ ORDER BY Events.event_start_time”;
$result = safe_query($query);

// build the table guts
$event_table = draw_event_list_table($result);

// output the table inside of a div
echo “<div id=’event_table_area’>”;
echo $event_table;
echo “</div>”;

// show the filter form
echo “<form method=’post’ name=’filter’ action='”. $_SERVER[‘PHP_SELF’] .”‘>”;
echo “<label for=’event_name_filter’>Event Name:</label>
<input type=’text’ onkeyup=’ajaxFunction();’ value=’start typing an event name to filter the form’ name=’event_name_filter’ id=’event_name_filter’ />”;

echo “</div></body>”; //close the main body area

?>[/code]

This is ajax_basic.js:

[CODE]function ajaxFunction()
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject(“Msxml2.XMLHTTP”);
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
catch (e)
{
alert(“Your browser does not support AJAX!”);
return false;
}
}
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.event_table_area.value=xmlHttp.responseText;
}
}
xmlHttp.open(“POST”,”event_list_table.func”,true);
xmlHttp.send(null);
}
[/CODE]

and this is event_list_table.func, the PHP script that it calls:

[code=php]<?PHP

/* The following is a set of functions to sort the event list display
based on user preferences. This function will be called using AJAX.
*/

// Create the header row of the Event List Table
function draw_event_list_tabletop()
{
$h_output = “<br /><table width=’720px’ align=’center’ class=’sortable’ id=’unique_id’>”;
$h_output .= “<tr class=’sortable’>
<th class=’sortable_space’></th>
<th width=’280px’ class=’sortable’>Event</th>
<th class=’sortable_space’></th>
<th width=’115px’ class=’sortable’>Producer</th>
<th class=’sortable_space’></th>
<th width=’55px’ class=’sortable’>Start</th>
<th class=’sortable_space’></th>
<th width=’55px’ class=’sortable’>End</th>
<th class=’sortable_space’></th>
<th width=’120px’ class=’sortable’>Date</th>
</tr>”;

return $h_output;
}

// Create the Events List Table
function draw_event_list_table($result)
{

// Start the table
$h_output = draw_event_list_tabletop();

$output = $h_output;

$i = 1;

WHILE ($row = mysql_fetch_array($result))
{
$output .= “<tr class=’sortable’>”;
$output .= “<td><input id=’eventlist_$i’ type=’checkbox’ /></td>”;
$output .= “<td class=’sortable’><label for=’eventlist_$i’>” . $row[‘event_name’] . “</label></td>”;
$output .= “<td></td>”;
$output .= “<td class=’sortable’>” . $row[‘producer_name’] . “</td>”;
$output .= “<td></td>”;
$output .= “<td class=’sortable’>” . date(‘g:i a’, $row[‘event_start_time’]) . “</td>”;
$output .= “<td></td>”;
$output .= “<td class=’sortable’>” . date(‘g:i a’, $row[‘event_end_time’]) . “</td>”;
$output .= “<td></td>”;
$output .= “<td class=’sortable’>” . date(‘l, F d’, $row[‘event_start_time’]) . “</td>”;
$output .= “</tr>”;

$i++;
}

// close the table
$output .= “</table>”;

return $output;
}

function filter_by_event_name($event_name_IP_string)
{

$query = “SELECT * FROM Events WHERE event_name = ‘%” . $event_name_IP_string . “%'”;

$result = safe_query($query);

// build the table guts
draw_event_list_table($result);

}

?>[/code]

to post a comment
JavaScript

29 Comments(s)

Copy linkTweet thisAlerts:
@lightnbauthorApr 02.2007 — [code=php]
// build the table guts
draw_event_list_table($result);
[/code]


has been changed to:

[code=php]
// build the table guts
$output = draw_event_list_table($result);
return $output;
[/code]


but still no errors, or result.
Copy linkTweet thisAlerts:
@BeanisApr 02.2007 — Instead of:
[CODE]document.event_table_area.value=xmlHttp.responseText;[/CODE]
try:
[CODE]alert(xmlHttp.responseText); //To make sure you are getting a reply (then remove)
document.getElementById("event_table_area").innerHTML=xmlHttp.responseText;[/CODE]
Copy linkTweet thisAlerts:
@lightnbauthorApr 03.2007 — I'm now using:

[CODE]if(xmlHttp.readyState==4)
{
alert(xmlHttp.responseText); //To make sure you are getting a reply (then remove)
document.getElementById("event_table_area").innerHTML=xmlHttp.responseText;
}[/CODE]


and still getting no response.
Copy linkTweet thisAlerts:
@BeanisApr 03.2007 — This would be easier if I had PHP installed on this machine...

Did the alert box show up? If it didn't show up the AJAX call was likely never made. If it did show up was it empty?

It doesn't look like event_list_table.func does anything. It is just a bunch of function declarations. You need to either call one of the functions or just display some text outside of a function to make sure the AJAX getting called properly.
Copy linkTweet thisAlerts:
@lightnbauthorApr 03.2007 — I had the wrong path to "event_list_table.func" in the ajax_basic.js file.

Now that I corrected it, I get an alert box with an ERROR 500 Internal Server Error.

I'll try sticking an echo command inside event_list_table to see if I can get some output from it.

Live Testing Site


Thanks,

Nick
Copy linkTweet thisAlerts:
@BeanisApr 03.2007 — Try renaming event_list_table.func to event_list_table.php, and updating the code where appropriate.

It might be an invalid filetype and PHP doesn't even attempt to process it. You can include .func (or anything) inside a php file, but servers will restrict the types of files a user can actually access.
Copy linkTweet thisAlerts:
@lightnbauthorApr 03.2007 — Alright, it's almost working. I had custom mime types setup in .htaccess for the .func extension, but I'm guessing that it's folder specific.

Anyway, I'm using .PHP now, and it's mostly working. the only problem is, it doesn't seem to be sending the value of the text text box to the form.

Do you use:

[code=php]$event_name_IP_string = $HTTP_POST_VARS['event_name_filter'];[/code]

like normal? or do you have to do something special to get the value?
Copy linkTweet thisAlerts:
@BeanisApr 03.2007 — You need to send the POST header in the request. Your current AJAX function isn't connected to the form, and is just sending an empty POST header. I can't remember how to do that off the top of my head, but if you search for AJAX POST scripts you should be a good skeleton to work off of.
Copy linkTweet thisAlerts:
@paradoxperfectApr 03.2007 — For POST header requests...

var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
function ajaxPost(oParameters){
var valsToSend = "id="+oParameters; // Example
var url = "myajaxpage.php";
xmlhttp.open("POST",url, true)
xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"); // Resolve any cache problems...
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); // Required for POST requests.
xmlhttp.onreadystatechange = someAjaxHandleFunction;
xmlhttp.send(toSend);
}


Should work fine using that... ?
Copy linkTweet thisAlerts:
@lightnbauthorApr 03.2007 — I'm using:

[CODE]function ajaxFunction()
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}

var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
function ajaxPost(oParameters){
var valsToSend = "id="+oParameters; // Example
var url = "../../include/ajax/event_list_table.php";
xmlhttp.open("POST",url, true)
xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"); // Resolve any cache problems...
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); // Required for POST requests.
xmlhttp.onreadystatechange = function()
{
if(xmlHttp.readyState==4)
{
// alert(xmlHttp.responseText); //To make sure you are getting a reply (then remove)
document.getElementById("event_table_area").innerHTML=xmlHttp.responseText;
}
}
xmlhttp.send(toSend);
}
}[/CODE]


And it's switched back to doing nothing. I must have broken something in the process?
Copy linkTweet thisAlerts:
@lightnbauthorApr 04.2007 — Is "oParameters" a built in variable, or am I supposed to put something else on that line?

[CODE]var valsToSend = "id="+oParameters; // Example"[/CODE]
Copy linkTweet thisAlerts:
@lightnbauthorApr 05.2007 — Actually it looks like this function is never called... Is this supposed to be called by the onkeyup event? And what do I pass to it as the oParameters argument?

[CODE]
function ajaxPost(oParameters)
{
var valsToSend = "id="+oParameters; // Example
[/CODE]
Copy linkTweet thisAlerts:
@lightnbauthorApr 05.2007 — Ok, I can get it as far as echoing "event_name_filter=d" (where 'd' is what I typed in the textbox) using 'alert(toSend);'.

I'm assuming this is what the 'toSend' value should be?

When I comment out alert(toSend);, I get nothing.

[code=php]
function filter_datas()
{
event_name_filter = document.getElementById('event_name_filter');
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}

var toSend = 'event_name_filter=' + event_name_filter.value;

// alert(toSend);
var url = "../../include/ajax/event_list_table_ajax.php";

xmlhttp.open("POST",url, true)

xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"); // Resolve any cache problems...
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); // Required for POST requests.

xmlhttp.onreadystatechange = function()
{
if(xmlHttp.readyState==4)
{
alert(xmlHttp.responseText); //To make sure you are getting a reply (then remove)
document.getElementById("event_table_area").innerHTML=xmlHttp.responseText;
}
}
xmlhttp.send(toSend);

}
[/code]
Copy linkTweet thisAlerts:
@lightnbauthorApr 06.2007 — Found a tutorial on IBM's website, and made some corrections. Still no response though. I like PHP so much better because when you make a mistake, the whole thing refuses to load and it gives a big ugly "PARSE ERROR" message. This whole 'doing nothing thing' makes it hard to troubleshoot. Anyway, here's my current code:

[code=php]function filter_datas()
{
event_name_filter = document.getElementById('event_name_filter');

var toSend = 'event_name_filter=' + event_name_filter.value;

var url = "../../include/ajax/event_list_table_ajax.php?" + escape(toSend)";

xmlhttp.open("GET", url, true)
xmlhttp.onreadystatechange = updatePage;
xmlhttp.send(null);
}

function updatePage()
{
if (request.readyState == 4)
{
if (request.status == 200)
{
alert(xmlHttp.responseText); //To make sure you are getting a reply (then remove)
document.getElementById("event_table_area").innerHTML=xmlHttp.responseText;
}
else
{
alert("status is " + request.status);
}
}
}[/code]
Copy linkTweet thisAlerts:
@BeanisApr 06.2007 — Is the xmlhttp object still getting created somewhere?

What is the request variable (request.readyState, request.status)? Shouldn't that be xmlhttp?

Also you have xmlhttp and xmlHttp, which would be two different variables.
Copy linkTweet thisAlerts:
@ricpApr 06.2007 — Could I just say, what an utterly bizarre way of doing things.

On the server, serve the data, via xml down to the client. On the client, via xmlhttp pick up the xml and use either DOM methods or xpath to parse the sent XML and then (and only then) build up the actual HTML.

How you are doing it is only increasing the amount of bandwidth (as you send down the HTML in the responseText) and making the whole thing hugely inflexible (say you wish to change some rendering on the client you need to amend the server side script and not the actual bit of the code that outputs the HTML).
Copy linkTweet thisAlerts:
@lightnbauthorApr 06.2007 — Is the xmlhttp object still getting created somewhere?

What is the request variable (request.readyState, request.status)? Shouldn't that be xmlhttp?

Also you have xmlhttp and xmlHttp, which would be two different variables.[/QUOTE]


Yes, at the top. I changed the variable in those items from request to xmlhttp, and made all references into xmlhttp so there the same varaiable.

This is the current code:

[code=php]
try
{
// Firefox, Opera 8.0+, Safari
xmlhttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}

function filter_datas()
{
event_name_filter = document.getElementById('event_name_filter');

var toSend = 'event_name_filter=' + event_name_filter.value;

var url = "../../include/ajax/event_list_table_ajax.php?" + escape(toSend)";

xmlhttp.open("GET", url, true)
xmlhttp.onreadystatechange = updatePage;
xmlhttp.send(null);

}

function updatePage()
{
if (xmlhttp.readyState == 4)
{
if (xmlhttp.status == 200)
{
alert(xmlhttp.responseText); //To make sure you are getting a reply (then remove)
document.getElementById("event_table_area").innerHTML=xmlhttp.responseText;
}
else
{
alert("status is " + xmlhttp.status);
}
}
}
[/code]
Copy linkTweet thisAlerts:
@lightnbauthorApr 06.2007 — Could I just say, what an utterly bizarre way of doing things.

On the server, serve the data, via xml down to the client. On the client, via xmlhttp pick up the xml and use either DOM methods or xpath to parse the sent XML and then (and only then) build up the actual HTML.

How you are doing it is only increasing the amount of bandwidth (as you send down the HTML in the responseText) and making the whole thing hugely inflexible (say you wish to change some rendering on the client you need to amend the server side script and not the actual bit of the code that outputs the HTML).[/QUOTE]


I'm not sure what DOM methods or xpath is/are. I have a script on the server side the runs a database query, and generates the HTML page. Is there a better way to do it?

I have an end result in mind, but in terms of coding I'm making this up / learning as I go.
Copy linkTweet thisAlerts:
@ricpApr 06.2007 — Well I suppose it was unfair of me to throw that big spanner in the works, especially as you were in the process of sorting your problems, but..

I'm sure you are familiar with [I]document.getElementById("myElementID");[/I] well the same is available from the XMLDOM. That along with getElementsByTagName plus numerous other methods/properties.

Perhaps have a look at the MSDN docs (while some of it is proprietary it is probably the most detailed DOM/HTML docs reference on the web):

http://msdn2.microsoft.com/en-us/library/ms533050.aspx

As for XPath, it's part of the XSL specifications, but you can use it with the selectNodes / selectSingleNode methods. These are native to the IE XMLDOM but you need to apply a little magic to make it work with other browsers:

http://km0ti0n.blunted.co.uk/mozXPath.xap

You can find a tutorial on xpath at w3schools - http://www.w3schools.com/xpath/default.asp

What you are doing just now is passing down a big lump of plain text (your HTML) and parsing that, when you could be passing down only the selected information needed for the client to build up the html it needs. When you send your content back, if it is valid XML then simply set the mime-type to [I]text/xml[/I] then you can use the [B]responseXML[/B] property (which will be the XMLDOM object of the xml you send back) of the returned xmlhttp object rather than the responseText.
Copy linkTweet thisAlerts:
@lightnbauthorApr 06.2007 — Ok, I'm reading about XML on W3C Schools website, and it makes sense, it's a mark-up language for data structure.

What I'm still unsure of is how everything works together.

I have:

a MySQL Database

a PHP script that queries that database

an XML file that holds data (Which is dynamically generated by the PHP script??)

a CSS file to display the data in a neat and pretty fashion

a JavaScript file to build/send/receive server requests

a PHP script that recieves the JS request, re-queries the DB, and sends data back to the JS??



EDIT: I just want to make sure were on the same page. The purpose of the page is to:


Display a list of events and information about the events (that's in a MySQL D? in an HTML table


Allow the user to type keywords into an input field that dynamically reduces the number of results un the table by running a new query when a letter is typed
Copy linkTweet thisAlerts:
@BeanisApr 06.2007 — This looks like it could cause a problem:
[CODE][COLOR=#000000][COLOR=#007700]var [/COLOR][COLOR=#0000bb]url [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#dd0000]"../../include/ajax/event_list_table_ajax.php?" [/COLOR][COLOR=#007700]+ [/COLOR][COLOR=#0000bb]escape[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]toSend[/COLOR][COLOR=#007700])[/COLOR][COLOR=#dd0000]";[/COLOR][/COLOR][/CODE]
Escaping toSend will cause the "=" to be escaped, which you don't want. You can use escape on the line before
[CODE][COLOR=#000000][COLOR=#007700]var [/COLOR][COLOR=#0000bb]toSend [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#dd0000]'?event_name_filter=' [/COLOR][COLOR=#007700]+ [/COLOR][COLOR=#0000bb]escape(event_name_filter[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]value[/COLOR][COLOR=#007700]);
[/COLOR][/COLOR][COLOR=#000000][COLOR=#007700]var [/COLOR][COLOR=#0000bb]url [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#dd0000]"../../include/ajax/event_list_table_ajax.php" [/COLOR][COLOR=#007700]+ [/COLOR][COLOR=#007700][/COLOR][COLOR=#0000bb]toSend[/COLOR][COLOR=#dd0000];[/COLOR][/COLOR][/CODE]
Copy linkTweet thisAlerts:
@lightnbauthorApr 06.2007 — Beanis:

I made the corrections as indicated, but still no reaction.
Copy linkTweet thisAlerts:
@BeanisApr 06.2007 — This should work. I removed a return and an extra }
<i>
</i>try {
// Firefox, Opera 8.0+, Safari
xmlhttp=new XMLHttpRequest();
} catch (e) {
// Internet Explorer
try {
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
alert("Your browser does not support AJAX!");
}
}
}

function filter_datas()
{
event_name_filter = document.getElementById('event_name_filter');
var toSend = 'event_name_filter=' + escape(event_name_filter.value);
var url = "../../include/ajax/event_list_table_ajax.php?" + escape(toSend);
xmlhttp.open("GET", url, true);
xmlhttp.onreadystatechange = updatePage;
xmlhttp.send(null);
}

function updatePage()
{
if (xmlhttp.readyState == 4) {
alert(xmlhttp.responseText); //To make sure you are getting a reply (then remove)
document.getElementById("event_table_area").innerHTML=xmlhttp.responseText;
} else {
alert("status is " + xmlhttp.status);
}
}
Copy linkTweet thisAlerts:
@lightnbauthorApr 07.2007 — Thanks Beanis! It does something now ?

I made a slight modification to the last part, since it was creating an alert box that said "Status is 200" every time the readystate was not equal to 4. Modified code reads:

[code=php]
function updatePage()
{
if (xmlhttp.readyState == 4)
{
if(xmlhttp.status == 200)
{
alert(xmlhttp.responseText); //To make sure you are getting a reply (then remove)
document.getElementById("event_table_area").innerHTML=xmlhttp.responseText;
}
else
{
alert("status is " + xmlhttp.status);
}
}
} [/code]


As you can see from the Live Site, it sends the request and recieves a response, but something is getting lost in translation. After the request, the form and the entire table disappear!
Copy linkTweet thisAlerts:
@BeanisApr 07.2007 — I pasted something incorrectly above.
[CODE]var url = "../../include/ajax/event_list_table_ajax.php?" + escape(toSend);[/CODE]
should be
[CODE]var url = "../../include/ajax/event_list_table_ajax.php?" + toSend;[/CODE]

That should fix the get request, but I'm not sure if it will fix the problem.
Copy linkTweet thisAlerts:
@lightnbauthorApr 07.2007 — The variable is getting to the PHP script now. I have an echo command setup to echo the variable it recieves before doing anything else.

It's still causing the whole table and form to disapear though. I'm guessing the problem must lie in the PHP code somewhere?
Copy linkTweet thisAlerts:
@lightnbauthorApr 07.2007 — I've looked closer at the PHP script, and it is generating the query corectly and returning a result (to my Alert box). It just isn't implementing it correctly into the page, or so it seems.
Copy linkTweet thisAlerts:
@lightnbauthorApr 10.2007 — Ok, now that I'm using FireBug, I've got a more detailed error:

[CODE][Exception... "'Permission denied to set property XULElement.selectedIndex' when calling method: [nsIAutoCompletePopup::selectedIndex]" nsresult: "0x8057001e (NS_ERROR_XPC_JS_THREW_STRING)" location: "JS frame :: http://www.mysite.com/include/ajax/ajax_basic.js :: update_page :: line 4" data: no]
http://www.mysite.com/include/ajax/ajax_basic.js
Line 4[/CODE]


What does that mean? Permission denied?
Copy linkTweet thisAlerts:
@lightnbauthorApr 10.2007 — UPDATE: It seems that this is a bug with the auto-complete function in FireFox. Turning "autocomplete='off'" makes the error go away.

I'm not getting any FireBug errors anymore, but am having the same problem my other ajax script is having. That is: the response shows up in FireBug, but not in the .responseText where it is supposed to!
×

Success!

Help @lightnb spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...