简体   繁体   English

与服务器端和客户端验证冲突

[英]Conflict with server-side and client-side validation

I'm working in a ColdFusion 11 environment. 我正在ColdFusion 11环境中工作。 I want to make a form, that when submitted, validates whether the value I'm entering is already in the database. 我想做一个表单,提交后验证我输入的值是否已经存在于数据库中。 I think I'm mixing up server-side and client-side and that is the cause of the problem. 我认为我正在混淆服务器端和客户端,这就是问题的原因。

query : 查询:

<cfquery name="FindIdMailExist" datasource="#application.zips_database#" username="#application.zips_username#" password="#application.zips_password#">
    SELECT *
    FROM users
    WHERE user_name = '#userName#'
</cfquery>

My javascript (client-side) + some CF code (server-side) : 我的javascript(客户端)+一些CF代码(服务器端):

   <script>
    function CheckSub()
    {
        alert("submited");
        return true;
    }
    function checkName(formObject, formField, fieldValue)
    {
        <cfoutput>
        alert('#FindIdMailExist.RecordCount#');
        </cfoutput>

        return true;
    }
  <script>

My form : 我的表格:

<cfform  onsubmit="CheckSub()" name="form">
    <br>
<table>
  <tr><td width="120" align="right">Name:</td><td><cfinput name="userName"type="text" onvalidate="checkName" message="pas  email"></td></tr>
  <tr><td><cfinput name="Submit" type="submit" value="Register"></td></tr>

</table>

</cfform>

So everything is working well expect for one thing. 因此,一切都很好,期望做一件事。 If you understand the code, an alert is displayed with the result of 1 or 0. 1 = yes the database already contains this name. 如果您理解该代码,则会显示警告,结果为1或0。1 =是,数据库已经包含此名称。 0 = no, the database doesn't already have it. 0 =否,数据库尚未安装它。 The problem is when I click submit the result is 1 submit later. 问题是,当我单击“提交”时,结果是“稍后提交”。

example : 例如:

  • Enter an already existing name and click submit : result = 0 (wrong one) 输入一个已经存在的名称,然后单击Submit:result = 0(错误的一个)
  • Enter nothing : result 1 (the result is always the one I submitted before) 不输入任何内容:结果1(结果始终是我之前提交的结果)

So maybe it impossible to validate server side before submitting the form? 因此,可能无法在提交表单之前验证服务器端吗? I want to make sure everything is good before sending my form to add the data to the database. 在发送表单以将数据添加到数据库之前,我想确保一切都很好。 I want to do this server-side for the safety. 为了安全起见,我想在服务器端进行此操作。

Can anyone suggest a better way? 谁能提出更好的方法?

Thank you for your help! 谢谢您的帮助!

Try with the below code and function: Keep the FindIdMailExist.RecordCount in a hidden field: 尝试使用以下代码和功能:将FindIdMailExist.RecordCount保留在隐藏字段中:

<cfoutput>
   <input type="hidden" name="hdnFindIdMailExist" 
        id="hdnFindIdMailExist" value="#FindIdMailExist.RecordCount#">
</cfoutput>

Now the above field will be in the form when clicking on submit. 现在,单击“提交”时,以上字段将显示在表单中。 Try to modify the function in below way 尝试通过以下方式修改功能

function checkName(formObject, formField, fieldValue)
    {
        var testid = document.getElementById('hdnFindIdMailExist').val();
        alert(testid);
        return true;
    }

Took a variable and assigned the variable with the form element value and gave an alert. 接收一个变量,并为该变量分配表单元素值,并发出警报。 It is not a good practice to keep <cfoutput> in the javascript which may not sometimes access. <cfoutput>保留在有时可能无法访问的javascript中不是一个好习惯。 Secondly it will not accept ## symbols in alert. 其次,它将不接受警报中的##符号。

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

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