holsy Posted December 3, 2011 Share Posted December 3, 2011 Hi All, I am unable to do a check for duplicate records before the form is submitted to Paypal. Here is the form: <form name="_xclick" action="https://www.paypal.com/cgi-bin/webscr" method="post" onSubmit="return validate(this);"><input name="txtUserName" id="txtUserName" type="Text" tabindex="1" /><input name="txtPassword1" type="password" tabindex="2" /><input name="txtPassword2" type="password" tabindex="3" /><input name="txtEmail" type="Text" tabindex="4" /></form> I am doing javascript client side validation with this block of code: //Form Validationfunction validate(form) {var e = form.elements, m = '';if(!e['txtUserName'].value) {m += 'Username is required<br/>';}if(!e['txtPassword1'].value) {m += 'Password is required<br/>';}if(!e['txtPassword2'].value) {m += 'Repeat Password is required<br/>';}if(e['txtPassword1'].value != e['txtPassword2'].value) {m += 'Your passwords does not match<br/>';}if(!/.+@[^.]+(\.[^.]+)+/.test(e['txtEmail'].value)) {m += 'Valid e-mail address is required<br/>';}var btn = valButton(form.radiobuttons);if (btn == null) {m += 'Please choose a plan<br/>';}if(!e['terms-conditions'].checked ) {m += 'Must agree to terms and conditions<br/>';}//if (recaptcha_confirm == null) {m += 'reCaptcha Probs<br/>';}//if (reCaptchaResponse.isValid()) {out.print("Answer was entered correctly!");} else {m += 'reCaptcha Wrong<br/>';}if(m) {//alert('The following error(s) occurred:\n\n' + m);message_box.show_message('Please Complete the Following:\n\n', m);return false;}return true;}function valButton(btn) { var cnt = -1; for (var i=btn.length-1; i > -1; i--) { if (btn[i].checked) {cnt = i; i = -1;} } if (cnt > -1) return btn[cnt].value; else return null;} The javascript form validation works. The problem is now I want to check for duplicate usernames using asp, then continue with the form submit to Paypal if ther are no duplicates. I am unable to catch the submit action with: IF request.Form.Count > 0 THENORIf Request.ServerVariables("HTTP_METHOD") = "POST" Then I realize that the onSubmit="return validate(this);" on the form calls the validation but how can I check for duplicate records using asp before the form is sent to paypal?Any ideas? Link to comment Share on other sites More sharing options...
justsomeguy Posted December 3, 2011 Share Posted December 3, 2011 Send a synchronous ajax request during the validation to look up the username and figure if it's taken. Link to comment Share on other sites More sharing options...
holsy Posted December 3, 2011 Author Share Posted December 3, 2011 I am pretty weak with my Ajax or javascript coding coming from a vbscript background however I did come up with an ugly solution that will allow me to continue with asp and should work for now cause I am staring at a deadline. 'Form Validation Response.Write "<script type=""text/javascript"">" & _ "function validate(form) {var e = form.elements, m = '';" & _ "if(!e['txtUserName'].value) {m += 'Username is required<br/>';}" 'ASP CODE HERE Dim objRS, strtemp, strUserName, strPassword, strEmail, strPlan, strResponse strUserName = trim(Request.form("txtUserName")) Set objRS=Server.CreateObject("ADODB.Recordset") Set objRS.ActiveConnection=objConn objRS.CursorType=adOpenDynamic objRS.LockType=adLockOptimistic objRS.Open "SELECT * FROM users WHERE ([username] ='" & strUserName & "')",objConn IF objRS.EOF Then Else response.Write "if(!e['txtTesting'].value) {m += 'Duplicate Name Detected<br/>';}" End If 'End ASP CODE Response.Write "if(!e['txtPassword1'].value) {m += 'Password is required<br/>';}" & _ "if(!e['txtPassword2'].value) {m += 'Repeat Password is required<br/>';}" & _ "if(e['txtPassword1'].value != e['txtPassword2'].value) {m += 'Your passwords does not match<br/>';}" & _ "if(!/.+@[^.]+(\.[^.]+)+/.test(e['txtEmail'].value)) {m += 'Valid e-mail address is required<br/>';}" & _ "var btn = valButton(form.radiobuttons);" & _ "if (btn == null) {m += 'Please choose a plan<br/>';}" & _ "if(!e['terms-conditions'].checked ) {m += 'Must agree to terms and conditions<br/>';}" & _ "if(m) {" & _ "message_box.show_message('Please Complete the Following:\n\n', m);" & _ "return false;" & _ "}" & _ "return true;" & _ "}" & _ "function valButton(btn) {" & _ "var cnt = -1;" & _ "for (var i=btn.length-1; i > -1; i--) {" & _ "if (btn[i].checked) {cnt = i; i = -1;}" & _ "}" & _ "if (cnt > -1) return btn[cnt].value;" & _ "else return null;" & _ "}" & _ "</script>" Now I am calling javascript within asp. I know it's not the best practice but it seems to be working..Except I am unable to retrieve a value from strUserName = trim(Request.form("txtUserName")) so I can use the variable in my SQL statement. I am using response.Write "if(!e['txtTesting'].value) {m += 'Duplicate Name Detected<br/>';}" just to force a message dialog when a duplicate record has been found.Any idea how I can retrieve a value for Request.form("txtUserName") Link to comment Share on other sites More sharing options...
holsy Posted December 4, 2011 Author Share Posted December 4, 2011 justsomeguy, Took your advice about sending an ajax request. From what I've read, it is the best way to go about this. I am trying something like this: //Form Validationfunction validate(form) {var e = form.elements, m = '';if(!e['txtUserName'].value) {m += 'Username is required<br/>';}var xhReq = new XMLHttpRequest();var strusername = e['txtUserName'].valuexhReq.open("GET", "checkusername.asp?username=\'' + strusername + '\'",false);xhReq.send(null);var serverResponse = xhReq.responseText;alert(serverResponse);if(!e['txtPassword1'].value) {m += 'Password is required<br/>';}if(!e['txtPassword2'].value) {m += 'Repeat Password is required<br/>';}if(e['txtPassword1'].value != e['txtPassword2'].value) {m += 'Your passwords does not match<br/>';}if([email="!/.+@[^.]+(.[^.]+)+/.test(e["]!/.+@[^.]+(\.[^.]+)+/.test(e['txtEmail'].value[/email])) {m += 'Valid e-mail address is required<br/>';}var btn = valButton(form.radiobuttons);if (btn == null) {m += 'Please choose a plan<br/>';}if(!e['terms-conditions'].checked ) {m += 'Must agree to terms and conditions<br/>';}if(m) {//alert('The following error(s) occurred:\n\n' + m);message_box.show_message('Please Complete the Following:\n\n', m);return false;}return true;}function valButton(btn) {var cnt = -1;for (var i=btn.length-1; i > -1; i--) {if (btn[i].checked) {cnt = i; i = -1;}}if (cnt > -1) return btn[cnt].value;else return null;} I am using an alert box to test the response which seems to be working with a value passed to checkusername.asp like this:xhReq.open("GET", "checkusername.asp?username=True",false);I am unable to pass a variable because I think I have the incorrect javascript syntax:var username = e['txtUserName'].value //can't retrieve text box value here xhReq.open("GET", "checkusername.asp?username=\'' + strusername + '\'",false);I have a test asp receiving page: <%Dim objRS, strUserName strUserName = Request.QueryString("username") Set objRS=Server.CreateObject("ADODB.Recordset") Set objRS.ActiveConnection=objConn objRS.CursorType=adOpenDynamic objRS.LockType=adLockOptimistic objRS.Open "SELECT * FROM users WHERE ([username] ='" & strUserName & "')",objConn IF objRS.EOF Then Else Response.Write "Exists" End If objRS.Close Set objRS = Nothing%> The perhaps I can catch the serverResponse ...if serverResponse == "exists" , then display the message box. Link to comment Share on other sites More sharing options...
holsy Posted December 4, 2011 Author Share Posted December 4, 2011 thanks justsomeguy for pointing me in the right direction. I have finally got the synchronous ajax request to work! cheers! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.