SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Location
    Florida
    Posts
    58
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

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

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

    Code Java:
    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?

  2. #2
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    The problem is that the two words you're typing into the text box aren't being escaped properly.

    Try this instead:

    Code JavaScript:
    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

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Location
    Florida
    Posts
    58
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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

  4. #4
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    Awesome
    It's much easier this way than to build the string yourself.
    In your original code, you could have done this too:

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

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Location
    Florida
    Posts
    58
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •