PDA

View Full Version : Getting js value using php



newbtophp
10-29-2009, 02:18 AM
I came from the js forum. I have this function:


<script>
hp_ok=true;

function hp_d01(s){
if(!hp_ok)return;var o="",ar=new Array(),os="",ic=0;

for(i=0;i<s.length;i++){
c=s.charCodeAt(i);

if(c<128)c=c^2;

os+=String.fromCharCode(c);

if(os.length>80){
ar[ic++]=os;os=""
}
}

o=ar.join("")+os;
document.getElementById('outputarea').value += '\n' + o;

}
document.write('<textarea id="outputarea" style="width:100%; height:100px;">');
document.write('</textarea>');
document.getElementById('outputarea').value = '';
</script>
(To all who are confused on what the function does, its a javascript decrypting function, which you place encoded code below it and then it displays a textarea contain the decrypted code).

and I'm trying to get the value of the textarea within a php variable.

I've tried:


<?php

$content = file_get_contents('index.php');

$temp = explode('<textarea id="outputarea" style="width:100%; height:100px;">',$content);

$temp2 = explode('</textarea>',$temp[1]);

$code = $temp2[0];

unset($content,$temp,$temp2);

//$code is the variable containing the code within the textarea?
print_r($code);


?>

But it don't do anything :confused:

Heres the site, with the js function live:

http://newbtophp.no-ip.biz/index.php

Appreciate all replies.

djr33
10-29-2009, 02:45 AM
That is completely the wrong approach, on a few levels.

Assuming that script works (I didn't test it to see if it does), you could get somethign like this:

');
document.write('
--because that is what is between the <textarea> tags in the SOURCE CODE of your page.


To solve this problem:
1. Read this:
http://www.dynamicdrive.com/forums/showthread.php?t=25847
Specifically, about what PHP is, and what Javascript is.

2. Javascript works IN THE BROWSER, and PHP works ON THE SERVER. They are not, and never will be connected. In order to transfer information between the two you can:
PHP>JS: use PHP to generate the source code of a page including Javascript, such as using PHP to write "var x = 'value';".
JS>PHP: PHP runs once, when the page is initially loaded and not again, so you can send values from JS to PHP via a form, or via Ajax (a behind the scenes "page load"), but not live or actually connected.

3. For this problem specifically, it's probably best to use a form and submit that to PHP, or to rewrite the code into PHP itself (using Javascript seems silly if you are able to use PHP instead). You could also use Ajax if it is required that the user not have to have the page reload.
Javascript is GENERATING something, INCLUDING the textarea itself, and only after the page has loaded. Because of that you must send a NEW request to the server for PHP to get this data. Once the page loads, PHP no longer has anything to do with the page; it cannot interact with Javascript like that.

newbtophp
10-29-2009, 02:51 AM
Ok thanks for clearing things up, i've never faced this challenge before, i've always stuck to coding php and whenever i'd needed javascript i'd google and search for snippets.

Can you tell me how and where to define the var on the js function, so i know where to add the php echo too.

djr33
10-29-2009, 06:18 AM
Unfortunately, that was just an example.

I'm not really sure what you are trying to do here, but the main problem is that you need to figure out how you want it to work, then how you can get it to actually do that.

You can echo with PHP to give information to Javascript.
You can use Javascript and forms or ajax to submit info back to PHP.

Which one is giving information to the other? What do you want done with it? Etc.

Then the problem is just figuring out where to put the code.