/    Sign up×
Community /Pin to ProfileBookmark

Passing parameters to PHP script via xmlhttp.open()

Hello all,
I am having problems passing parameters to a PHP script using xmlhttp.open().
Here is the javascript code:

[CODE]
<script>
function cancel_jobs() {
if (! confirm(“Are you sure you want to cancel your job(s)?”))
{ return; }

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
alert( this.responseText);
}
};
xmlhttp.open(“GET”, “test.php?param1&param2&param3”, true);
xmlhttp.send();
}
</script>
[/CODE]

Here is the code in test.php

[CODE]<?php
$param_array = $_SERVER[‘argv’];
$php_file = $param_array[0];
$param1 = $param_array[1];
$param2 = $param_array[2];
$param3 = $param_array[3];

echo “php file: $php_filen”;
echo “param1: $param1n”;
echo “param2: $param2n”;
echo “param3: $param3n”;
?>[/CODE]

I am also getting a PHP Notice in the error_log file.

[CODE] PHP Notice: Undefined index: argv in /var/www/html/test.php on line 3[/CODE]

I also tried the following with the same results:

[CODE] xmlhttp.open(“GET”, “test.php param1 param2 param3”, true);
[/CODE]

Any help would be greatly appreciated.
Thanks in advance.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@rootJun 14.2017 — $_GET['param1'] when reading a GET operation, FYI it is better to POST a form than use a GET method.

If you are going to be accepting inputs to the server, look at filter_var() and the process of cleaning and filtering the input data.
Copy linkTweet thisAlerts:
@kwatts59authorJun 14.2017 — I figured it out from another post on stackoverflow.

Thanks for your help.?

Javascript:
[CODE]function cancel_jobs() {
if (! confirm("Are you sure you want to cancel your job(s)?"))
{ return; }

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
alert( this.responseText);
}
};
xmlhttp.open("POST", "test.php", true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send("param1=p1&param2=p2&param3=p3");
xmlhttp.send();
}[/CODE]


PHP:
[CODE]
<?php

$param1 = $_POST["param1"];
$param2 = $_POST["param2"];
$param3 = $_POST["param3"];

echo "param1: $param1n";
echo "param2: $param2n";
echo "param3: $param3n";

?>[/CODE]
Copy linkTweet thisAlerts:
@rootJun 15.2017 — You would only need send the form not a string.

You only need the .send() issued once otherwise you get a double form post.
×

Success!

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