Jump to content

help solving this simple javascript problem


robert_gsfame

Recommended Posts

i got simple problem with javascript as i am a newbie, so i need some assist from you

 

<script type="text/javascript" language="javascript">

  function _preview(){

    param = document.getElementById("name").value

param2 = document.getElementById("address").value

 

    window.open("mydata.php?name="+param&&address=param2);

  }

</script>

 

 

confused, can anyone help me.. how to pass those value param and param2

Link to comment
Share on other sites

i got simple problem with javascript as i am a newbie, so i need some assist from you

 

<script type="text/javascript" language="javascript">

  function _preview(){

    param = document.getElementById("name").value

param2 = document.getElementById("address").value

 

    window.open("mydata.php?name="+param&&address=param2);

  }

</script>

 

 

confused, can anyone help me.. how to pass those value param and param2

 

It looks like your last line should be:

 

window.open("mydata.php?name=" + param + "&address=" + param2);

 

You may need to encode those values so they can be passed through the URL.  In mydata.php, you'd obtain those values by:

 

$name = $_GET['name'];
$address = $_GET['address'];

 

And, similarly, you may need to decode them here.

Link to comment
Share on other sites

i already did what has been written above

 

param = document.getElementById("Name").value

param2 = document.getElementById("address").value

    window.open("data.php?Name=" + param + "&address=" + param2);

  }

 

does it mean if i want to have another value such as telephone, i just add the parameter so it will looked like this

 

param = document.getElementById("Name").value

param2 = document.getElementById("address").value

param2 = document.getElementById("telephone").value

    window.open("data.php?Name=" + param + "&address=" + param2  + "&telephone=" + param3);  }

 

i've tried but it didn't work.....then how can i get value of the address with the same format like what written in the textbox.. in php i always use

 

nl2br($_GET['address']) but here didn't work

 

can help me solve this problem?

Link to comment
Share on other sites

Hmm...more research has shown that JavaScript's encoding functions aren't compatible with PHP's encoding functions.  Thankfully, I've found a 3rd party function that works.

 

Here's the function:

 

function urlencode( str ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: travc
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Lars Fischer
    // +      input by: Ratheous
    // %          note 1: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
                             
    var hash_map = {}, unicodeStr='', hexEscStr='';
    var ret = (str+'').toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The hash_map is identical to the one in urldecode.
    hash_map["'"]   = '%27';
    hash_map['(']   = '%28';
    hash_map[')']   = '%29';
    hash_map['*']   = '%2A';
    hash_map['~']   = '%7E';
    hash_map['!']   = '%21';
    hash_map['%20'] = '+';
    hash_map['\u00DC'] = '%DC';
    hash_map['\u00FC'] = '%FC';
    hash_map['\u00C4'] = '%D4';
    hash_map['\u00E4'] = '%E4';
    hash_map['\u00D6'] = '%D6';
    hash_map['\u00F6'] = '%F6';
    hash_map['\u00DF'] = '%DF';
    hash_map['\u20AC'] = '%80';
    hash_map['\u0081'] = '%81';
    hash_map['\u201A'] = '%82';
    hash_map['\u0192'] = '%83';
    hash_map['\u201E'] = '%84';
    hash_map['\u2026'] = '%85';
    hash_map['\u2020'] = '%86';
    hash_map['\u2021'] = '%87';
    hash_map['\u02C6'] = '%88';
    hash_map['\u2030'] = '%89';
    hash_map['\u0160'] = '%8A';
    hash_map['\u2039'] = '%8B';
    hash_map['\u0152'] = '%8C';
    hash_map['\u008D'] = '%8D';
    hash_map['\u017D'] = '%8E';
    hash_map['\u008F'] = '%8F';
    hash_map['\u0090'] = '%90';
    hash_map['\u2018'] = '%91';
    hash_map['\u2019'] = '%92';
    hash_map['\u201C'] = '%93';
    hash_map['\u201D'] = '%94';
    hash_map['\u2022'] = '%95';
    hash_map['\u2013'] = '%96';
    hash_map['\u2014'] = '%97';
    hash_map['\u02DC'] = '%98';
    hash_map['\u2122'] = '%99';
    hash_map['\u0161'] = '%9A';
    hash_map['\u203A'] = '%9B';
    hash_map['\u0153'] = '%9C';
    hash_map['\u009D'] = '%9D';
    hash_map['\u017E'] = '%9E';
    hash_map['\u0178'] = '%9F';
    
    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);

    for (unicodeStr in hash_map) {
        hexEscStr = hash_map[unicodeStr];
        ret = replacer(unicodeStr, hexEscStr, ret); // Custom replace. No regexing
    }
    
    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
}

 

Once you have the definition in place, run all of your values through it.  So:

 

param = urlencode(document.getElementById('Name').value);

 

And so on.  Then, in your PHP, use:

 

$name = urldecode($_GET['Name']);
$address = nl2br(urldecode($_GET['address']);

 

Etc.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.