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 14 of 14
  1. #1
    New Coder
    Join Date
    May 2007
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Passing jscript function variable to php

    Hello all!

    Little problem here!

    I am trying to pass a jscript function variable to php, here's an example.

    Code:
    function changeContent(url){
    
    document.getElementById("contentDiv").innerHTML = "<?
    $tmpVar = ?>" + url + "<?
    echo $tmpVar;
    ?>";
    
    }
    This gives me an error "Parse error: parse error, unexpected ';' in /home/azranet/public_html/test.php on line 10"

    On the other hand, this works

    Code:
    function changeContent(url){
    
    document.getElementById("contentDiv").innerHTML = "<?
    $tmpVar = "some text";
    echo $tmpVar;
    ?>";
    
    }
    Anyone has an idea?

    GCharb
    If you have nothing good to say about someone, say nothing at all!

  • #2
    New Coder
    Join Date
    Jun 2007
    Location
    VA
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by GCharb View Post
    Hello all!

    Little problem here!

    I am trying to pass a jscript function variable to php, here's an example.

    Code:
    function changeContent(url){
    
    document.getElementById("contentDiv").innerHTML = "<?
    $tmpVar = ?>" + url + "<?
    echo $tmpVar;
    ?>";
    
    }
    This gives me an error "Parse error: parse error, unexpected ';' in /home/azranet/public_html/test.php on line 10"
    Not sure if it will work but try this:

    Code:
    function changeContent(url){
    
    document.getElementById("contentDiv").innerHTML = "<?
    $tmpVar = ?>" + url + "<?;
    echo $tmpVar;
    ?>";
    
    }
    Dave

  • #3
    New Coder
    Join Date
    May 2007
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello there!

    Thanks for the quick reply!

    Tried and failed, same error message.

    GCharb
    If you have nothing good to say about someone, say nothing at all!

  • #4
    New Coder
    Join Date
    Jun 2007
    Location
    VA
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    why are you echoing the $tmpVar afterwards?

  • #5
    New Coder
    Join Date
    May 2007
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts
    just a bad construct, I could write echo ?>" + url + "<?;

    GCharb
    If you have nothing good to say about someone, say nothing at all!

  • #6
    New Coder
    Join Date
    Jun 2007
    Location
    VA
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, last effort, Try this:

    Code:
    "<?
    $tmpVar = "?>" + url + "<?";

  • #7
    New Coder
    Join Date
    May 2007
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello again!

    This time no errors but a value of zero(0) is assigned to the php variable, ill figure it out, thanks for your time!
    If you have nothing good to say about someone, say nothing at all!

  • #8
    New Coder
    Join Date
    Jun 2007
    Location
    VA
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm still kinda confused as to what you're trying to do. From my understanding (keep in mind I've only been writing code for about 6 months now) Javascript is a Client Side scripting languange, whereas, PHP is server side. So, I don't really think you can assign PHP variables in a Javascript funtion. can you paste all of your php code up so I can take a look?

  • #9
    New Coder
    Join Date
    May 2007
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You are right about jscript being client side and php being server side, but, it is easy to assing php variable to jscript like so...

    var myJsVar <?= $myPhpVar; ?>

    Has to be a way to do the opposite and what I am tying to do here is to update the content of a html Div with php generated through jscript!

    Hope it sheds light on my intentions!

    GCharb
    If you have nothing good to say about someone, say nothing at all!

  • #10
    New Coder
    Join Date
    Jun 2007
    Location
    VA
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Since your browser doesn't actually parse php, the variable has to be set on the serverside for PHP, so if you're trying to assign a jscript variable to a php variable that hasn't been set (since the browser hasn't set it yet) you would get a value of 0. Sounds like you're going to need some other technology. AJAX might be the way to go.

  • #11
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Good luck with your try, hope u make it, but i think that is impossible.

    It's the same as this code
    PHP Code:
    <?php

    $variable 
    ?>

    <script type="text/javascript">
    document.write("Hello World!") <?php ?>
    </script> <?php

    echo $variable;

    ?>
    And that's just wrong, couse PHP server side parser, parses through that file before it reaches the user. Maybe you should use Ajax for that, although interesting idea. If you make it, post it back here, you'll be sort of a hero

    EDIT: ddanatzko, beat me to it

  • #12
    New Coder
    Join Date
    Jun 2007
    Location
    VA
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by matak View Post
    EDIT: ddanatzko, beat me to it
    At least now I feel like I know what I'm talking about!!!

  • #13
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by ddanatzko View Post
    At least now I feel like I know what I'm talking about!!!
    Don't let me be the measure of -- that you know what ur talking about :lol:

    (that fraze looses some meaning due to translation )

  • #14
    New Coder
    Join Date
    May 2007
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello there!

    Always feels good to have the impression we know what we are talking about, happens even to me from time to time!

    Solved the problem with ajax, loving it too

    The httprequest aproach allows me to pass any jscript variable to php I need.

    Thanks both for your time!

    GCharb
    If you have nothing good to say about someone, say nothing at all!


  •  

    Posting Permissions

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