Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    May 2012
    Posts
    61
    Thanks
    1
    Thanked 0 Times in 0 Posts

    pass value from js variable to php variable

    hi everyone i have 2 file one is test.js and second is test.php
    in test.js file i have this function
    Code:
    function randString(n)
    {
        if(!n)
        {
            n = 35;
        }
    
        var text = '';
        var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    
        for(var i=0; i < n; i++)
        {
            text += possible.charAt(Math.floor(Math.random() * possible.length));
        }
    
        return text;
    }
    var randomvalue = randString(34);
    this function generate 34 random values and save to var randomvalue in test.js file its fine

    now what i want
    i have $value variable in test.php file
    i want when var randomvalue in test.js get value
    this same value also pass the $value variable in test.php
    and $value variable in test.php show also same value at a time

    how to push value var randomvalue in test.js to in $value variable in test.php?

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,297
    Thanks
    23
    Thanked 612 Times in 611 Posts
    The only way(s) you can have communication between A program on the server(PHP) and one in side the browser(javascript) is A.) submitting a form. B.) using session variables, but this is not strictly javascript. And C.) ajax.
    Here is ajax with a return that is used to show the variable was sent from JS to PHP and PHP sent it back.

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>Title of the document</title>
    </head>
    
    <body style="background-color: # AA5303;"><!--  #80ff00;">-->
    
    
    <div id="here">Information</div>
    
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
    <script type="text/javascript">
    function randString(n){
        if(!n){
            n = 35;
        }
        var text = '';
        var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
        for(var i=0; i < n; i++){
            text += possible.charAt(Math.floor(Math.random() * possible.length));
        }
        $.ajax({
    		type: "POST",
    		url: "ajax.php",
    		data: { name: text}
    		})
    		.done(function( msg ) {
    		document.getElementById("here").innerHTML = "Data Saved: " + msg ;
    	});
    }
    var randomvalue = randString(34);
    </script>
    </body>
    </html>
    This is ajax.php
    PHP Code:
    <?php
    $cosmo 
    $_POST["name"];
    if (
    $_POST['name'])
        echo 
    $cosmo;
    ?>
    It just might be better to make the variable up in the PHP and forget JS.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,026
    Thanks
    15
    Thanked 240 Times in 240 Posts
    You can also put the variable in the URL as a 'get' value and fetch it in the php using $_GET, or you can put it in a cookie.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #4
    New Coder
    Join Date
    May 2012
    Posts
    61
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanks for reply
    actually i use this function in jspopunder
    jspopunder('http://localhost/test.php?randomvalue='+randomvalue+'')
    this link in test.js
    its open link with also random number
    and we easly $_GET from this link in php file
    but we want also in test.js random pass to direct in php file

    after we match the random number from $_GET from url and get from js file value
    then show our page

    you asked why this method
    because i want secuitry on our link i want no person see detail when its run direct link its
    open in url but its detail show when he get from popup

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Why not do this onclick of the link?

    1. Randomize a number.
    2. Save it in a cookie.
    3. Open popup passing the generated random number in the querystring.
    4. In PHP, read the cookie and compare against the the random number in querystring.
    5. If they are the same, then display the content, otherwise don't.
    6. Always delete the cookie on PHP side regardless if content is displayed or not.

    This means that even if user clicks on the link and the content is displayed, if he refreshes the popup, it will not display the content. The user will be forced to always click the link to see the content.

    Note that this mechanism is not fool-proof. Any knowledgeable user might be able to figure out what to do.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #6
    New Coder
    Join Date
    May 2012
    Posts
    61
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by glenngv View Post
    Why not do this onclick of the link?

    1. Randomize a number.
    2. Save it in a cookie.
    3. Open popup passing the generated random number in the querystring.
    4. In PHP, read the cookie and compare against the the random number in querystring.
    5. If they are the same, then display the content, otherwise don't.
    6. Always delete the cookie on PHP side regardless if content is displayed or not.

    This means that even if user clicks on the link and the content is displayed, if he refreshes the popup, it will not display the content. The user will be forced to always click the link to see the content.

    Note that this mechanism is not fool-proof. Any knowledgeable user might be able to figure out what to do.
    thanks sir i am really want this . can you tell me how to save this random value in cookie and how to after match its delete its cookie value any tutorial

    Thanks again

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    There are tons of cookie scripts on the web. Here's one from a sticky post here in CF:

    http://www.codingforums.com/showthre...730#post172730

    For using cookies in PHP:

    http://php.about.com/od/advancedphp/qt/php_cookie.htm
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

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