www.webdeveloper.com
Results 1 to 10 of 10

Thread: Passing Data from Javascript to PHP

  1. #1
    Join Date
    Jun 2004
    Posts
    20

    Passing Data from Javascript to PHP

    I've been working hard for quite sometime to pass the value from a javascript text box to a php page. I've tried with all he conventional method but was unable to parse the data. I've attached the code with this post. Plesae see what went wrong with this.
    Attached Files Attached Files

  2. #2
    Join Date
    Apr 2005
    Posts
    16
    I think the following code might work:

    PHP Code:
    <?php
    //if the variable is "variable"
    $value "<script language='javascript'>document.write(variable);</script>";
    ?>
    But remember, php is serverside while javascript is clientside. I think the most conventional method would be to do something along the lines of the following:

    PHP Code:
    <?php
    if (!$xyz)
    {
    ?>
    <script language="javascript">
    //lets say the variable is "xyz" and the value is "15"
    //you already have "xyz" and you want to pass it to php, so it would be "$xyz"
    location.href="<?php echo $PHP_SELF?>?xyz=15";
    </script>
    <?php
    }
    ?>

    <?php
    //if you are using a newer version of php (i use php 5), you don't need to do anything but use $xyz. Otherwise, you would need to register it.
    //use $xyz as needed.
    ?>
    I hope this helps a bit.

  3. #3
    Join Date
    Jun 2004
    Posts
    20
    Quote Originally Posted by gimp
    I think the following code might work:

    [PHP]
    <?php
    //if the variable is "variable"
    $value = "<script language='javascript'>document.write(variable);</script>";
    ?>
    Thanks for your reply. Can you plz have a look on my attached file and suggest me how i can send the value of 'abc' box to the following page.

  4. #4
    Join Date
    Apr 2005
    Posts
    42
    Quote Originally Posted by gimp
    I think the following code might work:
    Hi-

    Unfortunately, the first suggestion doesn't work. The second would require a refresh. I am also investigating the passing of a variable from Javascript to PHP. If I come up with something I'll post it. It hasn't been a priority yet.
    Last edited by ATBS; 05-11-2005 at 02:22 PM. Reason: wrong item quoted

  5. #5
    Join Date
    Apr 2005
    Posts
    42
    This is from php.net

    5. How can I pass a variable from Javascript to PHP?

    Since Javascript is (usually) a client-side technology, and PHP is (usually) a server-side technology, and since HTTP is a "stateless" protocol, the two languages cannot directly share variables.

    It is, however, possible to pass variables between the two. One way of accomplishing this is to generate Javascript code with PHP, and have the browser refresh itself, passing specific variables back to the PHP script. The example below shows precisely how to do this -- it allows PHP code to capture screen height and width, something that is normally only possible on the client side.

    PHP Code:
    <?php
    if (isset($_GET['width']) AND isset($_GET['height'])) {
      
    // output the geometry variables
      
    echo "Screen width is: "$_GET['width'] ."<br />\n";
      echo 
    "Screen height is: "$_GET['height'] ."<br />\n";
    } else {
      
    // pass the geometry variables
      // (preserve the original query string
      //  -- post variables will need to handled differently)

      
    echo "<script language='javascript'>\n";
      echo 
    "  location.href=\"${_SERVER['SCRIPT_NAME']}?${_SERVER['QUERY_STRING']}"
               
    "&width=\" + screen.width + \"&height=\" + screen.height;\n";
      echo 
    "</script>\n";
      exit();
    }
    ?>

  6. #6
    Join Date
    Jun 2004
    Posts
    20
    thanks for your reply. But can anyone have a look in my attached file? Its a bit different and i need help in that specific section.

  7. #7
    Join Date
    Jun 2004
    Posts
    20
    I am waitting for somenoe's reply.

  8. #8
    Join Date
    Jun 2011
    Posts
    3

    Working on a similar issue, I think.

    This is what I have come up with so fare.
    Hope it helps. (I'll check back to see how it worked for you.)

    function toPHPArray(obj)
    {
    var string = '';
    alert (obj);

    if (typeof(obj) == 'object')
    {
    alert ('object');
    if (obj instanceof Array)
    {
    string = 'a:';
    tmpstring = '';
    count = 0;

    for (var key in obj)
    {
    tmpstring += toPHPArray(key);
    tmpstring += toPHPArray(obj[key]);
    count++;
    }

    string += count + ':{';
    string += tmpstring;
    string += '}';
    }
    else if (obj instanceof Object)
    {
    classname = obj.toString();

    if (classname == '[object Object]')
    {
    classname = 'StdClass';
    }

    string = 'O:' + classname.length + ':"' + classname + '":';
    tmpstring = '';
    count = 0;

    for (var key in obj)
    {
    tmpstring += toPHPArray(key);

    if (obj[key])
    {
    tmpstring += toPHPArray(obj[key]);
    }
    else
    {
    tmpstring += toPHPArray('');
    }

    count++;
    }

    string += count + ':{' + tmpstring + '}';
    }
    }
    else
    {
    switch (typeof(obj))
    {
    case 'number':
    if (obj - Math.floor(obj) != 0)
    {
    string += 'd:' + obj + ';';
    }
    else
    {
    string += 'i:' + obj + ';';
    }

    break;
    case 'string':
    string += 's:' + obj.length + ':"' + obj + '";';
    break;
    case 'boolean':
    if (obj)
    {
    string += 'b:1;';
    }
    else
    {
    string += 'b:0;';
    }

    break;
    }
    }

    return string;
    }

  9. #9
    Join Date
    Jun 2011
    Posts
    3
    After you should be able to php unserialize the data you are passing.
    I have almost completed implimenting it in my code. (so unless I didn't catch what you where asking...)

  10. #10
    Join Date
    Jun 2011
    Posts
    3
    The "alert ();" s aren't needed (just for my debugging)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles