Ajaxを使用してpostしたデータをPHPで表示させるまでのやり方がわからないのでご教授願います。
<common.js>
javascript
1$(function() { 2 $('input[name=hoge]').click(function() { 3 var param = {sendData : $(this).val()}; 4 $.ajax( { 5 url : "index.php", 6 cache : false, 7 type:"POST", 8 data : {sendData : $(this).val()}, 9 success: function(data){ 10 $('sendData').html(param) 11 }, 12 error: function(XMLHttpRequest, textStatus, errorThrown) { 13 }, 14 }); 15 }); 16});
<index.php>
PHP
1<?php 2//ConfigDataの参照 3require_once (dirname(__FILE__) . '/templates/config.php'); 4//Templateになるheader.phpをリクワイア 5require_once (dirname(__FILE__) . '/templates/header.php'); 6 7require_once (dirname(__FILE__) . '/templates/body.php'); 8 9 10 11var_dump($_POST['sendData']); 12 13if (isset($_POST['sendData'])) 14{ 15 //ここに何かしらの処理を書く(DB登録やファイルへの書き込みなど) 16 echo "OK"; 17} 18else 19{ 20 echo 'The parameter of "request" is not found.'; 21} 22?>
<body.php>
php
1<?php 2?> 3<h2>ajaxテストプロジェクト</h2> 4<form method="POST"> 5 <input type="button" name="hoge" value="viewA"> 6 <input type="button" name="hoge" value="viewB"> 7</form>
common.jsに記載している "$('sendData').html(param)" の
(param)を(data)に変更したのですがpostによる結果の表示までいたりませんでした。
43185より
その場合、data に Response 内容が格納されているので、それを表示させます。
中身は console.log(data); で確認ができるので、それを表示したい内容の加工するだけです。
とご教授をいただいておりますが、表示まで至っておりません。
極力ページのリロードを行わないようにしたいです。
ページの遷移比較ができるように追記掲載します
以下現行のページ表示状態
html
1 </head> 2 <body> 3 <h1>テストプロジェクト</h1> 4 </body> 5</html><h2>ajaxテストプロジェクト</h2> 6<form method="POST"> 7 <input type="button" name="hoge" value="viewA"> 8 <input type="button" name="hoge" value="viewB"> 9</form><pre class='xdebug-var-dump' dir='ltr'><font color='#3465a4'>null</font> 10</pre>The parameter of "request" is not found.
現行の表示されているページイメージ
以下、期待する遷移後のページ結果
html
1<!DOCTYPE html> 2<html> 3 <head> 4 5~~~~~略~~~~ 6 </head> 7 <body> 8 <h1>テストプロジェクト</h1> 9 </body> 10</html><h2>ajaxテストプロジェクト</h2> 11<form method="POST"> 12 <input type="button" name="hoge" value="viewA"> 13 <input type="button" name="hoge" value="viewB"> 14</form><pre class='xdebug-var-dump' dir='ltr'><small>string</small> <font color='#cc0000'>'viewA'</font> <i>(length=5)</i> 15</pre>OK