I'm working in a ColdFusion 11 environment. 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) :
<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. 0 = no, the database doesn't already have it. The problem is when I click submit the result is 1 submit later.
example :
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:
<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. Secondly it will not accept ## symbols in alert.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.