Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Http request open POST instead of GET

Status
Not open for further replies.

Zhris

Programmer
Aug 5, 2008
254
0
0
GB
Here is a section of my code:

Code:
function fAccess() {

	var message=document.getElementById('message').value;
	document.getElementById('message').value = "";
	document.getElementById('message').focus();
    var url="bottom_execute.pl?Message="+message+"&Random="+Math.random();
    xhr.open("GET",url,false);
    xhr.send(null);
    document.getElementById("DisplayA").innerHTML=xhr.responseText;

}

The problem I have is that sometimes the message will contain URL's which may be in the format: "
They are then sent to the php script in the format: "script.php?message=
However the script reads this format like:
script.php
?
message=&
key2=value2

Instead of:
script.php
?
message=
key2=value2 is no longer part of message.

I have tried replacing xhr.open("GET",url,false); with xhr.open("POST",url,false); assuming this would have fixed the problem, however message is not sent to the php script. Can this be fixed in the javascript or is this a php issue?

Any help will be much appreciated.

Thanks,

Chris
 
It's something easily fixed by escaping the ampersands in the initial URL. You would do this in javascript, and then would need to also un-escape them in your script server-side.

Code:
var param = "[URL unfurl="true"]http://www.tek-tips.com/viewthread.cfm?qid=1511561&page=1";[/URL]
escapedParam = [!]escape(param)[/!];
// escapedParam is now "http%3A//[URL unfurl="true"]www.tek-tips.com/viewthread.cfm%3Fqid%3D1511561%26page%3D1"[/URL]
var url = "[URL unfurl="true"]http://www.example.com/index.php?message="[/URL] + escapedParam;

In php you might do this:

Code:
$param = $_GET['message'];
$unescapedParam = urldecode($param);

Hope this is workable for you.

Cheers,
Jeff

[tt]Visit my blog [!]@[/!] Visit Code Couch [!]@[/!] [/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
it is a URL issue I believe...

message will read everything up to the next & sign.

I would recommend escaping the url for message and then unescape it in the php script.

so...
Code:
var oldMessage = '[URL unfurl="true"]http://domain.com/index.php?key=value&key2=value2';[/URL]

var newMessage = escape(oldMessage);

Then in your PHP script, I believe the function is called urldecode to get the URL back how it should be.

If that doesn't work, I hope this gives you some direction at least.
 
Thank you for your response(s),

I've been working with PHP all day and had it in my mind at the time of posting. I'm actually working with a Perl backend script but anyhow decoding isn't a problem (in fact when I first tested I didn't even need to decode :s).

Escaping works very nicely. However the information (message) is stored to a plain txt file and it seems as if a newline character is included to the end after encoding. I have tried using message = str.replace(/^\s+|\s+$/g, ''); to remove the newline after trying other numerous trimming methods, however none work.

Any suggestions?

Chris
 
Ok I should have tried it before, but I have it working now. I removed the newline character using Perl's chomp() which I had previously assumed wouldn't work due to the possiblity that javascript/perl newline characters would be different (don't ask me why!).

Still interested to know the reason why I don't need to decode the string again in the Perl script?

Thank you both for your help, its very much appreciated.

Chris
 
I've just realised it creates a new line if processed in Internet Explorer (wrong) but doesn't in Firefox (correct). I'm working on it but any suggestions in the mean time would be appreciated.

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top