Form Submit Problem


M

Myk Quayce

I have a small .aspx page that uses old style forms with client-side JavaScript validation. Everything works fine until I try to
upate the database with some new information. I must be doing something fundamentally wrong because I can't find any help on this
anywhere.

Here's my HTML form...

<form name="frmNewUser" onSubmit="return validateForm();">
<table>
<tr><td>User Name:</td><td><input id="iptUserName" type="text" size="20" /></td></tr>
<tr><td>E-mail Address:</td><td><input id="iptUserEmail" type="text" size="40" /></td></tr>
<tr><td colspan=2><input type="submit" value="Submit Details" /></td></tr>
</table>
</form>

Here's my client-side JavaScript validation...

function validateForm()
{
var userName = document.frmNewUser.iptUserName.value;
var userEmail = document.frmNewUser.iptUserEmail.value;

if(userName.length < 2 || userName.length > 15)
{
alert("The user name must be between 2 and 15 characters.");
return false;
}

if(userEmail.indexOf("@") == -1)
{
alert("You must enter a valid e-mail address.");
return false;
}

return true;
}

How do I get the values from the form in to my database?
 
Ad

Advertisements

S

Scott M.

Your form doesn't have an ACTION or a METHOD attribute and so if it is an
"old style form" as you say, the data isn't going anywhere and therefore,
you can't retrieve it.


Myk Quayce said:
I have a small .aspx page that uses old style forms with client-side
JavaScript validation. Everything works fine until I try to
upate the database with some new information. I must be doing something
fundamentally wrong because I can't find any help on this
anywhere.

Here's my HTML form...

<form name="frmNewUser" onSubmit="return validateForm();">
<table>
<tr><td>User Name:</td><td><input id="iptUserName" type="text"
size="20" /> said:
<tr><td>E-mail Address:</td><td><input id="iptUserEmail" type="text"
 
Ad

Advertisements

M

Munsifali Rashid

As this is an old-style form, you can't access the form elements the asp.net
way (eg. using controlname.Text). However, if you modify the form tag to
include the action and method attributes, so that is posts back to the same
page (action="mypage.aspx" method="post"), you should be able to access the
posted data using Request.Form.Get("iptUserName") and
Request.Form.Get("iptUserEmail") in the Page_Load event.

You might also want to add a hidden action field in the form, and check this
in the page load, to make sure that the postback is caused by your old-style
form and not the asp.net form. Once you have this data, you can the execute
the appropriate SQL to update your database.

Eg.

<form name="frmNewUser" action="newuser.aspx" method="post"
onSubmit="validateForm()">
<input name="action" type="hidden" value="newUserRegistration">
<!-- Standard stuff here, as specified below -->

In the code behind file:

private void Page_Load(object sender, System.EventArgs e)
{
// Get the action
string action = Request.Form.Get("action");

// Check the action
if (action == "newUserRegistration")
{
// Get posted values here
string username = Request.Form.Get("iptUserName").Trim();
string email = Request.Form.Get("iptUserEmail").Trim();

// Check they aren't blank, and/or other validation here
boolDataOK = ((username != string.Empty) && (email != string.Empty))
if (boolDataOK) updateData(username, email);
}
}

// Update the database
private void updateData(string username, string email)
{
// Use the username and email parameters here to update the DB
}


Hope this helps,

Mun




Myk Quayce said:
I have a small .aspx page that uses old style forms with client-side
JavaScript validation. Everything works fine until I try to
upate the database with some new information. I must be doing something
fundamentally wrong because I can't find any help on this
anywhere.

Here's my HTML form...

<form name="frmNewUser" onSubmit="return validateForm();">
<table>
<tr><td>User Name:</td><td><input id="iptUserName" type="text"
size="20" /> said:
<tr><td>E-mail Address:</td><td><input id="iptUserEmail" type="text"
 

Top