Passing values from JavaScript into MySQL/PHP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RootShell

    Passing values from JavaScript into MySQL/PHP

    Hello

    First of all i know that PHP is a server side, and JavaScript is a Client
    side programming language, but i need to know if there is any way i can add
    the variable "countDownInter val" from this particular FULL code into a MySQL
    field.

    Here's the drill:

    I have a timer on a PHP webpage, and i want to prevent the user from doing a
    refresh to reset the timer to it's full time (in this case 100), and i
    though of since i can pass the variable from PHP into JavaScript i could
    save the timer into a MySQL field and each time the user refreshs the page
    it's fethed from MySQL and passed by PHP back into JavaScript.

    So it user has only 5 seconds left, and uses a refresh, the script fetchs
    the value 5 from MySQL table and continues to count down from there.

    Is this possible? Let's me refrase that i only need a way to pass a variable
    from this JavaScript into MySQL, and that the page cannot be refreshed by
    it's own, since it has some pictures and it takes some time to load. the
    timer is on a <ilayer>

    HERE'S my example code, that shows how to pass values from PHP into
    JavaScript, please complete it in a way i can pass variable
    "countDownInter val" back into MySQL.

    *************** *************** *************** *************** *************** *
    ********

    <?PHP
    $somePhpVariabl e=100;
    ?>

    <script language="JavaS cript">
    //configure refresh interval (in seconds)
    var countDownInterv al=<?php echo $somePhpVariabl e ?>; // THIS IS WHERE
    I PASS THE PHP VARIABLE TO JAVASCRIPT
    //configure width of displayed text, in px (applicable only in NS4)
    var c_reloadwidth=2 00
    </script>

    <ilayer id="c_reload" width=&{c_reloa dwidth}; ><layer id="c_reload2"
    width=&{c_reloa dwidth}; left=0 top=0></layer></ilayer>

    <script>

    var countDownTime=c ountDownInterva l+1;
    function countDown(){
    countDownTime--;
    if (countDownTime <=0){
    countDownTime=c ountDownInterva l;
    clearTimeout(co unter)
    window.location .reload()
    return
    }
    if (document.all) //if IE 4+
    document.all.co untDownText.inn erText = countDownTime+" ";
    else if (document.getEl ementById) //else if NS6+
    document.getEle mentById("count DownText").inne rHTML=countDown Time+" "
    else if (document.layer s){ //CHANGE TEXT BELOW TO YOUR OWN
    document.c_relo ad.document.c_r eload2.document .write('Next <a
    href="javascrip t:window.locati on.reload()">re fresh</a> in <b
    id="countDownTe xt">'+countDown Time+' </b> seconds')
    document.c_relo ad.document.c_r eload2.document .close()
    }
    counter=setTime out("countDown( )", 1000);
    }

    function startit(){
    if (document.all|| document.getEle mentById) //CHANGE TEXT BELOW TO YOUR OWN
    document.write( 'Next <a
    href="javascrip t:window.locati on.reload()">re fresh</a> in <b
    id="countDownTe xt">'+countDown Time+' </b> seconds')
    countDown()
    }

    if (document.all|| document.getEle mentById)
    startit()
    else
    window.onload=s tartit

    </script>


  • Blue Raja

    #2
    Re: Passing values from JavaScript into MySQL/PHP

    RootShell wrote:[color=blue]
    > Hello
    >
    > First of all i know that PHP is a server side, and JavaScript is a
    > Client side programming language, but i need to know if there is any
    > way i can add the variable "countDownInter val" from this particular
    > FULL code into a MySQL field.[/color]

    <snip>

    AFAIK there are no Javascript functions that allow for MySQL interaction.
    That aside, storing the value in the database seems an inefficient solution.

    Have you tried putting the countdown variable into a cookie? Such a
    procedure could be:
    onload {
    get countdown from cookie
    if countdown is set
    resume countdown
    else
    begin new countdown
    }

    When the user clicks the reload link, you could call a function that puts
    the current countdown value into the cookie.

    Hope that helps.

    --

    Jason, aka The Blue Raja


    Comment

    • Dr John Stockton

      #3
      Re: Passing values from JavaScript into MySQL/PHP

      JRS: In article <4104542a$0$358 1$a729d347@news .telepac.pt>, dated Mon,
      26 Jul 2004 01:45:45, seen in news:comp.lang. javascript, RootShell
      <ANTISPAMRootSh ell@Netcabo.ptA NTISPAM> posted :
      [color=blue]
      >counter=setTim eout("countDown ()", 1000);[/color]

      That will not count seconds accurately.

      See current thread "Countdown timer inaccurate - losing time!?!" in
      c.l.j, and the FAQ, via below.

      Is a.c.l.j generally considered valid? I think Demon does not carry it.

      --
      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
      <URL:http://jibbering.com/faq/> JL / RC : FAQ for news:comp.lang. javascript
      <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

      Comment

      Working...