简体   繁体   English

提交功能不起作用

[英]Onsubmit function not working

My onsubmit function is not working. 我的onsubmit函数不起作用。

My PHP file has two forms. 我的PHP文件有两种形式。 Both form tags have an onsubmit attribute that links to the same javascript file. 这两个表单标签都有一个onsubmit属性,该属性链接到相同的javascript文件。 The first form's onsubmit is working. 第一个表单的onsubmit正在工作。 The function being called by the second form has been simplified to this: 第二种形式调用的函数已简化为:

function UserDataCheck()
{
   return false;
}

The form tag is 表单标签是

form name='user_form' method='post' onsubmit='return UserDataCheck();'

This tag is enclosed within <> and double quotes and echo-ed out in a PHP file. 该标签包含在<>和双引号中,并在PHP文件中回显。

Please help. 请帮忙。 This is driving me crazy! 这真让我抓狂! Earlier I was doing some data checking in the UserDataCheck() but I stripped all of that away to see if I could even use onsubmit to stop the user from submitting! 早些时候,我在UserDataCheck()中进行了一些数据检查,但我剥离了所有这些内容,以查看是否可以使用onsubmit阻止用户提交! NO SUCCESS. 没有成功。

Here's more code! 这是更多代码!

 echo "<html>"; 
    echo "<head>"; 
    echo "<title>Survey Form</title>"; 
    echo "<link link rel='stylesheet' href='examples.css' type='text/css'>"; 
    echo "</head>";
echo "<script src='validation.js'></script>"; 
echo "<body topmargin='20', leftmargin='20', rightmargin='20', style='background-color:beige; font-family:calibri;'>"; 
echo "<img src='CSE Global.jpg' width='250' height='70' align='right' />";
//--------------------------------------------------------------------- 
// Firstly, we determine who is using this system 
// Check $_GET['dex'] for a value, if provided, then it's the user 
// if not, then it's the administrator (see my notes below the code for 
// more info) 
//--------------------------------------------------------------------- 

"<input type='text' name='dex' length='50'>";
"<input type='boolean' name='sub' length='1'>";
if ($_GET['dex']=="") {  
    //--------------------------------------------------------------------- 
    // $_GET['dex'] is empty so we can presume this is the Admin 
    //--------------------------------------------------------------------- 
    echo "<p style='font-family:cambria;font-size:35px;'> WELCOME ADMIN!</p>"; 


    //--------------------------------------------------------------------- 
    // We now check to see if the form has been submitted or not ... 
    // To do this, we check if the variable 'sub' is in the $_POST array 
    //--------------------------------------------------------------------- 
    if ($_POST['sub']!=1) { 
        //--------------------------------------------------------------------- 
        // The form has not been submitted already so present the "admin form" 
        //---------------------------------------------------------------------


// check that they entered an amount tested, an amount passed,
// and that they didn't pass units than they more than tested



        echo "<form name='admin_form' method='post' onsubmit='if(!AdminDataCheck()) return      false;'>"; 
        echo "<p><label>Scope of Service <label style='color:red'><strong>*</strong></label>    <br/></label> <input type='text' name='scope' length='50' id='ScopeofService' onblur='checkScopeofService()'/><label id='labelScopeofService'></label></p>"; 

This is followed by more labels and text fields. 其次是更多标签和文本字段。

And then the form tag is closed! 然后关闭表单标签!

echo "<input type='hidden' name='dex' value={$dex}>"; 
            echo "<input type='hidden' name='sub' value=1>"; 
            echo "<input type='SUBMIT' style='background-color:white' name='submit' value='SUBMIT'>"; 
            echo "</form>"; 

Here's the function in validation.js 这是validation.js中的函数

function AdminDataCheck()
{
var SoS = document.getElementById('ScopeofService').value;
var PNumber = document.getElementById('ProjectNumber').value;
var numericExp = /^[0-9]+$/;
var alphaExp = /^[A-Za-z ]+$/; 
var Email = document.getElementById('Email').value;
var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\-]+\.[a-zA-z0-9]{2,4}$/;

if (!(SoS.match(alphaExp)))
{   
alert("Invalid Scope");
return false;
}
else if(!(PNumber.match(numericExp)))
{
alert("Invalid Project Number.");
return false;
}
else if(!(Email.match(emailExp)))
{
alert("Invalid Email.");
return false;
}
else
{
return true;
}
}

Let me EMPHASIZE that this was working a few minutes ago and I made no changes, just restarted the browser and the server! 让我强调这是在几分钟前起作用的,我没有进行任何更改,只是重新启动了浏览器和服务器! And it stopped working. 它停止工作了。 This is really frustrating! 这真令人沮丧!

尝试这个:

onsubmit='if(!UserDataCheck()) return false;'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM