#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Posts
    3
    Rep Power
    0

    JavaScript calling a PHP variable


    I wonder if it is possible for JavaScript to call a PHP variable.

    for example on what I am working on:

    <?
    function test()
    {

    ?>
    <SCRIPT LANGUAGE="JavaScript">
    function abc()
    {
    var RadioValue;
    if(document.test.number.value == 10)
    time=15;
    else if(document.test.number.value == 20)
    time=30;
    else(document.test.number.value == 30)
    time=45;
    }
    url = "http://www.certifyexpress.com/exam/oracle/indexR.php?time="+$time+"&action=quiz&number="+document.test.number.value+"&quiz="+RadioValue+"&name ="+test.name.value+"&email="+test.email.value;

    </script>

    <?
    function timing()
    {
    global $time;
    global $hhh;
    ?>
    <SCRIPT LANGUAGE="JavaScript">
    <!--

    if ($time = document.test.number.value){
    if(document.test.number.value == 10)
    $time = 15;

    else if(document.test.number.value == 20)
    $time = 30;

    else
    $time = 45;

    }
    ?>

    thanks.. new to Jscript...
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2001
    Location
    New Zealand
    Posts
    1,774
    Rep Power
    25
    You can't call a PHP variable from javascript client-side, I don't know if you can server-side either. Essentially by the time the javascript kicks in the variable has already been finished with by the PHP. What you can do is something like.

    if (myVariable == "<?php echo $PHPvariable; ?>")

    It will test against a static PHP variable, that will appear in the script as the PHP variable value at the time of pre-processing.

    About the best you can do.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Posts
    3
    Rep Power
    0
    Thank you for replying....I tried using the method you shown me but it doesn't seems to work...so, I tried using this way:
    <? if ($time = 'document.test.number.value')
    $time=15;
    else
    $time=30;
    ?>
    The only thing is now, the timer doesn't acknowldege the number of questions. It kept giving me 15 minutes..
    I have also tried

    <? if ($time = 'document.test.number.value') {
    if('document.test.number.value' ==10)

    $time=15;
    else

    $time=30;

    ?>
    but useless... making matter worse!

    Brain Dead!
  6. #4
  7. No Profile Picture
    ʚGˮ
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2001
    Posts
    66
    Rep Power
    14
    I am not sure why you want to use javascript if you have PHP at your disposal. I first learned some javascript, then jumped headfirst into PHP. Javascript does not run anything on the server and PHP does not run anything on the client. Is there a reason why you cannot write the whole thing in PHP?
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Posts
    3
    Rep Power
    0
    hmm, I asked around, and it seems that PHP dunno have a timer function... hope I'm wrong
  10. #6
  11. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    793
    As far as I know, there is not a timer function in PHP, but I think you are missing the point between JavaScript and PHP.

    JavaScript is executed on the client side of things. e.g.
    Once the page is displayed and the user clicks a button, you can call a js function that will perform something.

    PHP is executed on the server side of things so it is done doing all it is going to do before the user ever sees it. The only way to make PHP do anything from the client side is to submit info back to the server that would call another PHP script.
  12. #7
  13. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2001
    Location
    New Zealand
    Posts
    1,774
    Rep Power
    25
    Let me clarify that when I said server-side earlier I was talking about S-S JavaScript. Not all JS is carried out C-S and S-S JavaScript though not in common use is used out there. The only way to get javascript to use a PHP variable that isn't static is to load a PHP page with data being passed to it by Formpost/cookie and to have a javascript function in that page that uses the static variable (the non-staticness coming from repetetively loading the PHP page). I have used this method before, using an IFrame to do the loading of the PHP page. It worked effectively but slow. Really there isn't a good way to get Javascript PHP interaction that is non-static.

IMN logo majestic logo threadwatch logo seochat tools logo