お世話になります。
以下の点につきまして、お知恵をお借りできますと幸いでございます。
宜しくお願い致します。
1、やりたいこと
javascriptのクリックイベントでajaxを使いPHPに値を渡し、
データベースへ受け取った値を格納したのちに、いくつか数値計算を行い、計算結果を
javascriptへ返す
2、問題、ぶつかっていること
PHPに値を渡し、データベース格納、数値計算を行うところまで出来たのですが、
返り値としてjavascriptに値を渡すことができない状況です。
※以下のプログラム2つ目の、「//ここまでの変数を以下でクライアントに返す」
の上あたりの部分です
3、お伺いしたい事項
上記の問題につきまして、どこを修正すると良いのか、
よろしければご教示頂けますと幸いでございます。
宜しくお願い致します。
<クライアント>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja"> <head class=""> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript"> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js"></script> <link rel="stylesheet" href="style.css" type="text/css"> <script> function OnLinkClick3(element) { var id = element.id; var data = {content_id2 : id}; $.ajax({ type: "POST", url: "***", data: data, success: function(data,dataType) { var button = document.getElementById("btn2"); alert(data) }, error: function(XMLHttpRequest, textStatus, errorThrown) { alert('Error : ' + errorThrown); } }); return false; } </script> </head> <body> <form action="" method="post"> <input type="hidden" id="cont_id" value="<?php echo $comment_id ?>"> <input type="btn" onclick="OnLinkClick3(this);" id="<?php echo $comment_id; ?>" value="plus" class="review_down"> </form> </body>
<サーバー>
<?php header("Content-type: text/plain; charset=UTF-8"); if(isset($_POST['content_id'])) { $content_id = $_POST['content_id']; require("db-connect.php"); $sql10 = "SELECT * FROM comment_table WHERE`comment_id`=$content_id"; $result10 = $mysqli -> query($sql10); $data10 = $result10 -> fetch_array(); $count = $data10['good']; $good = $count + 1; $sql11 = "UPDATE comment_table SET good=$good WHERE `comment_id`=$content_id"; $result11 = $mysqli -> query($sql11); $data11 = $result11 -> fetch_array(); //再度lenthとパーセンテージ算出 }elseif(isset($_POST['content_id2'])) { //評価ダウンの処理 $content_id = $_POST['content_id2']; require("db-connect.php"); $sql11 = "SELECT * FROM comment_table WHERE`comment_id`=$content_id"; $result11 = $mysqli -> query($sql11); $data11 = $result11 -> fetch_array(); $count = $data11['bad']; $bad = $count + 1; $stmt12 = $mysqli -> prepare("UPDATE comment_table SET bad=? WHERE `comment_id`=$content_id"); $stmt12 -> bind_param('i',$bad); $result12 = $stmt12 -> execute(); if($result12){} //変更処理finish //再度lenthとパーセンテージ算出 $sql13 = "SELECT * FROM comment_table WHERE `comment_id`=$content_id"; $result13 = $mysqli -> query($sql13); $data13 = $result13 -> fetch_array(); $good = $data13['good']; $bad = $data13['bad']; //パーセンテージ計算 $totalrep = $good + $bad; if($totalrep==0) { $goodrate = 0; $badrate = 0; //文字色と大きさ指定 $color = "#808080"; $font_weight = "normal"; $backcolor = "#808080"; }else { $goodrate = ($good/$totalrep)*100; $badrate = 100 - $goodrate; $sum = $good - $bad; //文字色と大きさ指定 //+-10以上 if($sum >= 10 and $sum < 30) { //10以上30未満で太字にする $color = "#808080"; $font_weight = "bold"; }elseif($sum >= 30) { //+-30以上で青字で太字にする $color = "#1e90ff"; $font_weight = "bold"; }else { //通常時の文字設定設定 $color = "#808080"; $font_weight = "normal"; } } //barlengh if($totalrep==0) { $width = "20"; $good = ""; $bad = ""; }elseif($totalrep >= 1 && $totalrep <= 200) { $width = ($totalrep * 2) + 20; }elseif($totalrep > 201) { //長さは最大バーで一律 $width = "420"; } //ここまでの変数を以下でクライアントに返す $result[] = array( 'width' => $width, 'good'=>$good, 'bad'=>$bad, 'color'=>$color, 'font_weight'=>$font_weight, 'goodrate'=>$goodrate, 'badrate'=>$badrate ); echo json_encode($result); //finish }else{ echo 'The parameter of "request" is not found.'; } ?> </body>
また、実際のクリックイベントでは、ajax通信が成功した時には
alertで以下が表示されます。
※一番下のところだけ受け取りたいイメージです
<!doctype html> <html lang="ja" class="message"> <head> <meta charset="utf-8"> <link rel="stylesheet" href="style.css" type="text/css" > </head> <body> </body>[{"width":62,"good":"3","bad":"18","color":"#808080","font_weight":"normal","goodrate":14.285714285714,"badrate":85.714285714286}]</body>
回答1件
あなたの回答
tips
プレビュー