最近jQueryでpostデータを送信し、htmlに表示する処理を書いています。
しかし、実行結果が思い通りになりません。
コードは
php
1<!DOCTYPE html> 2<html lang="ja" dir="ltr"> 3 <head> 4 <meta charset="utf-8"> 5 <title>TEST</title> 6 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 7 </head> 8 <body> 9 10 11 <p id="test">jQueryの練習</p> 12 <input type="text" id="name" value=""> 13 <input type="button" id="button" value="ボタン"> 14 <p id="result"></p> 15 16 <script type="text/javascript"> 17 $(function(){ 18 $("#button").click(function(){ 19 $.post('test.php', 20 { 21 name: $("#name").val() 22 }, 23 function(data){ 24 $("#result").html(data); 25 }); 26 }); 27 }); 28 </script> 29 30 <?php 31 $name = $_POST['name']; 32 echo $name; 33 34 ?> 35 36 </body> 37</html>
このコードを実行すると
初期画面は
jQueryの練習
テキストエリア ボタン
Notice: Undefined index: name in C:\xampp\htdocs\test\test.php on line 31
になり
テキストエリアに文字を入力し、ボタンを押すと
jQueryの練習
テキストエリア ボタン
jQueryの練習
テキストエリア ボタン
テキストエリアに入力した文字列
Notice: Undefined index: name in C:\xampp\htdocs\test\test.php on line 31
になってしまいます。
改善したいのは
①phpの変数が未定義になっている所
②実行結果を、
jQueryの練習
テキストエリア ボタン
テキストエリアに入力した文字列
という風にしたいです
php,jQueryに詳しい方コードの改善お願いします
回答1件
あなたの回答
tips
プレビュー