SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    May 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    can we send javascript variables to as server side variable?

    hi,
    can we send javascript variables to as server side variable
    i have JavaScript variable i want to send as php session value ,
    like(var name="first name";
    i want to send this name to
    $_session['name']=name;
    )
    is it possible ?
    if possible how it is?
    Thank u

  2. #2
    Non-Member
    Join Date
    Jun 2012
    Posts
    88
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    2 options are to use ajax or just append the variable name and value as a query string to the url of the php script that sets the session variable.

    There are other options as well but unless you are more specific in exactly what you want to do not all the options would be suitable.

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You can also send a variable as a hidden field in a form.
    Code:
    // place holder in your html form
    <input type=hidden name=username value="" />
    Code:
    // Shunt a value to the html element using JS
    oFormObject.elements["username"].value = 'Jon Lord';

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,196
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    The only way you can do this is by

    • using a form to $_POST the values to the server
    • using ajax to send the data and store it


    Apart from those 2 basic ways there is no way JavaScript can communicate with PHP variables directly.

  5. #5
    SitePoint Member
    Join Date
    May 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank u for your response... i am using like that only..

  6. #6
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    http://www.sitepoint.com/forums/show...script-Refresh

    changeScript("/index.php?name="+name);
    Last edited by paul_wilkins; Jul 20, 2012 at 05:23.
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  7. #7
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I can't believe my answer was deleted and I was accused of thread highjacking! Below is a totally viable method to send javascript variable to any server-side language without the need to use third party libraries like ajax or jquery!

    http://www.sitepoint.com/forums/show...script-Refresh

    Javascript:
    Code:
    var name="anonymous"
    changeScript("/index.php?name="+name);
    Server coded Javascript: (/index.php)
    PHP Code:
    name=$_GET["name"];
    $_SESSION["name"]=name;

    header("ContentType: application/x-javascript");
    header("Cache-Control: no-cache");
    header("Expires: -1");

    echo 
    "alert(\"".$_SESSION["name"]."\");"
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  8. #8
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Sorry, haven't been here for a long while. Missed a few "$"'s on the PHP part of the post above and the edit button has gone. I guess you can only edit a few times. Make sure you put them in for it to work. ($name not name)
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if


Tags for this Thread

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
  •