/    Sign up×
Community /Pin to ProfileBookmark

XMLHTTP Object – POST vars to php

so, i got 2 pages

index.php:

[code=php]
<script type=”text/javascript” language=”JavaScript”>
<!–
var xmlhttp = false;

if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
xmlhttp.overrideMimeType(‘text/xml’);
} else if (window.ActiveXObject) {
xmlhttp = new ActiveXObject(” Microsoft.XMLHTTP”);
}

function foo(id) {
if(id !== “”){
document.getElementById(‘result’).innerHTML = “Working…”;
var url = ‘process.php?id=’ + id;
xmlhttp.open(‘GET’, url, true);
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById(‘result’).innerHTML = xmlhttp.responseText;
} else {
document.getElementById (‘result’).innerHTML = ”;
}
};
xmlhttp.send(null);
}
}
//–>
</script>

<button onClick=”foo(‘hello’)”>Foo</button><hr>
<div id=”result”></div>[/code]

process.php:

[code=php]
POST: <? print_r($_POST) ?>
<br />
GET: <? print_r($_GET) ?>[/code]

i click FOO button, executing foo() function,
variable “id” is passes to “process.php” by embeding it in url aka GET method.
i then get results from that “process.php” page and print them into “result” div, which are the following:

[quote]

[b]POST[/b]: Array ( )
[b]GET[/b]: Array ( [id] => hello )

[/quote]

everything works as expected, but my question is how do i pass variables to the process.php page using POST method? aka so in result div i would get something like:

[quote]

[b]POST[/b]: Array ( [id] => hello )
[b]GET[/b]: Array ( )

[/quote]

thank you in advance ?

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@frifoxauthorSep 18.2005 — so its that noone knows the answer? or i didnt explain myself well enough...

i ment how can you pass variables in XMLHTTP Object thing using POST method?
×

Success!

Help @frifox 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.21,
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,
)...