Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #1515 > unrolled thread

error while call php function in javascript?

Started byAmit Prakash Pawar <amitppawar2007@gmail.com>
First post2011-05-11 05:10 -0700
Last post2011-05-11 16:31 +0200
Articles 5 — 5 participants

Back to article view | Back to comp.lang.php


Contents

  error while call php function in javascript? Amit Prakash Pawar <amitppawar2007@gmail.com> - 2011-05-11 05:10 -0700
    Re: error while call php function in javascript? Tim Streater <timstreater@waitrose.com> - 2011-05-11 14:30 +0100
    Re: error while call php function in javascript? Gregor Kofler <usenet@gregorkofler.com> - 2011-05-11 15:38 +0200
    Re: error while call php function in javascript? Jeff North <jnorthau@yahoo.com.au> - 2011-05-11 23:51 +1000
      Re: error while call php function in javascript? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-05-11 16:31 +0200

#1515 — error while call php function in javascript?

FromAmit Prakash Pawar <amitppawar2007@gmail.com>
Date2011-05-11 05:10 -0700
Subjecterror while call php function in javascript?
Message-ID<3ece3c5d-94b1-4fea-9af7-08657b1eaae5@d19g2000prh.googlegroups.com>
[code]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));
}[/code]

$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
[code]
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);
[/code]
how to get correct encrypted data through java script?

[toc] | [next] | [standalone]


#1517

FromTim Streater <timstreater@waitrose.com>
Date2011-05-11 14:30 +0100
Message-ID<timstreater-B8BF0A.14305911052011@news.individual.net>
In reply to#1515
In article 
<3ece3c5d-94b1-4fea-9af7-08657b1eaae5@d19g2000prh.googlegroups.com>,
 Amit Prakash Pawar <amitppawar2007@gmail.com> wrote:

> [code]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));
> }[/code]
> 
> $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
> [code]
> 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);
> [/code]
> 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

[toc] | [prev] | [next] | [standalone]


#1519

FromGregor Kofler <usenet@gregorkofler.com>
Date2011-05-11 15:38 +0200
Message-ID<iqe3fv$d7i$1@dont-email.me>
In reply to#1515
Am 2011-05-11 14:10, Amit Prakash Pawar meinte:
> [code]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));
> }[/code]
> 
> $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
> [code]
> var my_url=document.getElementById('url').value;
> 
> // my_url='http://www.test.com';
> 
> var final_post_url=<?=encrypt_url(my_url)?>;

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.

> alert(final_post_url);
> [/code]
> 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

[toc] | [prev] | [next] | [standalone]


#1520

FromJeff North <jnorthau@yahoo.com.au>
Date2011-05-11 23:51 +1000
Message-ID<215ls6ppav5asq72thq1qobc6k4bmiea2l@4ax.com>
In reply to#1515
On Wed, 11 May 2011 05:10:57 -0700 (PDT), in comp.lang.php Amit
Prakash Pawar <amitppawar2007@gmail.com>
<3ece3c5d-94b1-4fea-9af7-08657b1eaae5@d19g2000prh.googlegroups.com>
wrote:

>| [code]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));
>| }[/code]
>| 
>| $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
>| [code]
>| 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);
>| [/code]
>| how to get correct encrypted data through java script?

try (note the quotes placement:
var final_post_url="<?=encrypt_url(my_url)?>";

[toc] | [prev] | [next] | [standalone]


#1523

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2011-05-11 16:31 +0200
Message-ID<4283243.ZASKD2KPVS@PointedEars.de>
In reply to#1520
Jeff North wrote:

> Amit Prakash Pawar wrote:
>>| // Javascript
>>| [code]
>>| 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);
>>| [/code]
>>| 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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web