Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 结合客户端值和Ajax验证表单数据_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript 结合客户端值和Ajax验证表单数据

Javascript 结合客户端值和Ajax验证表单数据,javascript,jquery,ajax,Javascript,Jquery,Ajax,我自己花了很多时间试图弄明白这一点,但我不熟悉使用jquery和ajax,这可能很简单 我有一个报价申请表,我想提交,我想验证提交前的细节。其中一些可以在简单的javascript中完成,这部分工作正常。我还需要从mysql数据库中获取一些值,并验证我遇到问题的地方 在我的HTML表单上,我有这一行 <form id="quoteform" onsubmit=" return validateHire()" method="post" action="quote-results.php"&

我自己花了很多时间试图弄明白这一点,但我不熟悉使用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错误控制台中查看时,没有报告错误

我希望我已经充分解释了我试图实现的目标,但我还不够清楚,请让我知道。此外,正如我提到的,我对这一点非常陌生,因此我非常希望能对我做错的事情给出一个简单易懂的解释,如果有更好的方法来实现这一点的话

非常感谢您的帮助!:

问候


Mike

很抱歉,错过了html中包含表单验证调用的那一行