Jquery - passing text form input, missing the second word?

<script src=“http://code.jquery.com/jquery-latest.js”></script> //for reference

function nameList(){
	url ="post-json.php?action=search&keyword="+$("#keyword").val()+"&additionalInfo=whatever";
	$("#modaldisplay").load(url);
	alert(url);
}

Normally this works, the problem is when i write two words in the input box the value isnt getting passed properly to the php side.

alert(url) = post-json.php?action=search&listName=two words&additionalInfo=whatever

error_log("REQUEST_URI = ".$_SERVER[‘REQUEST_URI’]);
error_log("Keyword = ".$_REQUEST[‘keyword’]);

REQUEST_URI = /post-json.php?action-search&keyword=two
Keyword = two

any one know how to get both words from the input box?

The problem is that the two words you’re typing into the text box aren’t being escaped properly.

Try this instead:


function nameList(){
    var url ="post-json.php?";
    var params = {
        action: 'search',
        keyword: $("#keyword").val(),
        additionalInfo: 'whatever'
    };
    $("#modaldisplay").load(url, params);
}

This will create an object for the request parameters which is passed as the second argument to jQuery’s load function.
jQuery will take care of escaping the contents for you now :slight_smile:

Thank you, Jquery (or java anything for that matter) isnt one of my strongest points, I have been playing with serialize, jsonEncode and quite a few other things for a long time now trying to get that to work. I would have never imagined it would be as simple as setting values similar to get/postJSON (which I have done quite a bit)

I copied your code and put it back together with the proper params and inside my original function it worked the first time

Awesome :wink:
It’s much easier this way than to build the string yourself.
In your original code, you could have done this too:


var url ="post-json.php?action=search&keyword="+encodeURIComponent($("#keyword").val())+"&additionalInfo=whatever";

I am definitely a bigger fan of setting the “params” . I will have to keep encodeURIComponent in mind though. Coming from PHP its things like parseInt() or encodeURIComponent() you don’t even ever think about.