PDA

View Full Version : a strange URL encoding


davidklonski
03-26-2004, 01:51 AM
Hello guys

see if you can help me with this strange problem:
I have the following javascript function:

function changeNumberOfItems() {
var form = document.forms[0];
var totalNumberOfItems = form.numItems.value;
alert("\"" + totalNumberOfItems + "\"");
totalNumberOfItems = totalNumberOfItems.replace(/^\s+/,"");
totalNumberOfItems = totalNumberOfItems.replace(/\s+$/,"");
alert("\"" + totalNumberOfItems + "\"");
form.action = "browsing.php?page=1&itemsPerPage=25&numItems=" + totalNumberOfItems;
alert(form.action);
form.submit();
}

The value that I obtain from a form element (the bold one) comes from a textfield. I trim all whitespaces from around its value and the second alert shows me that indeed whitespaces have been removed.

The last alert shows me the URL encoding that the browser will request.
However, when the function executes, the actual URL that is displayed in the browser has an addtional + sign before the totalNumberOfItems value.
For example: instead of browsing.php?page=1&itemsPerPage=25&numItems=7
The URL in the address bar reads: browsing.php?page=1&itemsPerPage=25&numItems=+7

Does anyone know what I am doing wrong?

thanks

glenngv
03-26-2004, 02:21 AM
Are you really sure you don't have a space in the "numItems" parameter?

form.action = "browsing.php?page=1&itemsPerPage=25&numItems=(space)" + totalNumberOfItems;

But in any case, you should still trim it in the server-side (php in your case) as users can copy the url and insert space in it before sending the request. Actually not only trimming but also validating and putting default values if parameters have non-numeric values.