下記のように、AJAXでMYSQLにデータを格納したいのですが、なんら格納されません。
また、AJAXで出力された$respの結果は””(空)でした。MYSQLのテーブルの名称や構造は正しいということは確認しているのですが、どこか誤りがあればご助言をいただければ幸いです。
よろしくお願い致します。
php
1//main.php 2//前略 3<input type="radio" name="radio" value="left"> 4<input type="radio" name="radio" value="right"> 5<textarea class="textarea"></textarea> 6<button class="submit">送信</button> 7<?php include('includejs.php');?> 8//後略 9
php
1//includejs.php 2$(function(){ 3 $('.submit').on('click',function(){ 4 5 6 let textarea = $('.textarea').val(); 7 let select = $('input[name="radio"]:checked').val(); 8 let idnum = <?php h($_GET['id']);?>; 9 let username = "<?php h($_SESSION['name']);?>"; 10 11 12 $.ajax({ 13 type:'POST', 14 url:'Ajax.php', 15 datatype:'json', 16 data:{ 17 sel:select, 18 tex:textarea, 19 id:idnum, 20 user:username 21 } 22 }) 23 .then( 24 function(data){ 25 console.log(data); 26 },function(){//pattern fail 27 alert('通信に失敗しました、通信環境を確認してください'); 28 }) 29 } 30 } 31 }) 32 })
php
1//Ajax.php 2//前略 3<?php 4$get_textarea=$_POST['tex']; 5$get_select=$_POST['sel']; 6$get_id=$_POST['id']; 7$get_user=$_POST['user']; 8 9 try { 10 $pdo->beginTransaction(); 11 try { 12 $stmt = $pdo->prepare('INSERT INTO result_table (selection,maincomment,pageid,username) VALUES (:selection,:maincomment,:pageid,:userame)'); 13 $stmt->bindValue(':selection',$get_select,PDO::PARAM_STR); 14 $stmt->bindValue(':maincomment',$get_textarea,PDO::PARAM_STR); 15 $stmt->bindParam(':pageid',$get_id,PDO::PARAM_INT); 16 $stmt->bindValue(':username',$get_user,PDO::PARAM_STR); 17 $stmt->execute(); 18 $resp="success"; 19 $pdo->commit(); 20 } catch (PDOException $e) { 21 $pdo->rollback(); 22 $resp="error1"; 23 throw $e; 24 } 25 } catch (PDOException $e) { 26 $resp=$error2; 27 exit(); 28 } 29 30 31 32header('Content-type:application/json;charset=utf-8'); 33echo json_encode($resp); 34?>
回答1件
あなたの回答
tips
プレビュー