ajax処理について勉強していますが、いまいちこれがajax処理なのか単なるjavascriptのpost処理なのか、はたまたjqueryのpost処理なのかよく分かっていません。これはajax処理になっていますでしょうか?
やりたいことはhtmlの'input'の'value'を'BUTTON'クリックしたらphpにpostされてphpでformatされたデータをhtmlの'result'に表示することです。
(index.html)
html
1<!DOCTYPE html> 2<html lang="ja"> 3<head> 4 <meta charset="utf-8"> 5 <script src="https://code.jquery.com/jquery-3.0.0.min.js"></script> 6 <script src="test.js"></script> 7 <title>ajax Test</title> 8</head> 9 <body> 10 11 <form id="form" method="post"> 12 <div id="button">BUTTON</div> 13 <input id="date1" type="date" value=""> 14 </form> 15 16 <div id="result"></div> 17 18 </body> 19</html>
(test.js)
dataに値を入れてますが、このdataは変数的なもので考えていいでしょうか?
しかしそう考えるとconsole.log(data)では出力されないので違うのですよね・・・
jquery
1 2 3 4 5$(function() { 6 'use strict'; 7 8 $('#button').click(function() { 9 10 var val = $("#date1").val(); 11 12 $.post('sample.php', { 13 data : val 14 }, function() { 15 console.log(val); 16 }); 17 }); 18 19});
(sample.php)
これでデータが来ているのかもよく分かっていません。console.log(val)では値の確認ができていたのでpostされたと思ってもいいのでしょうか?
php
1<?php 2 3$date1 = $_POST["data"]; 4$date = date("Y年m月d日", strtotime($date1));
結果的にここで何をreturnしてhtmlにどのように出力すれば良いのでしょうか?
ご教授よろしくお願いいたします。
回答2件
あなたの回答
tips
プレビュー