私はこれを自分で理解しようとかなりの時間を費やしましたが、jqueryとajaxを使用するのは初めてで、おそらく単純なものです...
送信したい見積依頼フォームがあり、送信前に詳細を検証したいと考えています。これの一部は単純な JavaScript で実行でき、その部分は正常に機能しています。また、mysql データベースからいくつかの値を取得し、問題が発生している場所を検証する必要があります。
私のHTMLフォームには、この行があります
<form id="quoteform" onsubmit=" return validateHire()" method="post" action="quote-results.php">
フォームを含むページに含まれる別のファイルに含まれる検証スクリプトを呼び出します。
validateHire スクリプトは次のとおりです。
function validateHire(){
alert("Hire Validation Script Being Called!");
// set the colllection date and time
var collectionDate = new Date ($("#collectiondate").datepicker('getDate'));
var collectionHour = ($("#collectionTime").val()).substr(0,2);
collectionDate.setHours(collectionHour);
var collectionMinute = ($("#collectionTime").val()).substr(3,4);
collectionDate.setMinutes(collectionMinute);
// set the return date and time
var returnDate = new Date ($("#returndate").datepicker('getDate'));
var returnHour = ($("#returnTime").val()).substr(0,2);
returnDate.setHours(returnHour);
var returnMinute = ($("#returnTime").val()).substr(3,4);
returnDate.setMinutes(returnMinute);
// calculate the length of hire and add additional day if necessary
var timeDiff = returnHour - collectionHour
var totaldays = Math.floor(((returnDate - collectionDate)/1000)/60/60/24);
if(totaldays <1 && totaldays >-1){
totaldays=1;
}
// if(timeDiff > 2){
totaldays = totaldays + 1;
alert(totaldays)
}
// get the collection and return day to check if it is a weekend hire
var collectionDay = collectionDate.getDay();
var returnDay = returnDate.getDay();
// validate if the hire is a friday or saturday it meets minimum length
if(collectionDay == 5 && totaldays <3){
$("#errorbox").slideDown('fast');
$("#errormessage").html("Hires Starting on a Friday Must be a Minimum of 3 Days");
return false;
}
if(collectionDay == 6 && totaldays <2){
$("#errorbox").slideDown('fast');
$("#errormessage").html("Hires Starting on a Saturday Must be a Minimum of 2 Days");
return false;
}
checkServerSide();
function checkServerSide()
{
alert("now checking server side!");
$.post("sproc-test.php",{q: totaldays, cdt: collectionDate, cTime: collectionTime, cLoc: CollectionLocation, cDay: collectionDay, cDas: collectionDateAsString, rDate: returnDate, rTime: returnTime, rLoc: returnLocation, rDay: returnDay, rdas: returnDateAsString},
function(data){
var minHire = data.minhire;
alert(minHire);
$('#testvalue').val(minHire);
}, "json");
alert("This is the end of the check server side function");
}
if($('#testvalue').val() == 1){
$("#errorbox").slideDown('fast');
$("#errormessage").html("The Hire is not long enough to meet the requried minimum!");
return false;
}
}
最初の部分は正常に機能し、ユーザーに正しいエラー メッセージを表示し、フォームは送信されませんが、checkServerSide 関数に関しては問題があります。
php スクリプトは、投稿後にアクセスしようとする配列を返し、値を取得して非表示のフォーム フィールドに設定し、それに対して検証できるようにしますが、Firebug によると、ページに実際に投稿されているものは何もありません。私もフォームを送信しようとしているので、 $post 構文が間違っているのではないかと思っていますか? また、 $post 呼び出しの前のアラートは機能しますが、後のアラートでも、ここで何かを台無しにしたのではないかと思いませんか? ただし、Firefox のエラー コンソールを見ると、エラーは報告されていません。
私が達成しようとしていることを十分に説明したことを願っていますが、十分に明確ではない場合はお知らせください。また、私が言ったように、私はこれに非常に慣れていないので、私が間違っていることのわかりやすい説明と、これを達成するためのより良い方法があるかどうかを本当に感謝しています.
ご協力いただきありがとうございます。:)
敬具
マイク