sidewalkcafe blog

日々是好日

スクリプト覚書

リターンキーからのForm送信チェック

<?php
//POSTされたら内容を表示
    $str = $_POST['text'];
    if ($str != null){
        $result = "あなたは、「{$str}」と書きました。";
    } else {
        $result = "なにか書いてください。";
    }
?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
        xml:lang="ja" lang="ja">
    <head>
        <meta http-equiv="Content-Type"
            content="text/html; charset=UTF-8" />
        <title>sample page</title>
		<script type="text/javascript" charset="UTF-8">
<!--
//リターンキーが押された時の送信チェック
function submitCheck(e){
    if (!e) var e = window.event;
 
    if(e.keyCode == 13){
        if(confirm('送信してもよろしいですか?')){
            input = e.srcElement? e.srcElement: e.target;
            var form = input.parentNode;
            while(form != null && form.nodeName != 'FORM'){
                form = form.parentNode;
            }
            if(form != null)
                form.submit();
        }
        else {
            return false;
        }
    }
}
-->
</script>

    </head>
    <body>
        <h1>Form Check!</h1>
        <div><?php echo $result; ?></div>
        <form method="post" action="<?php echo $PHP_SELF; ?>">
			<input type = "text" name = "text" onKeyPress="return submitCheck(event);">
            <input type = "submit" name = "button1" value = "送信">
        </form>
		<hr>

    </body>
</html>

ディレクトリ内のファイルを表示、ジャンプリスト

<?php
//POSTされたファイルにジャンプ
    if ($_POST != null && $_POST !=null){
        $url = $_POST['jump'];
        header("Location:{$url}");
    }
//ディレクトリ指定
$dir = ".";

//絶対パスを取得
$filedir = str_replace( "\\" , "/" , dirname(__FILE__) );

?>

<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
        xml:lang="ja" lang="ja">
    <head>
        <meta http-equiv="Content-Type"
            content="text/html; charset=UTF-8" />
        <title>List & Link PHP</title>
    </head>
    <body>
        <h1>List & Link PHP!</h1>
        <p><?php echo $filedir; ?></p>
        <hr>
        <form method="post" action="<?php echo $PHP_SELF; ?>">
        
            <select name="jump" size="10">
 
<?php
// 既知のディレクトリをオープンし、その内容を読み込み
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            echo "<option value=\"".$file."\">".$file."</option>\n";
        }
        closedir($dh);
    }
}
?>
            </select>
             <input type="submit" value="移動">
        </form>
        <hr>
    </body>
</html>