How do you call PHP from javascript?

Discussion in 'PHP' started by Imozeb, Feb 23, 2010.

  1. #1
    My form goes from 'html form' to 'javascript validation' which 'submits the form' which goes to 'PHP validation' which 'outputs the new data'. I want to use sessions to store the variable so PHP can get it when the page refreshes but I get an error when I place this code in my javascript just before the submit function.


    <?PHP include("session.php"); ?>


    //this is session.php
    <?PHP
    echo session_start();
    echo $_SESSION['varusername'] = $lusername;
    echo $_SESSION['varmemberalready'] = 1;
    ?>

    Why won't it work and how can I make it work? I was thinking about using $_post with hidden forms, would that work?

    Thanks.

    ~imozeb :)
     
    Imozeb, Feb 23, 2010 IP
  2. darkblade

    darkblade Peon

    Messages:
    187
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    But you can do it the other way.

    Example:
    <?PHP
    echo session_start();
    ?>
    
    <script type="text/javascript">
    var lusername = <? echo $_SESSION['varusername'] ?>
    </script>
    Code (markup):
    This will assign the varusernameto the lusername. Hope this helps.
     
    darkblade, Feb 23, 2010 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I want to assign varusername to lusername not the other way around. Is there anyway I can do this? Do you think hidden forms will work so that the javascript changes the value of the hidden form, then the form is submited, and PHP gets the value via $_POST ?
     
    Imozeb, Feb 23, 2010 IP
  4. suresh981

    suresh981 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    getElementById in javascript.

    var variablename=document.getElementById("textboxidname").value
     
    suresh981, Feb 25, 2010 IP
  5. viron86

    viron86 Active Member

    Messages:
    426
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #5
    using ajax
     
    viron86, Feb 25, 2010 IP
  6. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #6
    use hidden forms... or use ajax.
     
    bartolay13, Feb 25, 2010 IP