CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Passing Variables (JS --> PHP)

  1. #1
    Join Date
    May 2006
    Posts
    306

    Passing Variables (JS --> PHP)

    Is there any way to pass variables from JS to PHP?
    I thought of a couple ways myself...

    1. Create a window with a url like this: http://www.site.com/JS2PHP.php?varlist=Name:JSinputname|Age:50|Exit:Yes, then use PHP to split the "|" then split the ":" and then after 1 second, automatically close the popup since the variables are passed... then what? Can you do this with PHP 5?
    PHP Code:
    <?PHP
        
    // JS2PHP.php
        
    $VarList $_GET["varlist"];
    ?>
    Will that variable be accesible from this?

    PHP Code:
    <?PHP
        
    // EchoVar.php
        
    echo $VarList;
    ?>
    2. using this
    HTML Code:
    <SCRIPT>
        var JSVAR = nameinput.value;
        document.write("<?PHP $JS_Var = \"" + JSVAR + "\"; ?>");
    </SCRIPT>

  2. #2
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,943

    Re: Passing Variables (JS --> PHP)

    There are only two ways to pass a JavaScript variable to PHP. You can implement these with AJAX or by loading a new page.
    1. Using $_GET.
    2. Using $_POST.

    Note: Your second option will not work because the server-side code (php) would already have been executed before the JavaScript (client-side) could write it using document.write. Since PHP is server-side it cannot be written dynamically like JavaScript.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Aug 2002
    Posts
    879

    Re: Passing Variables (JS --> PHP)


  4. #4
    Join Date
    May 2006
    Posts
    306

    Re: Passing Variables (JS --> PHP)

    Ok thanks...but can you access $num1 in script1.php from script2.php?
    Do you have to make it global?

  5. #5
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,943

    Re: Passing Variables (JS --> PHP)

    In order to access PHP variables between PHP scripts you can do a number of things.

    1. Work with sessions and make it a session variable.
    2. Store it in a cookie.
    3. Use include(), include_once(), require(), or require_once().
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)