ked odosielas nejake hodnoty cez javascript, tak v query stringu musis tie hodnoty prehnat urlencode(), aby ti to zobralo spravne vsetko, aj medzery etc., napr.:
Kód: Vybrať všetko
function urlencode(str) {
str = encodeURIComponent(str);
str = str_replace(str,'+', '%2B');
str = str_replace(str,'%20', '+');
str = str_replace(str,'*', '%2A');
str = str_replace(str,'/', '%2F');
str = str_replace(str,'@', '%40');
return str;
}
a potom vo funkcii, napr.:
Kód: Vybrať všetko
function password(passwd)
{
var request = getHTTPObject(); //vrati XHR
request.onreadystatechange=function()
{
if ((request.readyState==4) && (request.status==200))
{
return true;
}
}
request.open("POST","password.php",true);
request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
data="password="+urlencode(passwd);
request.send(data);
}
takto to robim ja
//edit: este ta funkcia str_replace:
Kód: Vybrať všetko
function str_replace(inputString, fromString, toString) {
// Goes through the inputString and replaces every occurrence of fromString with toString
var temp = inputString;
if (fromString == "") {
return inputString;
}
if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
while (temp.indexOf(fromString) != -1) {
var toTheLeft = temp.substring(0, temp.indexOf(fromString));
var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
temp = toTheLeft + toString + toTheRight;
}
} else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
var midStrings = new Array("~", "`", "_", "^", "#");
var midStringLen = 1;
var midString = "";
// Find a string that doesn't exist in the inputString to be used
// as an "inbetween" string
while (midString == "") {
for (var i=0; i < midStrings.length; i++) {
var tempMidString = "";
for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
if (fromString.indexOf(tempMidString) == -1) {
midString = tempMidString;
i = midStrings.length + 1;
}
}
} // Keep on going until we build an "inbetween" string that doesn't exist
// Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
while (temp.indexOf(fromString) != -1) {
var toTheLeft = temp.substring(0, temp.indexOf(fromString));
var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
temp = toTheLeft + midString + toTheRight;
}
// Next, replace the "inbetween" string with the "toString"
while (temp.indexOf(midString) != -1) {
var toTheLeft = temp.substring(0, temp.indexOf(midString));
var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
temp = toTheLeft + toString + toTheRight;
}
} // Ends the check to see if the string being replaced is part of the replacement string or not
return temp; // Send the updated string back to the user
}