dongshan1036 2013-12-23 14:13
浏览 49
已采纳

将javascript变量传递给php变量[重复]

This question already has an answer here:

My web page

I am trying to make a highscore list. i have been seaching everywhere for an answer to this. but I can't seem to find an answer that works. yesterday I made it work yesterday. not sure which code i used think it was something like

window.location = "hihgscore_code.php=" + score

but this make it go to a new web page. i would like it to stay on my web page. then i know i have to use ajax. so i have tried post. and ajax function with the jquery but now where i want the score to stand it only becomes blank. I see no point adding my code here since it has becomed a mess after all my atempts and i am not sure what it says anymore :S

My goal is to send the score from my game made with javascript to php so that it gets the highscore list from the database and then check if the score is high enough to get on the list. then I will use <form> and <input> s to add name to the list. but for that to happen i need to get my javascript variable score into php variable :S

I hope you understand what i am trying to do if not ask. and I will try explaining it better!

i have tried window.location = "hihgscore_code.php=" + score it works but i get rerouted to http://mcclane654-productions.co.nf/game/highscore_code.php?score=7. i also most recently tried

 $.ajax({
                    type: "POST",
                    url: 'highscore_code.php',
   data: "score=" + killed})

and then used

$score = $_POST['score'];

in php to get it.

i did try a version of post yesterday in my javascript and same code in php. also tried with _GET in my php code -.-

</div>
  • 写回答

2条回答 默认 最新

  • duanjiwu0324 2013-12-28 16:29
    关注

    Since no one wanted to help I found the answer my self. so if anyone else is wondering how to do this use this script:

    javascript:

    var htmlrequest;
    if (window.XMLHttpRequest) {
    htmlrequest = new XMLHttpRequest();
    } else {
    hmtlrequest = new ActiveXObject("Microsoft.XMLHTTP");}
    htmlrequest.onreadystatechange = function() {
    if (htmlrequest.readyState == 4 && htmlrequest.status == 200) {
      document.getElementById("game").innerHTML = htmlrequest.responseText;}}
    htmlrequest.open("POST", "highscore_code.php", true);
    htmlrequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    htmlrequest.send("score=" + killed);}
    

    and to get it in php use

    $_POST['score']
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)