连接后,Filepath和它的URL变量会被缩短

I have the following JQuery AJAX function:

function JQueryAJAXFunction(){
$input = document.getElementById("textInput").value;
console.log($input ); //Console output: "How to"
$('#resultsOutput').load('ajax/serverside/filepath.php?urlvariable='+$input);
} 

It retrieves the value of $input from text field, which is a two letter word such as "How to" and displays it on the browser's console.

$input is then concatenated with the file path for the server side script and is assigned as a URL variable. However, in doing so the last word of $input -in this case "to"- disappears/is cut off.

Once the function is executed, the URL variable on the server is retrieved and displayed using for example: $_GET["urlvariable"] It's value is as you can guess "How".

What is the recommended solution to the problem? How can I get $_GET["urlvariable"] be equal to "How to".

Certain characters need to be encoded when used in URLs...a space is one of them. JavaScript provides a global function to encode any necessary characters for URLs - encodeURIComponent. Instead of just concatenating $input, you would concatenate:

encodeURIComponent($input)

Reference: