admin管理员组

文章数量:1279241

I need to URI encode a form input, that is then serialized with a bunch of a hidden inputs and sent to a PHP file.. is it possible to somehow bine encodeURIComponent into this line?:

var landingCreate = $(this).serialize();

UPDATE:

Doing this for example:

var landingCreate = $(this).serialize()+"&enc="+encodeURIComponent($('input[name=\'longform\']').val());

and entering the url:

/

into the text box, returns the URL unchanged.. shouldnt it be converting all the dashes and slashes etc to hex codes?

UPDATE

Here is the full code.

<form id="createTokenLanding">
    <input type="text" name="longform" />
    <input type="hidden" name="domain" value="<?php echo rawurlencode($_SERVER['HTTP_HOST']); ?>" />
    <input type="hidden" name="useragent" value="<?php echo rawurlencode($_SERVER['HTTP_USER_AGENT']); ?>" />
    <input type="hidden" name="ip" value="<?php echo rawurlencode($_SERVER['REMOTE_ADDR']); ?>" />
    <input type="hidden" name="cookieuser" value="<?php echo rawurlencode($_COOKIE['littlr_user']); ?>" />
    <input type="submit" name="submit" value="Shorten" />
</form>

<div id="result">
123
</div>

<script type="text/javascript">
    $(document).ready(function(){
        $.ajaxSetup ({ cache: false });
        $('#createTokenLanding').submit(function() {
            var landingCreate = $('#createTokenLanding').serialize();
            $.ajax({
                url:    'action-create.php',
                data:   landingCreate,
                success: function(responseText){
                        $('#result').html(responseText);
                }
            });
            return false;
        });
    });
</script>

I need to URI encode a form input, that is then serialized with a bunch of a hidden inputs and sent to a PHP file.. is it possible to somehow bine encodeURIComponent into this line?:

var landingCreate = $(this).serialize();

UPDATE:

Doing this for example:

var landingCreate = $(this).serialize()+"&enc="+encodeURIComponent($('input[name=\'longform\']').val());

and entering the url:

http://www.smashingmagazine./2008/10/13/pricing-tables-showcase-examples-and-best-practices/

into the text box, returns the URL unchanged.. shouldnt it be converting all the dashes and slashes etc to hex codes?

UPDATE

Here is the full code.

<form id="createTokenLanding">
    <input type="text" name="longform" />
    <input type="hidden" name="domain" value="<?php echo rawurlencode($_SERVER['HTTP_HOST']); ?>" />
    <input type="hidden" name="useragent" value="<?php echo rawurlencode($_SERVER['HTTP_USER_AGENT']); ?>" />
    <input type="hidden" name="ip" value="<?php echo rawurlencode($_SERVER['REMOTE_ADDR']); ?>" />
    <input type="hidden" name="cookieuser" value="<?php echo rawurlencode($_COOKIE['littlr_user']); ?>" />
    <input type="submit" name="submit" value="Shorten" />
</form>

<div id="result">
123
</div>

<script type="text/javascript">
    $(document).ready(function(){
        $.ajaxSetup ({ cache: false });
        $('#createTokenLanding').submit(function() {
            var landingCreate = $('#createTokenLanding').serialize();
            $.ajax({
                url:    'action-create.php',
                data:   landingCreate,
                success: function(responseText){
                        $('#result').html(responseText);
                }
            });
            return false;
        });
    });
</script>
Share Improve this question edited Nov 25, 2010 at 16:45 Gary asked Nov 25, 2010 at 13:49 GaryGary 1,0113 gold badges10 silver badges16 bronze badges 10
  • Does the input you want your pass URI encode is entered by the user or is a hidden input? – andres descalzo Commented Nov 25, 2010 at 14:22
  • "longform" is in the form "this"?. you're using the event submit to send the data for Ajax? – andres descalzo Commented Nov 25, 2010 at 15:06
  • yes, there is an <input type="text" name="longform" /> – Gary Commented Nov 25, 2010 at 15:07
  • It seems to work: "action-create.php?_=1290705763550&longform=http%3A%2F%2Fgoogle.%2F&domain=host&useragent=ua&ip=ip&cookieuser=cu" – thejh Commented Nov 25, 2010 at 17:24
  • action-create.php sends the data straight into a database, and even using serialize, the url does not enter the database encoded.. it still contains slashes, etc.. – Gary Commented Nov 25, 2010 at 17:32
 |  Show 5 more ments

2 Answers 2

Reset to default 4

if you use jQuery.ajax, you can see the documentation for the option "traditional." Also if you use jQuery 1.4 has the "traditional" in "jQuery.param." what they do is use the function "encodeURIComponent" for the key and value:

param = encodeURIComponent (key) + "=" + encodeURIComponent (value);

find traditional setting
jQuery.param

UPDATE

you can see from this example, "serialize" works well over the fields to be sent by post or get. Can you put the ajax code to which you send data? example

Can you try urlencode function and use .replace() for hex codes.

本文标签: javascriptjquery serialize and encodeURIComponentStack Overflow