1

我花了很多时间试图自己解决这个问题,但对使用 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 错误控制台中查看时,没有报告错误。

我希望我已经充分解释了我想要实现的目标,但我还不够清楚,请告诉我。另外,正如我所提到的,我对此很陌生,所以我非常感谢一个易于理解的解释我做错了什么以及是否有更好的方法来实现这一点。

非常感谢您的帮助!:)

亲切的问候

麦克风

4

0 回答 0