error while call php function in javascript? [message #173877] |
Wed, 11 May 2011 12:10 |
Amit Prakash Pawar
Messages: 10 Registered: October 2010
Karma: 0
|
Junior Member |
|
|
function encrypt_url($string)
{
$key = "1AT2#mr(luv^iU3tp>";
$result = '';
for($i=0; $i<strlen($string); $i++)
{
$char = substr($string, $i, 1);
$keychar = substr($key, ($i % strlen($key))-1, 1);
$char = chr(ord($char)+ord($keychar));
$result.=$char;
}
return urlencode(base64_encode($result));
}
$data=encrypt_url('http://www.test.com');
Normally through PHP code i got encrypted value ->
pqW1xGxSnOmf46Pqw9zJYdffqw%3D%3D
When i try through javascript getting some wrong output
// Javascript
var my_url=document.getElementById('url').value;
// my_url='http://www.test.com';
var final_post_url=<?=encrypt_url(my_url)?>;
alert(final_post_url);
how to get correct encrypted data through java script?
|
|
|
Re: error while call php function in javascript? [message #173880 is a reply to message #173877] |
Wed, 11 May 2011 13:30 |
Tim Streater
Messages: 328 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
In article
<3ece3c5d-94b1-4fea-9af7-08657b1eaae5(at)d19g2000prh(dot)googlegroups(dot)com>,
Amit Prakash Pawar <amitppawar2007(at)gmail(dot)com> wrote:
> function encrypt_url($string)[/color]
[color=blue]> {[/color]
[color=blue]> $key = "1AT2#mr(luv^iU3tp>";[/color]
[color=blue]> $result = '';[/color]
[color=blue]> for($i=0; $i<strlen($string); $i++)[/color]
[color=blue]> {[/color]
[color=blue]> $char = substr($string, $i, 1);[/color]
[color=blue]> $keychar = substr($key, ($i % strlen($key))-1, 1);[/color]
[color=blue]> $char = chr(ord($char)+ord($keychar));[/color]
[color=blue]> $result.=$char;[/color]
[color=blue]> }[/color]
[color=blue]> return urlencode(base64_encode($result));[/color]
[color=blue]> } [/color]
>
> $data=encrypt_url('http://www.test.com');
> Normally through PHP code i got encrypted value ->
> pqW1xGxSnOmf46Pqw9zJYdffqw%3D%3D
>
> When i try through javascript getting some wrong output
>
> // Javascript
> [/color]
[color=blue]> var my_url=document.getElementById('url').value;[/color]
[color=blue]> [/color]
[color=blue]> // my_url='http://www.test.com';[/color]
[color=blue]> [/color]
[color=blue]> var final_post_url=<?=encrypt_url(my_url)?>;[/color]
[color=blue]> alert(final_post_url);[/color]
[color=blue]> [/color]
> how to get correct encrypted data through java script?
This will never work. By the time your page is loaded, there is no
longer any PHP in it. You cannot call PHP functions from JavaScript. Any
PHP in your page is run *before* the page reaches the browser.
--
Tim
"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
|
|
|
Re: error while call php function in javascript? [message #173882 is a reply to message #173877] |
Wed, 11 May 2011 13:38 |
Gregor Kofler
Messages: 69 Registered: September 2010
Karma: 0
|
Member |
|
|
Am 2011-05-11 14:10, Amit Prakash Pawar meinte:
> function encrypt_url($string)[/color]
[color=blue]> {[/color]
[color=blue]> $key = "1AT2#mr(luv^iU3tp>";[/color]
[color=blue]> $result = '';[/color]
[color=blue]> for($i=0; $i<strlen($string); $i++)[/color]
[color=blue]> {[/color]
[color=blue]> $char = substr($string, $i, 1);[/color]
[color=blue]> $keychar = substr($key, ($i % strlen($key))-1, 1);[/color]
[color=blue]> $char = chr(ord($char)+ord($keychar));[/color]
[color=blue]> $result.=$char;[/color]
[color=blue]> }[/color]
[color=blue]> return urlencode(base64_encode($result));[/color]
[color=blue]> } [/color]
>
> $data=encrypt_url('http://www.test.com');
> Normally through PHP code i got encrypted value ->
> pqW1xGxSnOmf46Pqw9zJYdffqw%3D%3D
>
> When i try through javascript getting some wrong output
>
Depends what you mean by "some wrong output". I suppose this "wrong
output" should tell you all you need. (Error reporting is activated, I
assume...)
> // Javascript
> [/color]
[color=blue]> var my_url=document.getElementById('url').value;[/color]
[color=blue]> [/color]
[color=blue]> // my_url='http://www.test.com';[/color]
[color=blue]> [/color]
[color=blue]> var final_post_url=<?=encrypt_url(my_url)?>;[/color]
How do is PHP supposed to know about a JS variable? In this case the
server-side PHP assumes that my_url is a constant, which it won't find
(and issue a warning).
You'd have to send your JS value to the server, have it processed there
and work with the response. Though it escapes me, why you'd like to
encode a URI, which is already on the page - plain and un-encoded.
[color=blue]> alert(final_post_url);[/color]
[color=blue]> [/color]
> how to get correct encrypted data through java script?
It might be useful to know the differnce between server-side and
client-side scripting.
Gregor
--
http://vxweb.net
|
|
|
Re: error while call php function in javascript? [message #173883 is a reply to message #173877] |
Wed, 11 May 2011 13:51 |
Jeff North
Messages: 58 Registered: November 2010
Karma: 0
|
Member |
|
|
On Wed, 11 May 2011 05:10:57 -0700 (PDT), in comp.lang.php Amit
Prakash Pawar <amitppawar2007(at)gmail(dot)com>
<3ece3c5d-94b1-4fea-9af7-08657b1eaae5(at)d19g2000prh(dot)googlegroups(dot)com>
wrote:
> | function encrypt_url($string)[/color]
[color=blue]> | {[/color]
[color=blue]> | $key = "1AT2#mr(luv^iU3tp>";[/color]
[color=blue]> | $result = '';[/color]
[color=blue]> | for($i=0; $i<strlen($string); $i++)[/color]
[color=blue]> | {[/color]
[color=blue]> | $char = substr($string, $i, 1);[/color]
[color=blue]> | $keychar = substr($key, ($i % strlen($key))-1, 1);[/color]
[color=blue]> | $char = chr(ord($char)+ord($keychar));[/color]
[color=blue]> | $result.=$char;[/color]
[color=blue]> | }[/color]
[color=blue]> | return urlencode(base64_encode($result));[/color]
[color=blue]> | } [/color]
> |
> | $data=encrypt_url('http://www.test.com');
> | Normally through PHP code i got encrypted value ->
> | pqW1xGxSnOmf46Pqw9zJYdffqw%3D%3D
> |
> | When i try through javascript getting some wrong output
> |
> | // Javascript
> | [/color]
[color=blue]> | var my_url=document.getElementById('url').value;[/color]
[color=blue]> | [/color]
[color=blue]> | // my_url='http://www.test.com';[/color]
[color=blue]> | [/color]
[color=blue]> | var final_post_url=<?=encrypt_url(my_url)?>;[/color]
[color=blue]> | alert(final_post_url);[/color]
[color=blue]> | [/color]
> | how to get correct encrypted data through java script?
try (note the quotes placement:
var final_post_url="<?=encrypt_url(my_url)?>";
|
|
|
Re: error while call php function in javascript? [message #173886 is a reply to message #173883] |
Wed, 11 May 2011 14:31 |
Thomas 'PointedEars'
Messages: 701 Registered: October 2010
Karma: 0
|
Senior Member |
|
|
Jeff North wrote:
> Amit Prakash Pawar wrote:
>> | // Javascript
>> | [/color]
[color=teal]>> | var my_url=document.getElementById('url').value;[/color]
[color=teal]>> | [/color]
[color=teal]>> | // my_url='http://www.test.com';[/color]
[color=teal]>> | [/color]
[color=teal]>> | var final_post_url=<?=encrypt_url(my_url)?>;[/color]
[color=teal]>> | alert(final_post_url);[/color]
[color=teal]>> | [/color]
>> | how to get correct encrypted data through java script?
>
> try (note the quotes placement:
> var final_post_url="<?=encrypt_url(my_url)?>";
But `my_url' still is the identifier of a *client-side* variable, which
exists only *after* PHP has parsed the code, substituted the `<?php … ?>'
sections, and returned the result to the Web server for it to include
it in its HTTP response.
Please do not post attribution novels.
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
|
|
|