JSの変数の値を、PHPに渡したいと思いajaxを利用してます。
エラーで、Notice: Undefined index: win_count と出てしまい、うまく値が渡せていないようです。
書き方など間違って入れば教えていただけると助かります。
JS
1let stepY_count = 0; 2let stepX_count = 0; 3let step_count = 0; 4let meet_count = 0; 5let sum_count = 0; 6let win_count = 0; 7let appear_count = 0; 8let revel_count = 1; 9let stage; 10let kazan = -1; 11let mori = 2; 12let heichi = 1; 13 14class Enemy { 15 constructor (name) { 16 this.name = name; 17 } 18 ene1() { 19 this.rand1 = 1 + Math.floor(Math.random()*100); 20 } 21 appear() { 22 $("#result_area").append('<div>「' + enemy1.name + 'が現れた!」</div>'); 23 } 24} 25 26const enemy1 = new Enemy('デーモン'); 27enemy1.ene1(); 28 29class Yusha extends Enemy { 30 status1() { 31 this.number1 = 70; 32 } 33 status2() { 34 this.number2 = 50; 35 } 36 win() { 37 $("#result_area").append('<div>「勝った」</div>'); 38 } 39 lose() { 40 $("#result_area").append('<div>「負けた」</div>'); 41 } 42 defeat() { 43 $("#defeat_count").html("倒した敵の数:" + win_count); 44 } 45 process1() { 46 $("#before,#left,#back,#right").prop("disabled",false); 47 } 48 process2() { 49 $("#fight,#escape").prop("disabled",true); 50 } 51} 52 53const yusha = new Yusha('勇者'); 54yusha.status1(); 55yusha.status2(); 56 57$("#fight,#escape").prop("disabled",true); 58$(function(){ 59 $('#before,#back,#left,#right').off('click'); 60 $("#before,#back,#left,#right").on( 'click',() => { 61 //スタートが押されたら初期化 62 $("#start").on('click',() => { 63 $("#result_area,#log_date").empty(); 64 $("#revel").html("LV:0"); 65 $("#stepY").html("歩数Y:0"); 66 $("#stepX").html("歩数X:0"); 67 $("#defeat_count").html("倒した敵の数:0"); 68 $("#appear_average").html("敵出現平均歩数:0"); 69 stepY_count = 0; 70 stepX_count = 0; 71 step_count = 0; 72 meet_count = 0; 73 sum_count = 0; 74 win_count = 0; 75 appear_count = 0; 76 revel_count = 1; 77 $("#image").attr("src","images/heichi.jpeg"); 78 yusha.process1(); 79 yusha.process2(); 80 }); 81 //ダンジョン切り替え 82 step_count++; 83 if ((step_count % 6 === 0) || (step_count % 7 === 0)|| (step_count % 8 === 0)|| (step_count % 9 === 0)){ 84 $("#image").attr("src","images/mori.jpeg"); 85 stage = mori; 86 } 87 else if ((step_count % 10 === 0) || (step_count % 11 === 0) || (step_count % 12 === 0)){ 88 $("#image").attr("src","images/kazan.jpeg"); 89 stage = kazan; 90 } 91 else { 92 $("#image").attr("src","images/heichi.jpeg"); 93 stage = heichi; 94 } 95 //乱数生成 96 let randnum =1 + Math.floor(Math.random()*3 + stage); 97 meet_count++; 98 sum_count++; 99 //乱数と歩数が同じなら戦闘開始 100 if (randnum === meet_count){ 101 $("#fight,#escape").prop("disabled",false); 102 $("#before,#left,#back,#right").prop("disabled",true); 103 appear_count++; 104 $("#log_date").append("<div>" + sum_count + "歩目</div>"); 105 enemy1.appear() 106 $("#image").attr("src","images/enemy.jpeg"); 107 let average = sum_count / appear_count; 108 let rounding_average = average.toFixed(0); 109 $("#appear_average").html("敵出現平均歩数:" + rounding_average); 110 //戦うボタンが押されたら 111 $("#fight").off('click'); 112 $("#fight").on('click',() => { 113 enemy1.ene1(); 114 let win_or_lose = enemy1.rand1; 115 if (win_or_lose <= ( yusha.number1 + revel_count)){ 116 yusha.win(); 117 $("#image").attr("src","images/win.jpeg"); 118 yusha.process1(); 119 yusha.process2(); 120 win_count++; 121 yusha.defeat(); 122 if (win_count % 10 === 0){ 123 revel_count++; 124 $("#revel").html("LV:" + revel_count); 125 } 126 } 127 if (win_or_lose >= ( yusha.number1 + revel_count + 1)){ 128 yusha.lose(); 129 $("#image").attr("src","images/lose.jpeg"); 130 yusha.process1(); 131 yusha.process2(); 132 } 133 }); 134 //逃げるボタンが押されたら 135 $("#escape").off('click'); 136 $("#escape").on('click',() => { 137 enemy1.ene1(); 138 let escape_result = enemy1.rand1;; 139 if (escape_result <= 0){ 140 $("#result_area").append('<div>「逃げられた」</div>'); 141 yusha.process1(); 142 yusha.process2(); 143 } 144 if (escape_result >= 1){ 145 $("#result_area").append('<div>「逃げられない」</div>'); 146 enemy1.ene1(); 147 let end_result = enemy1.rand1;; 148 if (end_result <= ( yusha.number2 - revel_count)){ 149 yusha.lose(); 150 $("#result_area").append('<div>「' + yusha.name + 'は死んでしまった」</div>'); 151 $("#image").attr("src","images/lose.jpeg"); 152 yusha.process1(); 153 yusha.process2(); 154 } 155 if (end_result >= ( yusha.number2 - revel_count - 1)){ 156 $("#image").attr("src","images/win.jpeg"); 157 yusha.win(); 158 $("#result_area").append('<div>「歩く方向を押してください」」</div>'); 159 yusha.process1(); 160 yusha.process2(); 161 win_count++; 162 yusha.defeat(); 163 if (win_count % 10 === 0){ 164 revel_count++; 165 $("#revel").html("LV:" + revel_count); 166 } 167 } 168 } 169 }); 170 } 171 //歩数が乱数を超えたら 172 if (randnum < meet_count){ 173 meet_count = 0; 174 } 175 }); 176}); 177 178//歩数Y,Xをカウント、出力 179 $(function(){ 180 $("#before,#back").on( 'click',() => { 181 stepY_count++; 182 $("#stepY").html("歩数Y:" + stepY_count); 183 }); 184 $("#left,#right").on( 'click',() => { 185 stepX_count++; 186 $("#stepX").html("歩数X:" + stepX_count); 187 }); 188 }); 189 190 191//非同期通信 192$(function () { 193 $("#ranking").on("click",function(){ 194 alert("a"); 195 $.ajax({ 196 type: "POST", 197 url:"ajax_return.php", 198 data: {"win_count":win_count,"step_count":step_count} 199 }); 200 }); 201}); 202
php
1<?php 2$win_count = $_POST['win_count']; 3$step_count = $_POST['step_count']; 4echo $win_count; 5echo $step_count; 6?>
追記致しました。このようなjsのファイルがありました。とりあえずPHPに値
持って来られてるかの確認でPHPファイルでechoを出してみたところNotice: Undefined index: win_count in~
Notice: Undefined index: step_count in ~
というエラーが出ています。
回答3件
あなたの回答
tips
プレビュー