JavaScriptで日付設定を設定して頂き、サブミットを押すと値をPHPに渡す、という事をしたいのですが、JSからPHPに値の受け渡しというのは可能なのでしょうか?
また、それはどのようにすればいいのでしょうか?
以下が現在のコードです。
【reservecalender.php】
<head>
<script type="text/javascript">
//<![CDATA[
var nowYear;
var nowMonth;
var nowDate;
var nowLimitDay;
var nowTag1;
var nextTag1;
var selectMonth;
function onReady(){
var nowDate = new Date();
wkYear = nowDate.getYear();
//年
nowYear = (wkYear < 2000) ? wkYear+1900 : wkYear ;
//月
nowMonth = nowDate.getMonth()+1;
//日
nowDate = nowDate.getDate();
//今月の日数
nowLimitDay = new Date(nowYear, nowMonth, 0).getDate();
//日付用オプションタグ
nowTag1 = "";
nextTag1 = "";
//選択中の月
selectMonth = 0;
//プルダウン1日付、デフォ+3
pull1D = nowDate + 3;
//次の月のどこまで日付を選べるか
fitThirty = 30 - (nowLimitDay - pull1D)
//月のオプション
monthTag = '<option value=' + nowMonth + '>' + nowMonth + '月</option>';
if (nowMonth != 12) {
monthTag += '<option value=' + (nowMonth+1) + '>' + (nowMonth+1) + '月</option>';
}
//今月の日付オプション
for (i = pull1D; i <= nowLimitDay; i++) {
nowTag1 += '<option value="' + i + '">' + i + '日</option>';
}
//来月の日付オプション
for (i = 1; i <= fitThirty; i++) {
nextTag1 += '<option value="' + i + '">' + i + '日</option>';
}
document.getElementById("year1").innerHTML = nowYear + "年";
document.getElementById("year2").innerHTML = nowYear + "年";
document.getElementById("pull1M").innerHTML = monthTag;
document.getElementById("pull2M").innerHTML = monthTag;
document.getElementById("pull1D").innerHTML = nowTag1;
document.getElementById("pull2D").innerHTML = nowTag1;
}
function changeDay(){
selectObj = document.getElementById("pull1D");
options = document.getElementById("pull1D").options;
selectDay = options.item(selectObj.selectedIndex).value;
selectDay++;
selectDay--;
setTag = "";
if (selectMonth == 0) {
setTag = nowTag1;
} else {
setTag = nextTag1;
}
wkPos = setTag.indexOf('<option value="' + (selectDay+1) + '">' + (selectDay+1) + '日</option>', 0);
chageDayTag = '<option value="' + selectDay + '" selected>' + selectDay + '日</option>' + setTag.substr(wkPos);
document.getElementById("pull2D").innerHTML = chageDayTag;
}
function changeMonth1(){
setTag = "";
if (selectMonth == 0) {
setTag = nextTag1;
changeMonth(nowMonth+1);
selectMonth = 1;
} else {
setTag = nowTag1;
changeMonth(nowMonth);
selectMonth = 0;
}
document.getElementById("pull1D").innerHTML = setTag;
document.getElementById("pull2D").innerHTML = setTag;
}
function changeMonth2(){
if (selectMonth == 0) {
selectObj = document.getElementById("pull2D");
options = document.getElementById("pull2D").options;
selMonth2 = options.item(selectObj.selectedIndex).value;
if (nowMonth == selMonth2) {
document.getElementById("pull2D").innerHTML = nextTag1;
} else {
document.getElementById("pull2D").innerHTML = nowTag1;
}
} else {
changeMonth(nowMonth+1);
}
}
function changeMonth(selMonth){
optObj = document.getElementById("pull2M").getElementsByTagName('option');
for (i = 0; i < optObj.length; i++) {
if (optObj[i].value == selMonth) {
optObj[i].selected = true;
break;
}
}
}
//]]>
</script>
</head>
<body onload="onReady()">
<div id="box">
<form name="reservedateform" action="reserveselect.php" method="post">
<div id="year1"></div> / <select id="pull1M" onchange="changeMonth1()"></select> / <select id="pull1D" onchange="changeDay()"></select>
<div id="year2"></div> / <select id="pull2M" onchange="changeMonth2()"></select> / <select id="pull2D"></select>
<input type="hidden" name="checkin" value="1
つ目の年月日" />
<input type="hidden" name="checkout" value="2
つ目の年月日" />
<input type="submit" value="go" />
</form>
</body>
【reserveselect.php】
if ($_SERVER["REQUEST_METHOD"] == "POST"){
$checkin = htmlspecialchars($_POST["checkin"], ENT_QUOTES);
$checkout = htmlspecialchars($_POST["checkout"], ENT_QUOTES);
}
また、年月日の値を"2013-03-13"のように変換して格納するにはどうすれば良いでしょうか??
質問ばかりですみません…。
どなたかご回答頂けるとありがたいです。
No.2ベストアンサー
- 回答日時:
なんか最近似たようなの見た気がするなと思ったら、なんだ同じ人か。
新しい質問出す前に、前の質問片づけなさいな。
まぁ、それは置いといて。
> また、年月日の値を"2013-03-13"のように変換して格納するにはどうすれば良いでしょうか??
普通に結合すればいいと思いますよ。
文字列の結合は 「.」(ピリオド)を使う。他にも方法あるけど簡単にやると こうなる。
年月日が $year $month $day だとしたら、↓これで。
$date = sprintf("%02d" , $year) . "-" . sprintf("%02d" , $month) . "-" . sprintf("%02d" , $day);
あと、$_POSTを いきなり htmlspecialchars すんのは最近の流行りなんですか?
もしくは 推奨されるようにでもなってるんですかね?
ここんとこPHPまともに触ってなくて疎いもんでよく分からんのですけど。
あと、初心者に出す課題じゃねーな。って思いますよ。前回の質問のアレは。
No.1
- 回答日時:
意味がよくわからないのですが渡してどうするのでしょうか?
単に渡すならサブミットすれば渡りますよね?
非同期処理で渡したあと何らかの戻り値がほしいのであればajaxで処理します
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
似たような質問が見つかりました
- JavaScript 以前の質問だと、どの条件でも配列が表示されてしまいます。 1 2022/07/09 11:40
- JavaScript コードレビューをお願いします。 1 2022/07/16 05:38
- JavaScript console.logがどうしても2つ機能しないのでアドバイスをくださいお願いします 2 2022/07/07 22:13
- JavaScript プログラムがうまく動きませんレビューお願いします 1 2022/07/10 05:08
- JavaScript 1日1回引けるJavaScriptおみくじについて 1 2022/12/12 22:28
- JavaScript セレクトボックスで配列を呼び出したい。 1 2022/07/08 20:14
- JavaScript セレクトボックスを2つ選択してメッセージなどを表示するには。~運賃検索プログラムを完成させたい~ 1 2022/07/22 11:10
- JavaScript switch文のswitch(n)の部分を複数の値にするか、if文に変えてほしいです。 1 2022/07/27 17:18
- JavaScript sessionStorageを調べています。 1 2023/06/20 12:41
- JavaScript JavascriptからSQLへ繋ぎ方が分からない 3 2022/07/07 00:27
関連するカテゴリからQ&Aを探す
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
プルダウンとCSVの連動
-
php セレクトボックス 値取得
-
<select>文、foreachと初期値設定
-
2つのプルダウンメニュから受け...
-
複数 selectフォームから今回on...
-
input type="hidden"で送る値を...
-
mysql データ登録
-
頭文字→大学→学部を選択する入...
-
MySQLのレコードを検索、...
-
submitして確認画面へ
-
複数のセレクトボックスを1つに...
-
複数のセレクトボックスで
-
連動させたいセレクトボックス...
-
phpで重複チェック
-
フォームでのリセットを使う方法
-
PHP ボタンが押されたら処理を...
-
フォーム送信後の更新ボタンで...
-
ラジオボタンをsessionで使いたい
-
HTMLで前の画面に戻る時、入力...
-
検索時の選択内容を保持する方法
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
プルダウンとCSVの連動
-
「"」(ダブルクォーテーション)...
-
ラジオボタンの値を受け取ってP...
-
<select>文、foreachと初期値設定
-
phpでのセレクトボックスの値を...
-
php セレクトボックス 値取得
-
複数のセレクトボックスを1つに...
-
複数 selectフォームから今回on...
-
2つのプルダウンメニュから受け...
-
phpでのselect値の取得・表記に...
-
foreachで次の行の値を取得して...
-
ウェブ上で選択した複数行の内...
-
CodeIgniter/set_value()
-
ループ処理
-
連動させたいセレクトボックス...
-
現在時刻を反映させた時刻のプ...
-
プルダウンリスト(コンボボッ...
-
PHP で画面の一部だけを変えたい。
-
phpでカレンダー作成
-
プルダウンで選択された値を保持
おすすめ情報