PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a PHP Expert!

Join 300,362 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,483 people online right now. Registration is fast and FREE... Join Now!




Simple AJAX implementation not working

2 Pages V  1 2 >  

Simple AJAX implementation not working

livium

1 Jul, 2009 - 06:15 AM
Post #1

D.I.C Regular
***

Joined: 21 Dec, 2008
Posts: 294


My Contributions
I have this code in my main html page which creates an input text. Onkeyup it should execute a javascript function named showHint.

CODE

<div id="apDiv26">
        <?php
    
     echo '<input type="text" size="35" name="search2" id="search2" onkeyup="java script:showHint(this.value)" value="'.$_SESSION['search_text'].'" />';
     ?>
        
        </div>


i use this syntax "java script:showHint(this.value)" on another javascript function which is in the same file as this one and it works. So i don't see where's the error here.

the javascript function show hint looks like this

CODE





var xmlHttp;
function showHint(str)
{
if (str.length<=2)
  {
  document.getElementById("apDiv45").innerHTML="";
  return;
  }
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  }

var url="gethint.php";
url=url+"?search="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function stateChanged()
{
if (xmlHttp.readyState==4)
{
document.getElementById("apDiv45").innerHTML=xmlHttp.responseText;
}
}

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;
}




and gethint.php like this (it just echoes 'this is a test', too be sure that here there's no error):

CODE

<?php
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
echo 'this is a test';
?>




so... the codes above should put me in a div (apDiv45) 'this is a test' when i pres a key on my keybord, but it does nothing.
Do you have any ideea where's the mistake?

I know see that in works only in IE7. Not in Firefox.

What must i change to work in firefox too?

This post has been edited by livium: 1 Jul, 2009 - 06:15 AM

User is offlineProfile CardPM
+Quote Post


RudiVisser

RE: Simple AJAX Implementation Not Working

1 Jul, 2009 - 06:21 AM
Post #2

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,872



Thanked: 137 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
CODE
onkeyup="java script:showHint(this.value)"


That won't work...

onkeyup accepts a javascript embedded code string anyway, so you don't need the java script: part. FYI java script: wouldn't work ever.
User is offlineProfile CardPM
+Quote Post

livium

RE: Simple AJAX Implementation Not Working

1 Jul, 2009 - 06:24 AM
Post #3

D.I.C Regular
***

Joined: 21 Dec, 2008
Posts: 294


My Contributions
QUOTE(MageUK @ 1 Jul, 2009 - 06:21 AM) *

CODE
onkeyup="java script:showHint(this.value)"


That won't work...

onkeyup accepts a javascript embedded code string anyway, so you don't need the java script: part. FYI java script: wouldn't work ever.



it's not that. it's this editor's fault.

It doesn't work in firefox even without javascript. But in IE it works.

This post has been edited by livium: 1 Jul, 2009 - 06:25 AM
User is offlineProfile CardPM
+Quote Post

livium

RE: Simple AJAX Implementation Not Working

1 Jul, 2009 - 06:42 AM
Post #4

D.I.C Regular
***

Joined: 21 Dec, 2008
Posts: 294


My Contributions
i've tried onkeypress, onkeydown and it still doesn't work in FF. It works only in IE7.
User is offlineProfile CardPM
+Quote Post

RudiVisser

RE: Simple AJAX Implementation Not Working

1 Jul, 2009 - 06:43 AM
Post #5

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,872



Thanked: 137 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
Well it works perfectly fine here, I've got rid of the horrible function that I also saw copied into a client's website today sad.gif

But anyway yeah, check out the code behind this, works fine (and is much simpler)
Demonstration: http://mageuk.com/dic/livium.php
Source: http://mageuk.com/dic/source.php?f=livium.php
User is offlineProfile CardPM
+Quote Post

ShaneK

RE: Simple AJAX Implementation Not Working

1 Jul, 2009 - 06:51 AM
Post #6

require_once("brain.php"); //Fatal error :/
Group Icon

Joined: 10 May, 2009
Posts: 699



Thanked: 47 times
Dream Kudos: 75
Expert In: PHP, MySQL

My Contributions
QUOTE(livium @ 1 Jul, 2009 - 07:24 AM) *

QUOTE(MageUK @ 1 Jul, 2009 - 06:21 AM) *

CODE
onkeyup="java script:showHint(this.value)"


That won't work...

onkeyup accepts a javascript embedded code string anyway, so you don't need the java script: part. FYI java script: wouldn't work ever.



it's not that. it's this editor's fault.

It doesn't work in firefox even without javascript. But in IE it works.


It would never work without Javascript because they're javascript functions...so I don't say why you'd say "even without javascript".

Yours,
Shane~
User is offlineProfile CardPM
+Quote Post

RudiVisser

RE: Simple AJAX Implementation Not Working

1 Jul, 2009 - 06:53 AM
Post #7

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,872



Thanked: 137 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
I think he meant the prefix in the event attribute.
User is offlineProfile CardPM
+Quote Post

livium

RE: Simple AJAX Implementation Not Working

1 Jul, 2009 - 06:58 AM
Post #8

D.I.C Regular
***

Joined: 21 Dec, 2008
Posts: 294


My Contributions
Thanks you very much for your effort. I've tried your code but it doesn't work still in firefox, only in IE.
My javascript function is found on w3schools and everywhere on the internet. It is something standart i guess, and it worked in firefox on a similar implementation, when i made an ajax tutorial some time ago. But now i just don't know why it doesn't work anymore. I thinks it's something wrong with firefox ( a setting or something).

This post has been edited by livium: 1 Jul, 2009 - 06:59 AM
User is offlineProfile CardPM
+Quote Post

ShaneK

RE: Simple AJAX Implementation Not Working

1 Jul, 2009 - 07:04 AM
Post #9

require_once("brain.php"); //Fatal error :/
Group Icon

Joined: 10 May, 2009
Posts: 699



Thanked: 47 times
Dream Kudos: 75
Expert In: PHP, MySQL

My Contributions
Try getting Firebug for Firefox and seeing if it outputs any errors...

Yours,
Shane~
User is offlineProfile CardPM
+Quote Post

livium

RE: Simple AJAX Implementation Not Working

1 Jul, 2009 - 07:05 AM
Post #10

D.I.C Regular
***

Joined: 21 Dec, 2008
Posts: 294


My Contributions
I have checked my ff settings and javascript is enabled, so this is not the case.
User is offlineProfile CardPM
+Quote Post

ShaneK

RE: Simple AJAX Implementation Not Working

1 Jul, 2009 - 07:06 AM
Post #11

require_once("brain.php"); //Fatal error :/
Group Icon

Joined: 10 May, 2009
Posts: 699



Thanked: 47 times
Dream Kudos: 75
Expert In: PHP, MySQL

My Contributions
Um..that's not what I meant...
Firebug is an addon for Firefox that lets you do a lot of useful stuff, it also helps debug javascript a lot of the time.

Yours,
Shane~
User is offlineProfile CardPM
+Quote Post

RudiVisser

RE: Simple AJAX Implementation Not Working

1 Jul, 2009 - 07:10 AM
Post #12

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,872



Thanked: 137 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
http://www.mageuk.com/dic/itworksfine.avi

As the video name would suggest, it works fine (dot ay vee eye).

ShaneK, if you use Firefox (you poor poor soul sad.gif) try out my example and see if it works..

This post has been edited by MageUK: 1 Jul, 2009 - 07:11 AM
User is offlineProfile CardPM
+Quote Post

livium

RE: Simple AJAX Implementation Not Working

1 Jul, 2009 - 07:19 AM
Post #13

D.I.C Regular
***

Joined: 21 Dec, 2008
Posts: 294


My Contributions
QUOTE(MageUK @ 1 Jul, 2009 - 07:10 AM) *

http://www.mageuk.com/dic/itworksfine.avi

As the video name would suggest, it works fine (dot ay vee eye).

ShaneK, if you use Firefox (you poor poor soul sad.gif) try out my example and see if it works..


i know that your code works in firefox and ie, but if i put your js function in my project is doesnt work in ff.
User is offlineProfile CardPM
+Quote Post

RudiVisser

RE: Simple AJAX Implementation Not Working

1 Jul, 2009 - 07:21 AM
Post #14

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,872



Thanked: 137 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
If you copy the exact function over all of your javascript code it doesn't work?? Are you sure??

If you are can you post the full page's source code (and any related javascript files) or just a link to your files that are uploaded.
User is offlineProfile CardPM
+Quote Post

ShaneK

RE: Simple AJAX Implementation Not Working

1 Jul, 2009 - 07:22 AM
Post #15

require_once("brain.php"); //Fatal error :/
Group Icon

Joined: 10 May, 2009
Posts: 699



Thanked: 47 times
Dream Kudos: 75
Expert In: PHP, MySQL

My Contributions
QUOTE(MageUK @ 1 Jul, 2009 - 08:10 AM) *

http://www.mageuk.com/dic/itworksfine.avi

As the video name would suggest, it works fine (dot ay vee eye).

ShaneK, if you use Firefox (you poor poor soul sad.gif) try out my example and see if it works..


Actually I use Chrome most of the time (I live the Google life wink2.gif just need an Android XD) but I have firefox which I use for debugging Javascript tongue.gif

(And yes, your script did work)

Yours,
Shane~

This post has been edited by ShaneK: 1 Jul, 2009 - 07:23 AM
User is offlineProfile CardPM
+Quote Post

livium

RE: Simple AJAX Implementation Not Working

1 Jul, 2009 - 07:23 AM
Post #16

D.I.C Regular
***

Joined: 21 Dec, 2008
Posts: 294


My Contributions
i have used firebug and in the page source (ff) it puts me what it puts me in div45 in IE, i mean this:"this is a test'"

But why in IE it puts the text in div45 and in FF not?

This post has been edited by livium: 1 Jul, 2009 - 07:26 AM
User is offlineProfile CardPM
+Quote Post

RudiVisser

RE: Simple AJAX Implementation Not Working

1 Jul, 2009 - 07:27 AM
Post #17

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,872



Thanked: 137 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
Because Firefox isn't a competent browser.

But more importantly that's absolutely nothing to do with Javascript it's a PHP error, you're just not giving an index to which column you want to select and are passing an array instead.

Anyway, since you haven't posted all of your code, how do you expect us to help you? The example I give you works perfectly fine, I've proved this to you and you yourself said that it worked.

This post has been edited by MageUK: 1 Jul, 2009 - 07:35 AM
User is offlineProfile CardPM
+Quote Post

livium

RE: Simple AJAX Implementation Not Working

1 Jul, 2009 - 07:38 AM
Post #18

D.I.C Regular
***

Joined: 21 Dec, 2008
Posts: 294


My Contributions
my code is the first one posted. So, it puts me 'this is a test' in ie but not in ff. In ff it puts this text only in page source which i view through firebug. So it cannot be a php error.
User is offlineProfile CardPM
+Quote Post

RudiVisser

RE: Simple AJAX Implementation Not Working

1 Jul, 2009 - 07:44 AM
Post #19

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,872



Thanked: 137 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
You said that you tried my code, and my code is clearly not what you posted there.
User is offlineProfile CardPM
+Quote Post

livium

RE: Simple AJAX Implementation Not Working

1 Jul, 2009 - 08:26 AM
Post #20

D.I.C Regular
***

Joined: 21 Dec, 2008
Posts: 294


My Contributions
I've tried your code and i had the same problem. So i stick with mine then.
Why don't you try my javascript function and see that it works on your code. I bet it works.
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 08:14PM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month