プロが教える店舗&オフィスのセキュリティ対策術

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"のように変換して格納するにはどうすれば良いでしょうか??
質問ばかりですみません…。
どなたかご回答頂けるとありがたいです。

A 回答 (2件)

なんか最近似たようなの見た気がするなと思ったら、なんだ同じ人か。


新しい質問出す前に、前の質問片づけなさいな。
まぁ、それは置いといて。

> また、年月日の値を"2013-03-13"のように変換して格納するにはどうすれば良いでしょうか??

普通に結合すればいいと思いますよ。
文字列の結合は 「.」(ピリオド)を使う。他にも方法あるけど簡単にやると こうなる。

年月日が $year $month $day だとしたら、↓これで。

$date = sprintf("%02d" , $year) . "-" . sprintf("%02d" , $month) . "-" . sprintf("%02d" , $day);

あと、$_POSTを いきなり htmlspecialchars すんのは最近の流行りなんですか?
もしくは 推奨されるようにでもなってるんですかね?
ここんとこPHPまともに触ってなくて疎いもんでよく分からんのですけど。

あと、初心者に出す課題じゃねーな。って思いますよ。前回の質問のアレは。
    • good
    • 0

意味がよくわからないのですが渡してどうするのでしょうか?



単に渡すならサブミットすれば渡りますよね?
非同期処理で渡したあと何らかの戻り値がほしいのであればajaxで処理します
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!