計算機みたいなのを作っているのですが、jqueryで要素($selected)を取得してphp側で処理したいのですが、console.logで取得できているか確認したところできていませんでした。原因が分からなくて困っております。以下にCodeを添付致しますのでご査収の程宜しくお願いいたします。
php
1<?php 2 3require_once(__DIR__ . '/functions.php'); 4 5$text1 =''; 6$text2=''; 7 8if ($_SERVER['REQUEST_METHOD'] === 'POST'){ 9 $text1 = $_POST['text1']; 10 $text2 = $_POST['text2']; 11} 12 $result = $text1 + $text2 13 ?> 14 15<!DOCTYPE html> 16<html lang = "ja"> 17<head> 18 <meta charset = "utf-8"> 19 <title>calculator</title> 20 <link rel="stylesheet" href="styles.css"> 21</head> 22<body> 23<div id ="container"> 24 <h1>計算</h1> 25 <form action="" method="POST"> 26 <div id="container-1"> 27 <input type="text" id="text1" name="text1" placeholder="text1" value="<?= h($text1); ?>" onKeyup="this.value=this.value.replace(/[^0-9]+/,'')"> 28 <ul> 29 <li data-id="plus" class="btn">+</li> 30 <li data-id="mainasu" class="btn">−</li> 31 <li data-id="kakeru" class="btn">×</li> 32 <li data-id="waru" class="btn">÷</li> 33 <input type="hidden" id="answer" name="answer" value=""> 34 </ul> 35 <input type="text" id="text2" name="text2" placeholder="text2" value="<?= h($text2); ?>" onKeyup="this.value=this.value.replace(/[^0-9]+/,'')"> 36 </div> 37 <div id="result">result</div> 38 <div id="cal_result"><?= h($result); ?></div> 39</form> 40</div> 41<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 42<script> 43$(function(){ 44 'use strict'; 45 46 $('.btn').on('click', function(){ 47 $('.btn').removeClass('selected'); 48 $(this).addClass('selected'); 49 $selected = $('#answer').val($(this).data('id')); 50 }); 51 $('#result').on('click', function(){ 52 if ($('#text1').val() === '' || $('#text2').val() === ''){ 53 alert('Choose One!'); 54 console.log($selected); 55 56 } else { 57 $('form').submit(); 58 } 59 }); 60}); 61</script> 62</body> 63</html> 64
css
1body { 2 font-size: 16px; 3 font-family: Arial, sans-serif; 4} 5 6#container { 7 text-align: center; 8 width: 500px; 9 margin: 15px auto; 10} 11h1{ 12 font-size: 20px; 13 line-height:50px; 14 padding: 10px; 15} 16h1 ,ul > li { 17 border: 1px solid #ddd; 18 border-radius: 5px; 19} 20#container-1{ 21 display: flex; 22} 23ul{ 24margin:auto; 25 list-style: none; 26 padding: 0; 27} 28li{ 29 width:20px; 30 margin: auto; 31 cursor: pointer; 32} 33#text1{ 34 height: 10px; 35 margin-top: 25px; 36} 37#text2{ 38 height: 10px; 39 margin-top: 25px; 40} 41#result{ 42 margin-top: 0px; 43 float:right; 44 cursor: pointer; 45 box-shadow: 0 4px 0 #0088cc; 46 border-radius: 5px; 47 background: #00aaff; 48 width: 100px; 49 color: #fff; 50} 51#cal_result{ 52 float:right; 53 margin-top: 30px; 54} 55.btn{ 56 opacity: 0.5; 57 font-weight: bold; 58} 59.btn + .btn{ 60 margin-top: 5px; 61} 62.selected{ 63 opacity: 1.0; 64} 65
php
1<?php 2 3function h($s){ 4 return htmlspecialchars($s, ENT_QUOTES, 'UTF-8'); 5}
回答1件
あなたの回答
tips
プレビュー