Help with JQuery form and ASP code

Hello All,

I'm a little stuck, my form is not submitting..I have the following elements;;

The form

<form action="" method="post" name="Register" class="fancyform" id="Register">
  <h3>Register</h3>
    <div class="notify">Please complete the form below and click 'Register'</div>
    <div class="alert">&nbsp;</div><div class="halfbox">
    <label for="regfirstname">First name</label>
    <input name="regfirstname" type="text" required="required" id="regfirstname" form="Register" placeholder="First name" value="" maxlength="50" /><br/>
    </div>
    <div class="halfbox">
    <label for="regfirstname">Last name</label>
    <input name="reglastname" type="text" required="required" id="reglastname" form="Register" placeholder="Last name" maxlength="50" /><br/>
    </div>
    <div class="halfbox">
    <label for="regfirstname">Email</label>
    <input name="regemailaddress" type="email" required="required" id="regemailaddress" form="Register" placeholder="e.g joe.bloggs@yahoo.co.uk" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$" maxlength="135" /><br/>
    </div>
    <div class="halfbox">
    <label for="regfirstname">Password</label>
    <input name="regpassword" type="password" required="required" id="regpassword" form="Register" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}" maxlength="50" /><br/>
    </div>
    <div class="labeldiv">
      <label title="Tick this option to have this computer / device store your login credentials">
                                                <input type="checkbox" name="rememberme" value="true" checked="checked">
                                                Remember me
      </label>
    </div>
    <input name="Register" type="submit" disabled="disabled" id="RegisterSubmit" form="Register" value="Register" />
  <input name="Cancel" type="button" class="cancelbutton closefb" id="RegisterCancel" onclick="$.fancybox.close()" value="Cancel" form="Register" />
  <input type="hidden" name="returnUrl3" value="">
</form>

Open in new window


The JQuery / Ajax

  $("#Register").submit(function(e){
      e.preventDefault();
    $.ajax({
		type: "POST",
        url: "/actions/register.asp",
        data:$(this).serialize(),
		success:function(result){
          if(result==2){
            // do something success
     		 $(this).fancybox.close();
			 location.reload();
          }else {
            // do something fail
	      $('#RegisterError').html('<p>ErrorMessage here</p>');
          $('#RegisterError').fadeIn('slow');
          }
  	  }});
  });

Open in new window


The VBscript

<!--#include virtual="/Connections/******.asp" -->
<%
if Request("regemailaddress") <> "" AND Request("regpassword") <> "" AND Request("regfirstname") <> "" AND Request("reglastname") <> "" then

Dim CMDRegister__Username
CMDRegister__Username = NULL
if(Request("regemailaddress") <> "") then CMDRegister__Username = Trim(Request("regemailaddress"))

Dim CMDRegister__Password
CMDRegister__Password = NULL
if(Request("regpassword") <> "") then CMDRegister__Password = Trim(Request("regpassword"))

Dim CMDRegister__FirstName
CMDRegister__FirstName = NULL
if(Request("regfirstname") <> "") then CMDRegister__FirstName = Trim(Request("regfirstname"))

Dim CMDRegister__LastName
CMDRegister__LastName = NULL
if(Request("reglastname") <> "") then CMDRegister__LastName = Trim(Request("reglastname"))

set CMDRegister = Server.CreateObject("ADODB.Command")
CMDRegister.ActiveConnection = MM_******_STRING
CMDRegister.CommandText = "dbo.Register"
CMDRegister.CommandType = 4
CMDRegister.CommandTimeout = 0
CMDRegister.Prepared = true
CMDRegister.Parameters.Append CMDRegister.CreateParameter("@RETURN_VALUE", 3, 4)
CMDRegister.Parameters.Append CMDRegister.CreateParameter("@Username", 200, 1,350,CMDRegister__Username)
CMDRegister.Parameters.Append CMDRegister.CreateParameter("@Password", 200, 1,50,CMDRegister__Password)
CMDRegister.Parameters.Append CMDRegister.CreateParameter("@FirstName", 200, 1,50,CMDRegister__FirstName)
CMDRegister.Parameters.Append CMDRegister.CreateParameter("@LastName", 200, 1,80,CMDRegister__LastName)
set RegisterRS = CMDRegister.Execute
RegisterRS_numRows = 0

If not RegisterRS.eof then
arrRegister = RegisterRS.GetRows()
End if 

RegisterRS.Close() 'Clean Up
Set RegisterRS = Nothing 'Clean Up
Set CMDRegister = Nothing 'Clean Up

If IsArray(arrRegister) Then
dim candlogin 
For candlogin = 0 to ubound(arrRegister, 2)

if arrRegister(0,candlogin) = 1 then

Response.write(1)

Elseif arrRegister(0,candlogin) = 2 then

Response.Cookies("UserAuth")("Userid")= arrRegister(1,candlogin)
Response.Cookies("UserAuth")("Authentication")= arrRegister(2,candlogin)
Response.Cookies("UserAuth")("UserEmail")= arrRegister(3,candlogin)
Response.Cookies("UserAuth").("LoggedIn")=response.write(Date)

If request("rememberme") is null then
Response.Cookies("UserAuth").Expires = dateAdd("n", 20, Now())
end if

Set myMailApplication=CreateObject("CDO.Message")
URLShort="this is the subject of a test email second version"
URLLong="http://j******"
ReplyMail="******@*****"
ToMail="******@*****"
FromMail="""******"" <******@*****>"
'Attach=(Server.MapPath(CVAPPLICATION("JBAPCV")))
myMailApplication.Subject= URLShort
myMailApplication.From= FromMail
myMailApplication.To= ToMail
myMailApplication.ReplyTo= ReplyMail
myMailApplication.CreateMHTMLBody URLLong
'myMailApplication.AddAttachment Attach
myMailApplication.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 1
myMailApplication.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverpickupdirectory") = "c:\inetpub\mailroot\pickup"
myMailApplication.Configuration.Fields.Update
myMailApplication.Configuration.Fields.Update
myMailApplication.Send
set myMailApplication=nothing
Response.Write(2)
end if
end if
%>

Open in new window


Finally the stored procedure

CREATE PROCEDURE [dbo].[******Register]
@Username nvarchar(350),
@Password nvarchar(50),
@FirstName nvarchar(50),
@LastName nvarchar(80)
AS
SET NOCOUNT ON;

/*Declare additional variables */
DECLARE @Result int
DECLARE @UserID NVARCHAR(225)
DECLARE @UserAuth NVARCHAR(225)
DECLARE @SALT NVARCHAR(25)
DECLARE @PWDWITHSALT NVARCHAR(125)

BEGIN

IF EXISTS (SELECT ID from dbo.Candidate where Username = @Username)
BEGIN
SET @Result = '1'
END
ELSE
BEGIN
SELECT @SALT = [dbo].[ufn_GeneratePassword] (10)
SET @PWDWITHSALT = @Password + @SALT
SET @Result = '2'
INSERT INTO dbo.Candidate (Username, [Password], FirstName, LastName, DateRegistered, Authorised, JobAlertFrequesncy, LastAccess, SALT)
VALUES (@Username, HASHBYTES('SHA1', @PWDWITHSALT), @FirstName, @LastName, GETDATE(), 'N', 1, GETDATE(), @SALT)
SET @UserID = SCOPE_IDENTITY()
SET @UserAuth = HASHBYTES('SHA1', @PWDWITHSALT)
INSERT INTO dbo.Profile(AccountID, DateCreated)
Values (@UserID, GETDATE())
END
Select 
@Result as Result, 
@UserID as UserID, 
@UserAuth as UserAuth,
@Username as UserEmail
END


GO

Open in new window


When I test - with a password like @dyskub1 nothing happens..
Additionally I would only like to show the div <div class="alert">&nbsp;</div> if the result = 1.

Any ideas - much appreciated.

Thank you
garethtnashAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Can you give us some idea of what goes wrong - which part is not working.

1. Check the console for script errors - are there any
2. Does the AJAX fire - check your console for a POST and check the response for errors
3. Is your VB code working - can you test the code independently by either creating a dummy form and posting test values to it OR send the parameters to the script with a get request and see what is returned
4. Probably do this first - is your stored procedure working - if you setup a dummy script to call the Stored proc with test values does it work

We would need some idea of where to look on this - there are several components and short of testing them all we don't know where to start looking. Give us as much info as you can on how far the process gets before it fails - and any errors that are returned at that point.
hieloCommented:
You have:
For candlogin = 0 to ubound(arrRegister, 2)

but you don't have the "closing" Next statement

Also:
                    If request("rememberme") is null then
should be:
                    If IsNull(request("rememberme")) Then

You also seem to be missing and End If right before where Next should be:
...
                    set myMailApplication=nothing
                 end if
            Next

            Response.Write(2)
      end If
end if

You should be looking at your server logs to track down the server errors. It should tell you the file and line number where errors were encountered.

Try:
<!--#include virtual="/Connections/******.asp" -->
<%
if Request("regemailaddress") <> "" AND Request("regpassword") <> "" AND Request("regfirstname") <> "" AND Request("reglastname") <> "" then

	Dim CMDRegister__Username
	CMDRegister__Username = NULL
	if(Request("regemailaddress") <> "") then CMDRegister__Username = Trim(Request("regemailaddress"))

	Dim CMDRegister__Password
	CMDRegister__Password = NULL
	if(Request("regpassword") <> "") then CMDRegister__Password = Trim(Request("regpassword"))

	Dim CMDRegister__FirstName
	CMDRegister__FirstName = NULL
	if(Request("regfirstname") <> "") then CMDRegister__FirstName = Trim(Request("regfirstname"))

	Dim CMDRegister__LastName
	CMDRegister__LastName = NULL
	if(Request("reglastname") <> "") then CMDRegister__LastName = Trim(Request("reglastname"))

	set CMDRegister = Server.CreateObject("ADODB.Command")
	CMDRegister.ActiveConnection = MM_******_STRING
	CMDRegister.CommandText = "dbo.Register"
	CMDRegister.CommandType = 4
	CMDRegister.CommandTimeout = 0
	CMDRegister.Prepared = true
	CMDRegister.Parameters.Append CMDRegister.CreateParameter("@RETURN_VALUE", 3, 4)
	CMDRegister.Parameters.Append CMDRegister.CreateParameter("@Username", 200, 1,350,CMDRegister__Username)
	CMDRegister.Parameters.Append CMDRegister.CreateParameter("@Password", 200, 1,50,CMDRegister__Password)
	CMDRegister.Parameters.Append CMDRegister.CreateParameter("@FirstName", 200, 1,50,CMDRegister__FirstName)
	CMDRegister.Parameters.Append CMDRegister.CreateParameter("@LastName", 200, 1,80,CMDRegister__LastName)
	set RegisterRS = CMDRegister.Execute
	RegisterRS_numRows = 0

	If not RegisterRS.eof then
		arrRegister = RegisterRS.GetRows()
	End if

	RegisterRS.Close() 'Clean Up
	Set RegisterRS = Nothing 'Clean Up
	Set CMDRegister = Nothing 'Clean Up

	If IsArray(arrRegister) Then
		dim candlogin 
		For candlogin = 0 to ubound(arrRegister, 2)

			if arrRegister(0,candlogin) = 1 then

				Response.write(1)

			Elseif arrRegister(0,candlogin) = 2 then
        
		        Response.Cookies("UserAuth")("Userid")= arrRegister(1,candlogin)
    		    Response.Cookies("UserAuth")("Authentication")= arrRegister(2,candlogin)
    		    Response.Cookies("UserAuth")("UserEmail")= arrRegister(3,candlogin)
    		    Response.Cookies("UserAuth").("LoggedIn")=response.write(Date)

		        ' If request("rememberme") is null then
		        If IsNull(request("rememberme")) Then
        		    Response.Cookies("UserAuth").Expires = dateAdd("n", 20, Now())
		    	end if
        
	        	Set myMailApplication=CreateObject("CDO.Message")
        		URLShort="this is the subject of a test email second version"
		        URLLong="http://j******"
		        ReplyMail="******@*****"
		        ToMail="******@*****"
		        FromMail="""******"" <******@*****>"
		        'Attach=(Server.MapPath(CVAPPLICATION("JBAPCV")))
		        myMailApplication.Subject= URLShort
		        myMailApplication.From= FromMail
		        myMailApplication.To= ToMail
		        myMailApplication.ReplyTo= ReplyMail
		        myMailApplication.CreateMHTMLBody URLLong
		        'myMailApplication.AddAttachment Attach
		        myMailApplication.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 1
		        myMailApplication.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverpickupdirectory") = "c:\inetpub\mailroot\pickup"
		        myMailApplication.Configuration.Fields.Update
		        myMailApplication.Configuration.Fields.Update
		        myMailApplication.Send
		        set myMailApplication=nothing
			end if
		Next
		Response.Write(2)
	end If
end if
%>

Open in new window

garethtnashAuthor Commented:
Hello,

The error I'm getting now is;

|56|800a03f2|Expected_identifier

Line 56 is Response.Cookies("UserAuth").("LoggedIn")=response.write(Date)

Any suggestions
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

hieloCommented:
Get rid of the "." between the ")(":

 Response.Cookies("UserAuth")<Nothing should be here>("LoggedIn")
garethtnashAuthor Commented:
Hi Hielo,

Thats great - data is now being written to the database. However the Fancybox module isn't doing anything, and no cookies are being written. So for reference

The form is a different page loaded in a Fancybox
The JQuery is in an includes file
The Vbscript is stored in a Scripts file called when posted..

So in the Vbscript, this part isn't working...

		        Response.Cookies("UserAuth")("Userid")= arrRegister(1,candlogin)
    		    Response.Cookies("UserAuth")("Authentication")= arrRegister(2,candlogin)
    		    Response.Cookies("UserAuth")("UserEmail")= arrRegister(3,candlogin)
    		    Response.Cookies("UserAuth")("LoggedIn")=response.write(Date)

		        ' If request("rememberme") is null then
		        If IsNull(request("rememberme")) Then
        		    Response.Cookies("UserAuth").Expires = dateAdd("n", 20, Now())
		    	end if

Open in new window


And the JQuery bit below isn't working;

		success:function(result){
          if(result==2){
            // do something success
     		 $(this).fancybox.close();
			 location.reload();
          }else {
            // do something fail
	      $('#RegisterError').html('<p>ErrorMessage here</p>');
          $('#RegisterError').fadeIn('slow');
          }

Open in new window


Appreciate your help.

Thank you
hieloCommented:
You need to verify that you are not getting any javascript errors first:
Load your page in Chrome.  Right-click on it and then choose "Inspect Element" >Console.  Clear any errors/message that may be there.  Then refresh your page and see if new errors show up and if so address those first.

Then click on the "Network" tab.  Submit your form.  Click on the "request" that is sending data to your server.  You should see a "Response" section.  What output are you getting?

Is your form on the public domain somewhere?
garethtnashAuthor Commented:
Hi

So the response I'm getting is -

12/08/20152

Which is strange..

Yes on the WWW - please click register  in the left column of the footer here

http://goo.gl/DXkw2i

Thanks
garethtnashAuthor Commented:
OK, now I get a response of 2, I changed

Response.Cookies("UserAuth")("LoggedIn")=Response.Write(Date)

to

Response.Cookies("UserAuth")("LoggedIn")=Date

However, the Fancybox is still not closing, and the cookies are not being created.

Do I need to have the JQuery write the cookies instead of the remote script?

Thanks
hieloCommented:
Instead of Response.write(1) or and Response.Write(2), save the value onto a variable first -ex
result =1  and result=1 respectively

You can only send cookies before you send any output to the browser.  I suspect your array has more than one result and is initially emitting a 1, then on the next iteration (in the elseif clause) it attempts to send a cookie but it's too late.  It already did a Response.write(1).

Try:
<!--#include virtual="/Connections/******.asp" -->
<%
if Request("regemailaddress") <> "" AND Request("regpassword") <> "" AND Request("regfirstname") <> "" AND Request("reglastname") <> "" then

	Dim CMDRegister__Username
	CMDRegister__Username = NULL
	if(Request("regemailaddress") <> "") then CMDRegister__Username = Trim(Request("regemailaddress"))

	Dim CMDRegister__Password
	CMDRegister__Password = NULL
	if(Request("regpassword") <> "") then CMDRegister__Password = Trim(Request("regpassword"))

	Dim CMDRegister__FirstName
	CMDRegister__FirstName = NULL
	if(Request("regfirstname") <> "") then CMDRegister__FirstName = Trim(Request("regfirstname"))

	Dim CMDRegister__LastName
	CMDRegister__LastName = NULL
	if(Request("reglastname") <> "") then CMDRegister__LastName = Trim(Request("reglastname"))

	set CMDRegister = Server.CreateObject("ADODB.Command")
	CMDRegister.ActiveConnection = MM_******_STRING
	CMDRegister.CommandText = "dbo.Register"
	CMDRegister.CommandType = 4
	CMDRegister.CommandTimeout = 0
	CMDRegister.Prepared = true
	CMDRegister.Parameters.Append CMDRegister.CreateParameter("@RETURN_VALUE", 3, 4)
	CMDRegister.Parameters.Append CMDRegister.CreateParameter("@Username", 200, 1,350,CMDRegister__Username)
	CMDRegister.Parameters.Append CMDRegister.CreateParameter("@Password", 200, 1,50,CMDRegister__Password)
	CMDRegister.Parameters.Append CMDRegister.CreateParameter("@FirstName", 200, 1,50,CMDRegister__FirstName)
	CMDRegister.Parameters.Append CMDRegister.CreateParameter("@LastName", 200, 1,80,CMDRegister__LastName)
	set RegisterRS = CMDRegister.Execute
	RegisterRS_numRows = 0

	If not RegisterRS.eof then
		arrRegister = RegisterRS.GetRows()
	End if

	RegisterRS.Close() 'Clean Up
	Set RegisterRS = Nothing 'Clean Up
	Set CMDRegister = Nothing 'Clean Up

	Dim result
	
	If IsArray(arrRegister) Then
		dim candlogin 
		For candlogin = 0 to ubound(arrRegister, 2)

			if arrRegister(0,candlogin) = 1 then

				'Response.write(1)
				result = 1

			Elseif arrRegister(0,candlogin) = 2 then
        
		        Response.Cookies("UserAuth")("Userid")= arrRegister(1,candlogin)
    		    Response.Cookies("UserAuth")("Authentication")= arrRegister(2,candlogin)
    		    Response.Cookies("UserAuth")("UserEmail")= arrRegister(3,candlogin)
    		    Response.Cookies("UserAuth")("LoggedIn")=Date

		        ' If request("rememberme") is null then
		        If IsNull(request("rememberme")) Then
        		    Response.Cookies("UserAuth").Expires = dateAdd("n", 20, Now())
		    	end if
        
	        	Set myMailApplication=CreateObject("CDO.Message")
        		URLShort="this is the subject of a test email second version"
		        URLLong="http://j******"
		        ReplyMail="******@*****"
		        ToMail="******@*****"
		        FromMail="""******"" <******@*****>"
		        'Attach=(Server.MapPath(CVAPPLICATION("JBAPCV")))
		        myMailApplication.Subject= URLShort
		        myMailApplication.From= FromMail
		        myMailApplication.To= ToMail
		        myMailApplication.ReplyTo= ReplyMail
		        myMailApplication.CreateMHTMLBody URLLong
		        'myMailApplication.AddAttachment Attach
		        myMailApplication.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 1
		        myMailApplication.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverpickupdirectory") = "c:\inetpub\mailroot\pickup"
		        myMailApplication.Configuration.Fields.Update
		        myMailApplication.Configuration.Fields.Update
		        myMailApplication.Send
		        set myMailApplication=nothing
			end if
		Next
		result = 2
	end If
end if

Response.Write(result)
%>

Open in new window

garethtnashAuthor Commented:
Hi Hielo,

No joy I'm afraid. The fancybox still remains and no cookies created, but register.asp is returning a 2.

For the cookies is it because they are not being created in the actual browser? Instead a remote script?

I'm guessing that the fancy box not closing is something to do with the JQuery?

  $("#Register").submit(function(e){
      e.preventDefault();
    $.ajax({
		type: "POST",
        url: "/actions/register.asp",
        data:$(this).serialize(),
		success:function(result){
          if(result==2){
            // do something success
     		 $(this).fancybox.close();
			 location.reload();
          }else {
            // do something fail
	      $('#RegisterError').html('<p>ErrorMessage here</p>');
          $('#RegisterError').fadeIn('slow');
          }
  	  }});
  });

Open in new window


Interestingly, I should only get a 2 if the registration was successful, if you attempt to register with an existing email address, it should return 1.

I've deleted all data since you registered

Thanks
garethtnashAuthor Commented:
OK, I've managed to sort the last bit -

"Interestingly, I should only get a 2 if the registration was successful, if you attempt to register with an existing email address, it should return 1."

But still having difficulty with the rest.

Thanks
hieloCommented:
>> $(this).fancybox.close();
Try:
$.fn.fancybox.close();

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
garethtnashAuthor Commented:
Hi Hielo,

No joy I'm afraid!

Any other thoughts?

Thanks
garethtnashAuthor Commented:
OK - The cookies now work - So its just two items now -

Closing Fancybox if the registration is successful and

Additionally I would only like to show the div <div class="alert">&nbsp;</div> if the result = 1

So I want to hide the div, unless result =1 if result =1 I want to show with an error message that reads - There is already an account registered with @email username.

Thanks for all your support with this.
garethtnashAuthor Commented:
OK, changed to this;

$.fancybox.close();

This works, so the only issue now is the error div?

Any thoughts?

Thanks
garethtnashAuthor Commented:
Hello Heilo,

All sorted - thank you - you're a star

Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
AJAX

From novice to tech pro — start learning today.