doulan0297 2014-03-01 20:50
浏览 74
已采纳

使用Ajax获取javascript变量的值?

Long time stackoverflow user but first time poster! I recently made a very simple little browser based game, and when I say simple I cannot stress this enough.

Anyway, the entire game logic is in one little javascript file. In that file I have a variable called value, and it contains the players score.

The user at any given time during gameplay is allowed to click Submit, and this then uses a php script to access and write their username and hopefully soon their score to my database.

The problem I am having and I know quite a few people new to Ajax have had is, how do I send the value of the javascript variable value to my php page which handles sending the variables to my database?

I have read quite a bit on this and can't seem to get it working. So starting from scratch, assume I have a functioning javascript file with the value variable containing the score. How do I send the score to the php file using ajax? Where would I place the Ajax code?

Thank you stackoverflow! Also I already know that javascript resides on the client side and php runs on the server side, I just would like to know how to properly send the score to the php file, that is all! Thanks again stackoverflow!

EDIT: Because I seem to be getting down voted this is what I have but it does not work:

My Ajax:

function ajaxCall() {alert("To AJAX: the value is: " + value);

    $.ajax
    (   {
            type: "POST",
            url: "handler.php",
            data: {'value' : value}, 
            success: function(response)
            { alert("The value was passed!")}
        }
    );};

ajaxCall();

Part of handler.php:

if(isset($_POST['value']))

{
$value = $_POST['value']."";
var_dump($value);
}

var_dump($_POST['value']);
  • 写回答

2条回答 默认 最新

  • duanci6484 2014-03-01 21:09
    关注

    Simply use this,

    your ajax.js should contain this.

    $(document).ready(function() {
       $("#submitscore").click(function(event){
         var user = $('#username').val();
         var value = document.getElementById('yourscore').innerHTML;
          $.post( 
             "submitscore.php",
             { username: user, score: value }
               );
      });
    });
    

    Your other file should have a button with the id "submitscore"

    Hi, <input type="text" id="username" size="12"><br/>
    Your Score is : <span id="yourscore">5000</span><br/>
    <button id="submitscore">Submit Score</button>
    

    And in that submit file, use this:

    if(isset($_REQUEST['score'])||isset($_REQUEST['username']))
    {
    $username = $_REQUEST['username'];
    $value = $_REQUEST['score'];
    var_dump($value);
    //Your Insertion/Updation Databse Script
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决