简体   繁体   English

如何在数据库中存储 JavaScript 变量

[英]How to store JavaScript variable in the database

I wanted to store the value "totalscore" from my JavaScript code to my database.我想将值“totalscore”从我的 JavaScript 代码存储到我的数据库中。 I tried using ajax call but something is not working, I have not used ajax before.我尝试使用 ajax 调用,但有些东西不起作用,我以前没有使用过 ajax。

In the following JavaScript code, I display the value of score which i have found to the html element.在以下 JavaScript 代码中,我将找到的 score 值显示到 html 元素中。

JavaScript code: JavaScript 代码:

  if (matches==8){
            var totalscore = calcScore();
            document.getElementById("score").innerHTML=totalscore;
          }

I want to save the value of totalscore in my users database when the submit button is clicked.当单击提交按钮时,我想将 totalscore 的值保存在我的用户数据库中。 So i tried something like :所以我尝试了类似的东西:

   $("#sendscore").on("click",function(){

   gamescore= document.getElementById('score').innerHTML;
   $.ajax({
   type:'POST',
   url: 'score-processor.php',
   data:{
      gamescore: gamescore,
    }
   })
  });

the php code : php代码:

<?php
  session_start();
  $db = mysqli_connect('localhost', 'root', '', 'registration');
  if (isset($_POST['login_user'])) {
  $username = mysqli_real_escape_string($db, $_POST['username']);
  $password = mysqli_real_escape_string($db, $_POST['password_1']);

  if (empty($username)) {
   array_push($errors, "Username is required");
  }
  if (empty($password)) {
   array_push($errors, "Password is required");
  }

  if (count($errors) == 0) {
    $password = md5($password);
    $query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
    $results = mysqli_query($db, $query);
    if (mysqli_num_rows($results) == 1) {
    $_SESSION['username'] = $username;
     header('location: profile.php');
    }
    else {
    array_push($errors, "Wrong username/password combination");
   }
 }
}
   if(isset($_POST['gamescore'])){
   $fetch = "SELECT id FROM users WHERE username='$username'";
   $fetchid =mysqli_query($db, $fetch);
   while ($row=mysqli_fetch_array($fetchid)){
   $id = $row['id'];
   $gamescore= $_POST['gamescore'];
   $updatescore= "INSERT INTO users(id, score)VALUES('$id','$gamescore') ON DUPLICATE KEY UPDATE score='$gamescore'";
   mysqli_query($db, $updatescore);
   }
   }

In my html :在我的 html 中:

 <?php session_start();?>

 <body> 
 <p>Your score: <span id=score></p>
 <button id="sendscore" class="Go-on">Submit</button>

the database table has columns , id, username, email, password and score.数据库表有列、id、用户名、电子邮件、密码和分数。

the value for columns id, username, email and password are collected during login/register.在登录/注册期间收集列 id、用户名、电子邮件和密码的值。

The game runs smoothly and presents the score but when I click the submit button which on clicked should add the value to the table, but nothing happens, no errors in the log and the value is not added to the table.游戏运行流畅并显示分数,但是当我单击提交按钮时,单击该按钮应将值添加到表中,但没有任何反应,日志中没有错误并且值未添加到表中。

Problem 1问题一

gamescore= document.getElementById('score');

This is an HTML element, not the value of it.这是一个 HTML 元素,而不是它的值。

You need to read the .innerHTML just like you wrote to it earlier你需要像你之前写的那样阅读.innerHTML


Problem 2问题二

gamescore: gamescore

jQuery.ajax doesn't have a gamescore option. jQuery.ajax 没有gamescore选项。 So this is meaningless.所以这是没有意义的。

You need to pass data .你需要传递data

data: {
    gamescore: gamescore
}

Problem 3问题三

contentType: false,

This stops jQuery overriding the content-type when you pass a FormData object to generate a multipart request (which is useful for uploading files).当您传递FormData对象以生成多部分请求(这对于上传文件很有用)时,这会阻止 jQuery 覆盖内容类型。

You aren't doing that, so contentType: false will break the normal allocation of the correct Content-Type header.您没有这样做,因此contentType: false破坏正确 Content-Type 标头的正常分配。

Remove that删除那个


Problem 4问题四

processData: false

You need the data to be processed.需要处理数据。 The object you pass to data needs encoding into the HTML request.您传递给data的对象需要编码到 HTML 请求中。

Remove that.去掉那个。


Problem 5问题五

 $updatescore= "UPDATE users SET(username='$username', score='$gamescore') WHERE (id='$id')";

You failed to define $username or $id .您未能定义$username$id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM