Results 1 to 4 of 4

Thread: Getting js value using php

  1. #1
    Join Date
    Oct 2009
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Getting js value using php

    I came from the js forum. I have this function:

    Code:
    <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:

    Code:
    <?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

    Heres the site, with the js function live:

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

    Appreciate all replies.
    Last edited by newbtophp; 10-29-2009 at 02:23 AM.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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:
    Code:
    ');
            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/s...ad.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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Oct 2009
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    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.

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •