PHP和HTML表单,javascript onsubmit未执行

I have a HTML form asking for user input. A javascript function is meant to validate the form and should be triggered (using onsubmit) BEFORE executing the PHP code. (The js file is referenced in the header <script type="text/javascript" src="js/javascript.js"></script>)

HOWEVER the javascript is never executed and instead the PHP code executes and (correctly) includes the new file.

How can i make the javascript execute as required?

HTML file:

<form name="details" action="" method="post" 
     onSubmit="return ValidateForm(details);">
...
<input type="submit" name="post-this-form" value="Next"/>
</form>

PHP file:

if (isset($_POST['post-this-form']) and 
     $_POST['post-this-form'] == 'Next')
{
...
some php 
...
include 'shipping-details.html.php';
exit();
}

EDIT

here is the javascript as requested. It has been tested and worked without any PHP involved. By passing details (the name of the form) i'm making all the form fields accessible.

function ValidateForm(details)
        {
        var firstName=document.forms.details.firstName.value;
        var lastName=document.forms.details.lastName.value;
        var streetAddress=document.forms.details.streetAddress.value;
        var town=document.forms.details.town.value;
        var city=document.forms.details.city.value;
        var zip=document.forms.details.zip.value;
        var country=document.forms.details.country.value;       
        var creditCard=document.forms.details.creditcard.value;                 

                ...

        //Checks firstName
        if ((firstName=="")||(!firstName.match(alphabetExpression)))
            {
            alert ("Invalid first name, please re-enter");
            return false;
            }
                 ...

        //Checks creditCard
        if ((creditCard=="")||(!creditCard.match(numericExpression))||(creditCardLength!=16))
            {
            alert ("Invalid credit card entry, please re-enter");
            return false;
            }       
        }// end function

EDIT 2

i added alert ("Hi"); to the start of the javascript and the alert never shows up which leads me to think that the function isn't executed at all.

EDIT 3

My initial suspicion that this problem could be due to PHP was wrong. As Jason mentioned in his comments the problem was in the javascript itself. It is a bit strange thought because the same javascript code worked "on its own" without PHP and on my local machine. So many factors to consider...thanks All for taking the time to have a look at my problem!

dsizd368332
dsizd368332 进一步的故障排除似乎指向我在javascript中的正则表达式(未发布)。看起来错误往往是他们不期望的地方:)非常感谢提示使用错误控制台!如果您发表评论,我很乐意接受它作为答案
8 年多之前 回复
duanbianweng5353
duanbianweng5353 不,只是关于如何编写代码的问题,使用exit()来打破视图显示逻辑通常意味着不正确地关注问题,因为它并不直接与我将其作为评论发布的问题相关,因为这是评论的用途:)
8 年多之前 回复
doz15449
doz15449 谢谢,我越来越近了...我按照建议进行检查,并说“ValidateForm未定义”...但是它包含在标头标签中,所以它必须是由于我实现的PHP'包含'机制?不知道如何解决这个问题:(
8 年多之前 回复
dqpwdai095465
dqpwdai095465 我打赌有一个javascript错误。打开错误控制台。在Firefox中使用ctrl+shift+j。
8 年多之前 回复
duagfgfn1981
duagfgfn1981 我试过但没有运气
8 年多之前 回复
duan198811
duan198811 出口();是我实现的PHP逻辑的一部分。这会干扰验证表单功能吗?
8 年多之前 回复
dropbox1111
dropbox1111 在旁注,你为什么退出();而不是回归?是ValidateForm(详情);归还假?
8 年多之前 回复
dongwo2222
dongwo2222 -这可能是问题,但却无法看到js
8 年多之前 回复
dqr3883
dqr3883 你打算传递给你的Javascript函数究竟是什么?因为现在,你告诉它你传递一个名为details的变量,而不是变量字符串'details',我猜这是你想要的。
8 年多之前 回复
dragam0217
dragam0217 尝试写onSubmit=“returnValidateForm(this);”而不是onSubmit=“returnValidateForm(details);”
8 年多之前 回复
douting0585
douting0585 你能发贴你的javascript代码吗?
8 年多之前 回复

2个回答



评论转换为答案</ strong> </ em> </ p>

“我打赌有一个javascript错误。在Firefox中打开错误控制台.ctrl + shift + j。”</ p>

很高兴有帮助。</ p>
</ div>

展开原文

原文

Comment converted to answer

"I bet there is a javascript error. Open error console. ctrl+shift+j in firefox."

Glad that helped.



以下是您必须了解的有关正在尝试构建的函数的一些内容</ p>

 <  code> function ValidateForm(){
this; //这包含表单名称,不需要任何变量声明

返回true; //或false这对于继续执行非常重要
// false停止提交
}
</ code> </ pre>

当您解决这两个问题时,它应该 按你的意愿工作。</ p>
</ div>

展开原文

原文

Here are few things you must know the about the functions you are trying to build

function ValidateForm() {
     this; // this contains the form name no need for any variable declaration

     return true; // or false This is very important to continue the execution
                  // false stops the submittion
}

When you fix both of these issues, it should work as you want.

dougan4884
dougan4884 它确实以某种方式回答了他的问题,但是在你编辑之前你甚至没有解释他必须做什么......通过对OP的评论你可以看到我同意你的解决方案,但没有实际答案 他当时的问题
8 年多之前 回复
dongqi7631
dongqi7631 我很抱歉你这么认为。
8 年多之前 回复
doutou7961
doutou7961 这并没有真正回答这个问题......
8 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐