/    Sign up×
Community /Pin to ProfileBookmark

PHP/AJAX problem – DIV’s

Hi

I am trying a very simple AJAX script where when you click a button it sends a query string to a php file to echo the string into a div which is static at the moment.

Heres the javascript:

[CODE] // Change the value of the outputText field

function setOutput(){

if(httpObject.readyState == 4){

document.getElementById(‘outputText’).innerHTML = httpObject.responseText;

}

}

// Implement business logic

function doWork(){

httpObject = getHTTPObject();

if (httpObject != null) {

httpObject.open(“GET”, “ajaxvote.php?inputText=hello”;

httpObject.send(null);

httpObject.onreadystatechange = setOutput;

}

}[/CODE]

Heres the button and the div

[CODE]<form name=”testForm”>

Input text: <input type=”button” value=”TD” onchange=”doWork();” name=”inputText” id=”inputText” />

</form>
<div name=”outputText” id=”outputText”>&nbsp;</div>[/CODE]

And if you need it heres the script inside the php file

[CODE]if (isset($_GET[‘inputText’])){

echo strtoupper($_GET[‘inputText’]);
}

Thanks in advance
[/CODE]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@Sterling_IsfineAug 22.2009 — Two immediately apparent errors:
[CODE] function doWork(){

httpObject = getHTTPObject();

if (httpObject != null) {

httpObject.open("GET", "ajaxvote.php?inputText=hello" [B] [COLOR="Red"], true)[/COLOR][/B]; // missing )

httpObject.send(null);

httpObject.onreadystatechange = setOutput; [B]//Must be installed before calling send[/B]

}

}[/CODE]

[/QUOTE]
Use the error console.
Copy linkTweet thisAlerts:
@SeanJenkinsauthorAug 23.2009 — Ok I didn't realise that ", true)" was missing so I replaced this

But with regards to

//Must be installed before calling send
[/QUOTE]


If you mean setting up the object then I already have this code before the code in my original post

[CODE]function getHTTPObject(){

if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");

else if (window.XMLHttpRequest) return new XMLHttpRequest();

else {

alert("Your browser does not support AJAX.");

return null;

}

}[/CODE]


But it still doesn't echo anything out?
Copy linkTweet thisAlerts:
@Sterling_IsfineAug 23.2009 — 


If you mean setting up the object then I already have this code before the code in my original post
[/QUOTE]
No, I meant that the [I]readstatechange[/I] handler should be installed before calling [I]send[/I], since after could be too late.
Copy linkTweet thisAlerts:
@SeanJenkinsauthorAug 23.2009 — Ok I think i'm a bit lost due to the fact I havn't done any site work in a while. So can someone please post a link for a good video tutorial.

Thanks
×

Success!

Help @SeanJenkins 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.19,
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,
)...