{"id":1414,"date":"2014-09-23T21:53:22","date_gmt":"2014-09-23T12:53:22","guid":{"rendered":"http:\/\/www.prime-architect.co.jp\/myblog\/?p=1414"},"modified":"2014-09-23T22:45:46","modified_gmt":"2014-09-23T13:45:46","slug":"cakephp%e3%81%a7ajax%e6%9b%b4%e6%96%b0%e5%87%a6%e7%90%86%e3%82%92%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/www.prime-architect.co.jp\/myblog\/ec-cube-1414","title":{"rendered":"CakePHP\u3067ajax\u66f4\u65b0\u51e6\u7406\u3092\u3059\u308b"},"content":{"rendered":"<p>\u6700\u8fd1\u3001CakePHP\u3092\u4f7f\u3063\u3066\u30b7\u30b9\u30c6\u30e0\u3092\u4f5c\u308b\u969b\u3001ajax\u66f4\u65b0\u3059\u308b\u51e6\u7406\u3092\u3088\u304f\u4f5c\u308b\u306e\u3067\u305d\u306e\u8fba\u306e\u3084\u308a\u65b9\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n<p>\u57fa\u672c\u7684\u306bajax\u3067\u66f4\u65b0\u3057\u305f\u307b\u3046\u304c\u3001\u51e6\u7406\u3082\u30b9\u30e0\u30fc\u30ba\u3067\u30d5\u30a1\u30c3\u30c8\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u306b\u306a\u308a\u305a\u3089\u3044\u3068\u3044\u3046\u306e\u3082\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u307e\u305a\u306fController\u306e\u307b\u3046\u304b\u3089<\/p>\n<p>\u901a\u5e38\u3001scaffold\u306a\u3069\u3067\u9aa8\u7d44\u307f\u3092\u4f5c\u308b\u3068\u3001\u66f4\u65b0\u51e6\u7406\u306fedit\u30e1\u30bd\u30c3\u30c9\u3067\u884c\u308f\u308c\u307e\u3059\u3002<\/p>\n<p>\u3053\u3093\u306a\u611f\u3058\u3067\u3059\u306d\u3002<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n    public function edit($id = null) {\r\n        $this-&gt;Post-&gt;id = $id;\r\n        if (!$this-&gt;Post-&gt;exists()) {\r\n            throw new NotFoundException(__('Invalid %s', __('post')));\r\n        }\r\n\r\n        \/\/post\u306e\u5834\u5408\u3001\u66f4\u65b0\r\n        if ($this-&gt;request-&gt;is('post') || $this-&gt;request-&gt;is('put')) {\r\n\r\n            if ($this-&gt;Post-&gt;save($this-&gt;request-&gt;data)) {\r\n                $this-&gt;Session-&gt;setFlash('\u51e6\u7406\u306b\u6210\u529f');\r\n                $this-&gt;redirect(array('action' =&gt; 'index'));\r\n            } else {\r\n                $this-&gt;Session-&gt;setFlash('\u51e6\u7406\u306b\u5931\u6557');\r\n            }\r\n        } else {\r\n            \/\/edit\u306e\u753b\u9762\u8868\u793a\u51e6\u7406\r\n            $this-&gt;request-&gt;data = $this-&gt;Post-&gt;read(null, $id);\r\n        }\r\n    }\r\n<\/pre>\n<p>\u57fa\u672c\u7684\u306b\u66f4\u65b0\u753b\u9762\u3092\u8868\u793a\u3059\u308b\u51e6\u7406\u3068\u66f4\u65b0\u3059\u308b\u51e6\u7406\u304c\u6df7\u5728\u3057\u3066\u3044\u308b\u72b6\u614b\u3067\u3059\u3002<\/p>\n<p>\u307e\u305a\u306f\u3053\u3053\u3092\u5206\u96e2\u3057\u3066\u3001edit\u30e1\u30bd\u30c3\u30c9\u5185\u306f\u66f4\u65b0\u753b\u9762\u306e\u8868\u793a\u51e6\u7406\u306e\u307f\u306b\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\/\/\u5206\u96e2\u3057\u305f\u51e6\u7406\r\npublic function edit($id = null) {\r\n    $this-&gt;Post-&gt;id = $id;\r\n    if (!$this-&gt;Post-&gt;exists()) {\r\n        throw new NotFoundException(__('Invalid %s', __('post')));\r\n    }\r\n\r\n    \/\/edit\u306e\u753b\u9762\u8868\u793a\u51e6\u7406\r\n    $this-&gt;request-&gt;data = $this-&gt;Post-&gt;read(null, $id);\r\n}\r\n<\/pre>\n<p>edit\u753b\u9762\u306e\u8868\u793a\u51e6\u7406\u3060\u3051\u306a\u306e\u3067\u3001\u304b\u306a\u308a\u30b9\u30c3\u30ad\u30ea\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u7d9a\u3044\u3066\u3001\u66f4\u65b0\u51e6\u7406\u3092\u5225\u30e1\u30bd\u30c3\u30c9\u3067\u66f8\u304d\u307e\u3059\u3002<\/p>\n<p>\u3053\u3093\u306a\u611f\u3058<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n    function ajax_edit(){\r\n\r\n        $this-&gt;autoRender = false;\r\n\r\n        if ($this-&gt;request-&gt;is('ajax')) {\r\n            try {\r\n\r\n                if(!$this-&gt;Post-&gt;save($this-&gt;request-&gt;data)){\r\n                    throw new Exception('\u51e6\u7406\u306b\u5931\u6557');\r\n                }\r\n                $data['is_error'] = false;\r\n                $data['message'] = '\u51e6\u7406\u306b\u6210\u529f';\r\n                $this-&gt;Post-&gt;commit();\r\n\r\n            } catch (Exception $e) {\r\n                $data['is_error'] = true;\r\n                $data['message'] = $e-&gt;getMessage();\r\n                $this-&gt;Post-&gt;rollback();\r\n            }\r\n            echo json_encode($data);\r\n        }\r\n    }\r\n<\/pre>\n<p>\u3053\u306e\u66f8\u304d\u65b9\u306f\u5b9f\u306f\u3001\u3069\u3093\u306a\u66f4\u65b0\u753b\u9762\u3067\u3082\u307b\u3068\u3093\u3069\u304b\u308f\u308b\u3053\u3068\u304c\u306a\u3044\u306e\u3067\u3001\u4f7f\u3044\u56de\u3057\u304c\u697d\u3067\u3059\u3002<br \/>\ntry-catch\u306b\u3059\u308b\u3053\u3068\u3067\u3001save\u51e6\u7406\u3092\u30e2\u30c7\u30eb\u5185\u306b\u79fb\u3057\u305f\u5834\u5408\u3067\u3082\u3001\u30a8\u30e9\u30fc\u51e6\u7406\u304c\u7c21\u5358\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\n\u8907\u6570\u306e\u30e2\u30c7\u30eb\u3092\u66f4\u65b0\u3059\u308b\u5834\u5408\u306a\u3069\u306f\u3001\u51e6\u7406\u304c\u6df1\u304f\u306a\u308b\u306e\u3067\u3001try-catch\u306e\u307b\u3046\u304c\u7dba\u9e97\u306b\u66f8\u3051\u307e\u3059\u3002<\/p>\n<p>\u7d9a\u3044\u3066View\u3067\u3059\u304c\u3001<\/p>\n<p>\u57fa\u672c\u7684\u306b\u306f\u4e00\u7dd2\u3067\u3001submit\u30dc\u30bf\u30f3\u3092\u901a\u5e38\u306e\u30dc\u30bf\u30f3\u306b\u3057\u3066\u3001ajax\u3067\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u98db\u3070\u3059\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n<p>\u901a\u5e38\u306e\u66f4\u65b0\u51e6\u7406<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;!-- \u901a\u5e38\u306e\u66f4\u65b0\u51e6\u7406 --&gt;\r\n&lt;?php echo $this-&gt;Form-&gt;create('Post');?&gt;\r\n\r\n&lt;!--\u30a4\u30f3\u30d7\u30c3\u30c8\u9805\u76ee--&gt;\r\n&lt;?php echo $this-&gt;Form-&gt;input('name');?&gt;\r\n\r\n&lt;!--\u66f4\u65b0\u30dc\u30bf\u30f3--&gt;\r\n&lt;?php echo $this-&gt;Form-&gt;Submit('\u66f4\u65b0');?&gt;\r\n<\/pre>\n<p>ajax\u306b\u5909\u66f4\u51e6\u7406\u3092\u5909\u66f4\u3059\u308b<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;!--ajax\u306e\u66f4\u65b0\u51e6\u7406 --&gt;\r\n&lt;?php echo $this-&gt;Form-&gt;create('Post');?&gt;\r\n\r\n&lt;!--\u30a4\u30f3\u30d7\u30c3\u30c8\u9805\u76ee--&gt;\r\n&lt;?php echo $this-&gt;Form-&gt;input('name');?&gt;\r\n\r\n&lt;!--\u66f4\u65b0\u30dc\u30bf\u30f3--&gt;\r\n&lt;button type=&quot;button&quot; id=&quot;ajax_edit&quot; &gt;\u66f4\u65b0 &lt;\/button&gt;\r\n<\/pre>\n<p>\u305d\u3057\u3066\u5b9f\u969b\u306bjquery\u3067ajax\u51e6\u7406\u3092\u66f8\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\r\n&lt;script type=&quot;text\/javascript&quot;&gt;\r\n\r\n    $(function(){\r\n        \/\/button\u3092\u30af\u30ea\u30c3\u30af\u3057\u305f\u5834\u5408\u3001\r\n        $(document).on('click', '#ajax_edit', function () {\r\n\r\n            \/\/\u30dc\u30bf\u30f3\u3092\u7121\u52b9\u5316\r\n            $('#ajax-edit').attr('disabled', true);\r\n            \/\/ajax_edit\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3059\r\n            $.post('\/posts\/ajax_edit', $('form').serialize(), function (rs) {\r\n\r\n                \/\/\u30dc\u30bf\u30f3\u3092\u623b\u3059\r\n                $('#ajax-edit').attr('disabled', false);\r\n\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\/\/\u51e6\u7406\u7d50\u679c\u304cjson\u5f62\u5f0f\u3067\u8fd4\u3063\u3066\u304f\u308b\u306e\u3067parse\u3059\u308b\r\n                var data = JSON.parse(rs);\r\n                if (data.is_error) {\r\n                    \/\/\u30a8\u30e9\u30fc\u51e6\u7406\r\n                    $('#message_area').append(data.message);\r\n                    \/\/\u30a8\u30e9\u30fc\u6642\u306e\u51e6\u7406\u3092\u8272\u3005\u66f8\u304f\r\n                        \u301c\u301c\u301c\u301c\r\n                } else {\r\n                    \/\/\u30e1\u30c3\u30bb\u30fc\u30b8\u51fa\u529b\r\n                    $('#message_area').append(data.message);\r\n                    \/\/\u6b63\u5e38\u51e6\u7406\u3092\u8272\u3005\u66f8\u304f\r\n                        \u301c\u301c\u301c\u301c\r\n                }\r\n\r\n            })\r\n        });\r\n    });\r\n\r\n&lt;\/script&gt;\r\n<\/pre>\n<p>\u7d50\u69cb\u7c21\u5358\u3067\u3059\u3002<\/p>\n<p>\u3053\u308c\u3067OK\u3067\u3059\u3002<br \/>\n\u8272\u3005\u3068\u66f4\u65b0\u51e6\u7406\u306fajax\u3067\u884c\u3046\u3068\u3001\u51e6\u7406\u304c\u7169\u96d1\u306b\u306a\u3089\u305a\u4fbf\u5229\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u3001CakePHP\u3092\u4f7f\u3063\u3066\u30b7\u30b9\u30c6\u30e0\u3092\u4f5c\u308b\u969b\u3001ajax\u66f4\u65b0\u3059\u308b\u51e6\u7406\u3092\u3088\u304f\u4f5c\u308b\u306e\u3067\u305d\u306e\u8fba\u306e\u3084\u308a\u65b9\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002 \u57fa\u672c\u7684\u306bajax\u3067\u66f4\u65b0\u3057\u305f\u307b\u3046\u304c\u3001\u51e6\u7406\u3082\u30b9\u30e0\u30fc\u30ba\u3067\u30d5\u30a1\u30c3\u30c8\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u306b\u306a\u308a\u305a\u3089\u3044\u3068\u3044\u3046\u306e\u3082\u3042\u308a\u307e\u3059\u3002 \u307e\u305a\u306f &hellip; <a href=\"https:\/\/www.prime-architect.co.jp\/myblog\/ec-cube-1414\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;CakePHP\u3067ajax\u66f4\u65b0\u51e6\u7406\u3092\u3059\u308b&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[4],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.prime-architect.co.jp\/myblog\/wp-json\/wp\/v2\/posts\/1414"}],"collection":[{"href":"https:\/\/www.prime-architect.co.jp\/myblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.prime-architect.co.jp\/myblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.prime-architect.co.jp\/myblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.prime-architect.co.jp\/myblog\/wp-json\/wp\/v2\/comments?post=1414"}],"version-history":[{"count":6,"href":"https:\/\/www.prime-architect.co.jp\/myblog\/wp-json\/wp\/v2\/posts\/1414\/revisions"}],"predecessor-version":[{"id":1420,"href":"https:\/\/www.prime-architect.co.jp\/myblog\/wp-json\/wp\/v2\/posts\/1414\/revisions\/1420"}],"wp:attachment":[{"href":"https:\/\/www.prime-architect.co.jp\/myblog\/wp-json\/wp\/v2\/media?parent=1414"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.prime-architect.co.jp\/myblog\/wp-json\/wp\/v2\/categories?post=1414"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.prime-architect.co.jp\/myblog\/wp-json\/wp\/v2\/tags?post=1414"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}