1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

初めてのajaxその2

Last updated at Posted at 2017-08-20

#出ができたので入りも作る
前回の投稿でajaxの出力が出来たので、入力も作ってみることにしました。

お手本
http://webcake.no003.info/webdesign/jquery-ajax-php-post-sample.html

#実践
##JavaScript側実装
XXXXが実行されたらdataに設定した情報をpostパラメータとして
PHPに送ります。

      function XXXX(){ 
        var data = {param : 'new_message'};
        $.ajax({
          url: 'http://192.168.XX.XX/XXX/XXX_php', //PHPの処理のパス
          type:'POST',
          data: data,
          timeout:1000,
          success: function(data) {
                        console.log(data);
                     },
          error: function(data) {
                         console.log(data);
                     }
        });
      };

##PHP側実装
PHP側はPOSTパラメータとして受取り、文字列を連結してechoします。

      function XXX_php()
        {
            if (isset($_POST['param']))
            {
                echo "OK param = " . $_POST['param'];
            }
            else
            {
                echo 'The parameter of "request" is not found.';
            }
        }

結果

OK param = new_message      

あとはPHP内の処理をいじると色々できそう。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?