Javascript does not show data obtained from PHP and SQL Server

问题: I'm new to PHP and I have little knowledge of Javascript, I'm trying to create a chronometer that the time limit is obtained from a database (SQL SERVER), so when you get t...

问题:

I'm new to PHP and I have little knowledge of Javascript, I'm trying to create a chronometer that the time limit is obtained from a database (SQL SERVER), so when you get the value in PHP and assign it to a variable of Javascript function, I get a -1.

From what I read it appears to me that the PHP object type is not the same as in Javascript and this can be solved with json_encode (), however it keeps appearing -1.

 <?php
  require('db.php');
  if ($connection) 
  { 
    $TimEva = 60;
    $rs = odbc_exec($connection,"SELECT time FROM DBO.tablaCrono cr WHERE cr.id = '$EncIdx';");   
    while(odbc_fetch_row($rs))
    {
      $TimEva=odbc_result($rs,"TimEva");
    }
    echo "<div align='center'><h1><label id = 'tiempo'>".$TimEva."</label></h1></div>";
    $rs = odbc_close($connection);
    }
  else
  {
    echo "<div align='center'>no se pudo conectar</div><br />";
  }
 ?>

And in the Javascript function I have the following

var label = document.getElementById("tiempo"),
minutos = <?php  echo json_encode($TimEva,JSON_HEX_TAG);?>,

I expect 60, which is the value in the database, it shows up well in html, but the value of the variable minutes, throws -1

I could be guided, help, use AJAX, I'm really a bit lost.


回答1:

  $rs = odbc_exec($connection,"SELECT `time` FROM DBO.tablaCrono cr WHERE cr.id = '$EncIdx';");

Change your query to this. time is a word used from the language so if you want to use it as a column you need to escape it.

Also modify your JS code to something like :

<script>
    var label = document.getElementById("tiempo")
    var minutos = <?php  echo json_encode($TimEva,JSON_HEX_TAG);?>
    console.log(minutes)
</script>

Then open your console window and see what is the value that your variable has.

Lastly ensure that you actually have a value in $EncIdx variable cause we don't see how you assign a value to it. I guess it's in a part of the code you did not share.


回答2:

<script>
            function tiempo()
            {


                var label = document.getElementById("tiempo"),

                minutos   = <?php  echo json_encode($TimEva,JSON_NUMERIC_CHECK);?>,
                segundos  = 0,
                intervalo = setInterval(function(){
                            console.log(minutos)


                    if (--segundos < 0){
                        segundos = 59;
                        minutos--;
                    }

                    if (!minutos && !segundos)
                    {
                        clearInterval(intervalo);
                        alert("Lo sentimos se termino el tiempo");
                        //document.evaluacion.submit();
                    }

                    label.innerHTML = minutos + ":" + (segundos < 10 ? "0" + segundos : segundos);
                }, 1000);
            }
        </script>
  • 发表于 2019-01-16 12:20
  • 阅读 ( 215 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除