###前提・実現したいこと
- PHP5.5.38
- Apache/2.4.23
- MacBook Pro (Retina, 13-inch, Mid 2014)
- macOS Sierra
簡単なAjaxのサンプルを作ってまして、PHP出力結果を取得してその結果にてHTMLを書き換える処理を書いているのですが、
なぜか出力結果ではなくPHPソースコード文字列を取ってきてしまっています。
###該当のソースコード
html
1<html> 2 <head> 3 <meta charset="UTF-8"> 4 <title>Boards 'R' Us</title> 5 <!-- <link rel="stylesheet" type="text/css" href="boards.css" /> --> 6 </head> 7 8 <body> 9 <h1>Boards 'R' Us :: Custom Boards Report</h1> 10 <div id="boards"> 11 <table> 12 <tr><th>Snowboards Sold</th> 13 <td><span id="boards-sold">1012</span></td></tr> 14 <tr><th>What I Sell 'em For</th> 15 <td>$<span id="price">249.95</span></td></tr> 16 <tr><th>What it Costs Me</th> 17 <td>$<span id="cost">84.22</span></td></tr> 18 </table> 19 <h2>Cash for the Slopes: 20 $<span id="cash">167718.76</span></h2> 21 <form method="GET" action="getUpdatedBoardSales.php"> 22 <input id="submit" value="Show Me the Money" type="button" /> 23 </form> 24 </div> 25 26 <script> 27 (function() { 28 29 var request = null; 30 31 document.getElementById('submit').addEventListener('click', function() { 32 getBoardsSold(); 33 }, false); 34 35 function createRequest() { 36 try { 37 request = new XMLHttpRequest(); 38 } catch (trymicrosoft) { 39 try { 40 request = new ActiveXObject('Msxm12.XMLHTTP'); 41 } catch (othermicrosoft) { 42 try { 43 request = new ActiveXObject('Microsoft.XMLHTTP'); 44 } catch (failed) { 45 request = null; 46 } 47 } 48 } 49 50 if (request === null) { 51 alert('エラー! requestオブジェクトの作成に失敗しました。'); 52 } 53 } 54 55 function getBoardsSold() { 56 createRequest(); 57 request.open('GET', './getUpdatedBoardSales-ajax.php', true); 58 request.onreadystatechange = updatePage; 59 request.send(null); 60 } 61 62 function updatePage() { 63 if (request.readyState === XMLHttpRequest.DONE && request.status === 200) { 64 var newTotal = request.responseText; 65 var boardsSoldElm = document.getElementById('boards-sold'); 66 var cashElm = document.getElementById('cash'); 67 boardsSoldElm.textContent = newTotal; 68 69 var price = document.getElementById('price'); 70 var cost = document.getElementById('cost'); 71 var costPerBoard = parseFloat(price.textContent) - parseFloat(cost.textContent); 72 var totalProfit = parseFloat(costPerBoard) * parseFloat(boardsSoldElm.textContent); 73 74 cashElm.textContent = totalProfit; 75 } 76 } 77 78 })(); 79 </script> 80 </body> 81</html>
php
1<?php 2 3// Start with an arbitrary number of boards sold 4$totalSold = 1012; 5 6// Reflect new sales 7srand((double)microtime() * 1000000); 8$totalSold = $totalSold + rand(0,1000); 9 10echo $totalSold; 11 12?>
###試したこと
ターミナルからPHPファイル実行すると、きちんと出力返してくれます。
bash
1$ php getUpdatedBoardSales-ajax.php 21599
ブラウザでPHPファイルを開いても数値が正しく表示されます。
以上
皆様のお知恵を拝借できれば幸いです。宜しくお願い致します。
回答2件
あなたの回答
tips
プレビュー