コメント |
http://winofsql.jp/php/basic/form/entry.php
@DIV
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=1280,initial-scale=1.0">
<style type="text/css">
body {
line-height:150%;
background-color: pink;
}
</style>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
function checkForm() {
// ***********************************************
// DOM で書いた場合のサンプル
// ***********************************************
// var sei = document.getElementsByName("sei")[0].value;
// var sei = document.getElementById("sei").value;
// var mei = document.getElementById("mei").value;
// ***********************************************
// jQuery で書いた場合のサンプル( 値の取り出し )
// ***********************************************
var sei = $("#sei").val();
var mei = $("#mei").val();
// ***********************************************
// jQuery で書いた場合のサンプル( 値のセット )
// ***********************************************
$("#jyusyo").val("大阪府");
// ***********************************************
// デベロッパーツールへの出力( デバッグ用 )
// ***********************************************
console.log( "姓 : " + sei );
console.log( "名 : " + mei );
// jQuery で一括表示
$( $( "form" ).serializeArray() ).each(function(){
console.log( this );
});
// ***********************************************
// 未入力チェック
// ***********************************************
if ( sei.trim() == "" ) {
alert("氏名を入力して下さい");
$("#sei").focus();
return false;
}
return true;
}
</script>
</head>
<body>
<form method="POST"
onsubmit="return checkForm();"
action="result.php"
target="result">氏名
<input type="text"
name="sei"
id="sei"
style="width:100px">
<input type="text"
name="mei"
id="mei"
value="改行
しています"
style="width:100px">
<br>住所
<input type="text"
name="jyusyo"
id="jyusyo"
style="width:240px">
<br>性別
<select name="seibetu"
id="seibetu">
<option value="man">男</option>
<option value="woman">女</option>
</select>
<br>
<label for="in_check1">スマホ</label>
<input type="checkbox"
name="smh"
id="in_check1"
value="smh">
<label for="in_check2">ガラケー</label>
<input type="checkbox"
name="gk"
id="in_check2"
value="gk">
<br>
<label for="in_radio1">有り</label>
<input type="radio"
name="in_radio"
id="in_radio1"
value="1"
checked>
<label for="in_radio2">無し</label>
<input type="radio"
name="in_radio"
id="in_radio2"
value="0">
<br>備考
<br>
<textarea name="bikou"
id="bikou"
style="width:320px;height:100px;"></textarea>
<br>
<input type="submit"
name="send"
value="送信">
</form>
<iframe
src="about:blank"
name="result"
frameborder="1"
scrolling="yes"
width="600"
height="400"
style='border:solid 1px #000;margin-top:10px;'
></iframe>
</body>
</html>
@END
[[result.php]]
@DIV
<?php
header("Content-Type: text/html; charset=UTF-8");
// ***********************************************
// デバッグ表示
// ***********************************************
print "<pre>";
print_r( $_POST );
print "</pre>";
print "<hr>";
// ***********************************************
// ファイルに書き込む為の準備作業
// ***********************************************
$abc = $_POST["sei"] . "\n";
$abc .= $_POST["mei"] . "\n";
$abc .= $_POST["jyusyo"] . "\n";
$abc .= $_POST["seibetu"] . "\n";
$abc .= $_POST["smh"] . "\n";
$abc .= $_POST["gk"] . "\n";
// ***********************************************
// 実際の改行コードは変換して出力します
// ***********************************************
$work = str_replace("\r\n","\\r\\n",$_POST["bikou"]);
$work = str_replace("\n","\\n",$work);
$abc .= $work . "\n";
// ***********************************************
// ファイルへ書き込み
// ***********************************************
file_put_contents(
"data.txt",
$abc
);
// ***********************************************
// 表示( セキュリティ対策していません )
// ***********************************************
print $abc;
print "<hr>";
// ***********************************************
// HTML に埋め込む為の準備作業
// ( セキュリティ対策 )
// ***********************************************
$abc = str_replace("\"",""",$abc);
$abc = str_replace("<","<",$abc);
$abc = str_replace(">",">",$abc);
?>
<input
type="text"
name="adr"
value="<?= $abc ?>"
style='width:320px;'>
<br>
<textarea
style="width:320px;height:100px;"><?= $abc ?></textarea>
<br><br>
<a href="entry.php">戻る</a>
@END |