/    Sign up×
Community /Pin to ProfileBookmark

xmlhttpRequest is driving me insane! Please help

Hello there,

I’m relatively new to Javascript so please forgive me if I’m making some obvious mistake here.

I’m just trying to get a simple form to send an xmlhttprequest to a php script which returns basic info (at this stage) based on the $_GET variable it receives.

It looks to me that I’m just not getting anything back from the server but the php is fine. At least, it works fine when not called by xmlhttprequest.

Any help would be greatly appreciated.

B

Here’s a link to the non-working example:

[url]http://www.invisibleforce.net/dragme_f/try.html[/url]

And here’s the HTML/javascript I’m using:

<code>
<!DOCTYPE html PUBLIC “-//WC3//DTD XHTML 1.0 Strinct//EN”
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd“>
<html>
<head><title>XMLHTTPOBJECT TEST</title>

<script type=”text/javascript”>
function getText() {
var url = “reply.php?keywords=” + document.keyform.keywords.value;
alert(url);
var request = GetXmlHttpObject();

request.onreadystatechange = function() {
if (request.readyState == 4 || request.readyState==”complete”){
alert(“ready: ” + readyState);
document.getElementById(“results”).innerHTML=request.responseText
}
}
if(request){
request.open(“GET”, url, false);
request.send(null);
alert(“sent”);
}else alert(“-NotWorking-“);

};
</script>

<script type=”text/javascript”>
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;
};
</script>

<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″/>
<link rel=”STYLESHEET” type=”text/css” href=”draggable.css”/>
</head>
<body>

<div class=”content”>

<form method=”GET” action =”” name=”keyform”>
keywords: <input type=”text” name=”keywords” id=”keywords” size=”50″/>
<input type=”submit” name=”submit” value=”submit” onclick=”getText()”/>
</form>

</div>
<div id=”results”>
RESULTS GO HERE!
</div>

</body></html>
</code>

And here’s the php:

<code>
<?php

if(isSet ($_GET[‘submit’])){
$f_search = $_
GET[‘keywords’];
echo “Well Done!: ” .$f_search;
}else echo “GET NOT SET”;

?>
</code>

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@A1ien51May 23.2008 — You are not cancelling the forms submit action so the page is probably posting back.

Either change the submit button to a regular button

or cancel the click with onclick

onclick="getText();return false;"

Eric
Copy linkTweet thisAlerts:
@barry_normalauthorMay 23.2008 — Thanks Eric, thanks for the insight.

So now I have the form like this:

<code>

<form method="GET" action ="" name="keyform">

keywords: <input type="text" name="keywords" id="keywords" size="50"/>

<input type="submit" name="submit" value="submit" onclick="getText();return false;"/>

</form>

</code>

But strangely all the alerts have stopped working and I still get no results back. It does stop the URL from changing though, so there might be some progress.

Could there be some setting on my server that's not working? There doesn't seem to be any logic to the alerts just stopping like that. They don't work now if I go back to the original version.

Once again any help would be really good.

Mark
Copy linkTweet thisAlerts:
@barry_normalauthorMay 23.2008 — Hi Eric, I've kind of solved it but not sure why:

I just changed the try.html to try.php and it works fine.

But I have no idea why. I hate it when things resolve in an inexplicable manner so if you know why, please tell me. There's no php in that file!

http://www.invisibleforce.net/dragme_f/try.php

Cheers

Mark
×

Success!

Help @barry_normal 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.16,
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,
)...