2014年10月31日金曜日

カレンダー (PHP JavaScript jQuery)

簡単なカレンダーを作成しました。

呼び出し元のフォーム名を取得できるように工夫をしました。


【index.php】

<?php

    if (isset($_POST['current_date'])) {
        $current_date = $_POST['current_date'];
    } else {
        $current_date = date('Y/m/d', strtotime('now'));
    }

?>
<!DOCTYPE html>
<html>
  <head>
  <meta charset="UTF-8">
  <title>index.php</title>
  <link rel="stylesheet" href="css/common.css">
  <script src="/js/jquery.js"></script>
  <script>
  <!--
      $(document).ready(function(){

          $('#content :input[name="select_date"]').bind('click', function(){

              var current_date = encodeURI($('#content :input[name="current_date"]').val());
              var url = 'calendar.php?current_date=' + current_date;
              var name = 'calendar';

              var calendar = window.open(url, name, 'width=400, height=400, menubar=no, toolbar=no, scrollbars=yes');
              calendar.focus();

          });

      });
  -->
  </script>
  <style>
  <!--

    #content {

    }

    #content form {
         width : 200px;
         height: 100px;
         border: 1px solid #cccccc;
    }


  //-->
  </style>
  </head>
  <body>
    <div id="content">
      <form name="form1" method="POST" action="index.php">
        <input type="text" name="current_date" value="<?php echo $current_date ?>">
        <input type="button" name="select_date" value="…">
        <br>
        <br>
        <input type="submit" value="送信">
      </form>
      <br>
      <br>
      <form name="form2" method="POST" action="index.php">
        <input type="submit" value="送信">
      </form>
      <br>
      <br>
      <form name="form3" method="POST" action="index.php">
        <input type="submit" value="送信">
      </form>
    </div>
  </body>
</html>


【calendar.php】

<!DOCTYPE html>
<html>
  <head>
  <meta charset="UTF-8">
  <title>calendar.php</title>
  <link rel="stylesheet" href="css/common.css">
  <script src="/js/jquery.js"></script>
  <script>
  <!--
      $(document).ready(function(){


          $('#calendar th').bind('click', function(){
//alert('clicked!');
              var date = $(this).attr('date');
              if (('' == date) || (undefined == date)) {
                  return false;
              }

              location.href='calendar.php?current_date=' + date;

          });


          $('#calendar td').bind('click', function(){
//alert('clicked!');
              var date = $(this).attr('date');
//alert(date);
              if (('' == date) || (undefined == date)) {
                  return false;
              }
              var parent_form_name = window.opener.$('form').map(function(){
                 if ($(this).find(':input[name="select_date"]').length) {
                     return $(this).attr('name');
                 }
              }).get();
//alert(parent_form_name);
              var parent_form = window.opener.$('form[name="' + parent_form_name + '"]')

              parent_form.find(':input[name="current_date"]').val(date);
//              parent_form.attr('action', '');
              parent_form.submit();

              window.focus();
//              window.close();

          });

      });
  -->
  </script>
  <style>
  <!--

    #calendar {
        width; 100%
    }

    #calendar table {
        margin: 0 auto;
        width: 350px;
    }

    #calendar th {
        background-color: pink;
        text-align : center;
        width:  50px;
        height: 50px;
        cursor:pointer;
    }

    #calendar td {
        background-color: #fffacd;
        text-align : center;
        width:  50px;
        height: 50px;
        cursor:pointer;
    }

    #calendar .current {
        background-color: #cccccc;
    }

    #calendar .sat {
        color: #0000ff;
    }

    #calendar .sun {
        color: #ff0000;
    }

  //-->
  </style>
  </head>
  <body>
    <div id="contents">
      <div id="calendar">
<?php

    $current_date = urldecode($_GET['current_date']);

//echo '$current_date : ';
//echo  $current_date;
//echo '<br>';
//echo '<br>';

    $current_year = date('Y', strtotime($current_date));

//echo '$current_year : ';
//echo  $current_year;
//echo '<br>';
//echo '<br>';

    $current_month = date('m', strtotime($current_date));

//echo '$current_month : ';
//echo  $current_month;
//echo '<br>';
//echo '<br>';

    $current_day = date('j', strtotime($current_date));

//echo '$current_day : ';
//echo  $current_day;
//echo '<br>';
//echo '<br>';

    $last_day = date("t", mktime(0, 0, 0, $current_month, 1, $current_year));

//echo '$last_day : ';
//echo  $last_day;
//echo '<br>';
//echo '<br>';

    ## 0 => 'Sun' 6 => 'Sat'
    $first_day_week = date('w', mktime(0, 0, 0, $current_month, 1, $current_year));

//echo '$first_day_week : ';
//echo  $first_day_week;
//echo '<br>';
//echo '<br>';

    ## 0 => 'Sun' 6 => 'Sat'
    $last_day_week = date('w', mktime(0, 0, 0, $current_month, $last_day, $current_year));

//echo '$last_day_week : ';
//echo  $last_day_week;
//echo '<br>';
//echo '<br>';

    $prev_year = date('Y/m/d', mktime(0, 0, 0, $current_month, 1, ($current_year - 1)));

//echo '$prev_year : ';
//echo  $prev_year;
//echo '<br>';
//echo '<br>';

    $prev_month = date('Y/m/d', mktime(0, 0, 0, ($current_month - 1), 1, $current_year));

//echo '$prev_month : ';
//echo  $prev_month;
//echo '<br>';
//echo '<br>';

    $next_year = date('Y/m/d', mktime(0, 0, 0, $current_month, 1, ($current_year + 1)));

//echo '$next_year : ';
//echo  $next_year;
//echo '<br>';
//echo '<br>';

    $next_month = date('Y/m/d', mktime(0, 0, 0, ($current_month + 1), 1, $current_year));

//echo '$next_month : ';
//echo  $next_month;
//echo '<br>';
//echo '<br>';

    echo '        <table>' . PHP_EOL;

    echo '          <tr>' . PHP_EOL;
    ## 日付切り替え部分
    echo '            <th date="' . $prev_year . '">&lt;&lt;</th>' . PHP_EOL;
    echo '            <th date="' . $prev_month . '">&lt;</th>' . PHP_EOL;
    echo '            <th colspan="3">' . $current_year . '年' . $current_month . '月' . '</th>' . PHP_EOL;
    echo '            <th date="' . $next_month . '">&gt;</th>' . PHP_EOL;
    echo '            <th date="' . $next_year . '">&gt;&gt;</th>' . PHP_EOL;
    echo '          </tr>' . PHP_EOL;

    echo '          <tr>' . PHP_EOL;
    ## 日付がない部分(1日より前)
    for ($week=0; $week<$first_day_week; $week++) {
        echo '            <td></td>' . PHP_EOL;
    }
    ## 日付がある部分
    for ($day=1; $day<=$last_day; $day++) {
        $week = date('w', mktime(0, 0, 0, $current_month, $day, $current_year));
        switch ($week) {
            case 0 :
                $text = '<span class="sun">' . $day . '</span>';
                break;
            case 6 :
                $text = '<span class="sat">' . $day . '</span>';
                break;
            default :
                $text = $day;
                break;
        }
        if ($current_day == $day) {
            echo '            <td class="current" date="' . date('Y/m/d', mktime(0, 0, 0, $current_month, $day, $current_year)) .'">' . $text . '</td>' . PHP_EOL;
        } else {
            echo '            <td date="' . date('Y/m/d', mktime(0, 0, 0, $current_month, $day, $current_year)) .'">' . $text . '</td>' . PHP_EOL;
        }
        if (6 == $week) {
            echo '          </tr>' . PHP_EOL;
            echo '          <tr>' . PHP_EOL;
        }
    }
    ## 日付がない部分(月末日より後)
    for ($week=$last_day_week; $week<6; $week++) {
        echo '            <td></td>' . PHP_EOL;
    }
    echo '          </tr>' . PHP_EOL;
    echo '        </table>' . PHP_EOL;

?>
      </div>
    </div>
  </body>
</html>


2014年10月29日水曜日

子ウィンドウから親ウィンドウを操作する (jQuery)

親ウィンドウの指定
var opener_form = window.opener.$('#content #opener_form');


【親ウィンドウ】

<?php

    if (isset($_POST['test'])) {
       $test =  $_POST['test'];
    } else {
       $test =  "";
    }

?>
<!DOCTYPE html>
<html>
  <head>
  <meta charset="UTF-8">
  <title>opener</title>
  <link rel="stylesheet" href="css/common.css">
  <script src="/js/jquery.js"></script>
  <script>

      $(document).ready(function(){

          $('#content :input[name="show_window"]').bind('click', function(){
              window.open('window.php', 'window', 'width=400, height=300, menubar=no, toolbar=no, scrollbars=yes');
          });

      });

  </script>
  <style>

    div #content {

    }

  </style>
  </head>
  <body>
    <div id="content">
      <?php echo 'test : ' . $test; ?>
      <br>
      <form id="opener_form" method="POST" action="index.php">
        <input type="button" name="show_window" value="子ウィンドウを表示">
        <input type="submit" value="送信">
        <input type="hidden" name="test" value="submit!">
      </form>
    </div>
  </body>
</html>


【子ウィンドウ】

<!DOCTYPE html>
<html>
  <head>
  <meta charset="UTF-8">
  <title>window</title>
  <link rel="stylesheet" href="css/common.css">
  <script src="/js/jquery.js"></script>
  <script>

      $(document).ready(function(){

          $('#content :input[name="opener_submit"]').bind('click', function(){
             var opener_form = window.opener.$('#content #opener_form');
             opener_form.attr('action', 'http://www.yahoo.co.jp')
             opener_form.submit();
          });

      });

  </script>
  <style>

    div #content { 

    }

  </style>
  </head>
  <body>
    <div id="content">
      <input type="button" name="opener_submit" value="親ウィンドウを操作">
    </div>
  </body>
</html>


2013年8月1日木曜日

jQueryでテーブルに行を追加


簡単なものを作成しました。

<html>
  <head>
    <meta charset="utf-8">
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
    <script type="text/javascript">
      $(function () {

        $('#contents input[name="append"]').click(
          function () {
            var origin = $('#001 .origin');
            var clone = origin.clone();
            $('#001').append(clone.removeClass('origin'));
            $('#001 tr:last input[name="name03"]').click(function(){
              alert('データを追加します');
            }).attr({ 
              'value': '追加する'
            });
          }
        ); // end of $('#contents input[name="append"]').click(

        $('#contents input[name="remove"]').click(
          function () {
            $('#001 tr:last').remove();
          }
        ); // end of $('#contents input[name="remove"]').click(

      });
    </script>
    <style type="text/css">
      
      .origin {
        display : none;
      }
      
    </style>
  </head>
  <body>
    <div id="wrapper">
      <div id="contents">
        <input type="button" name="append" value="行追加">
        <input type="button" name="remove" value="行削除">
        <table border="1" id="001">
          <tr class="origin">
            <td><input type="text" name="name01" value=""></td>
            <td><input type="text" name="name02" value=""></td>
            <td><input type="button" name="name03" value="更新する"></td>
          </tr>
          <tr>
            <td><input type="text" name="name01" value="aaa"></td>
            <td><input type="text" name="name02" value="aaa"></td>
            <td><input type="button" name="name03" value="更新する"></td>
          </tr>
        </table>
      </div>
    </div>
  </body>
</html>

2013年6月7日金曜日

VBScriptでbase64やquoted-printableをデコードするスクリプト


'    Wscript.Echo MailDecode("44K544Oe44Kk44Or44OX44Oq44Kt44Ol44Ki", "UTF-8", "base64")
'    Wscript.Echo MailDecode("=1B$B0&$7$F$k=1B(B", "iso-2022-jp", "quoted-printable")

'---------------------------------------------------
'  MailDecode
   (
       SourceData : 変換元の文字列
       CharSet : 変換元の文字コード
       EncodeType  : 変換元のエンコードタイプ
   )
'---------------------------------------------------

  Function MailDecode(SourceData, CharSet, EncodeType)

    'Create CDO.Message object For the encoding.
    Dim Message: Set Message = CreateObject("CDO.Message")

    'Set the encoding
'   Message.BodyPart.ContentTransferEncoding = "quoted-printable"
'    Message.BodyPart.ContentTransferEncoding = "base64"
    Message.BodyPart.ContentTransferEncoding = EncodeType
   
    'Get the data stream To write source string data
    Dim Stream 'As ADODB.Stream
    Set Stream = Message.BodyPart.GetEncodedContentStream

    If VarType(SourceData) = vbString Then
      'Set charset To base windows charset
      Stream.charset = "windows-1250"
      'Write the VBScript string To the stream.
      Stream.WriteText SourceData
    Else
      'Set the type of the stream To adTypeBinary.
      Stream.Type = 1
     
      'Write the source binary data To the stream.
      Stream.Write SourceData
    End If
   
    'Store the data To the message BodyPart
    Stream.Flush
   
    'Get an encoded stream
    Set Stream = Message.BodyPart.GetDecodedContentStream
   
    'Set the type of the stream To adTypeBinary.
    Stream.CharSet = CharSet
   
    'You can use Read method To get a binary data.
    MailDecode = Stream.ReadText

  End Function