I have an ASP script with aspcaptcha which has comments form field, and sends an email to the webmaster. However if the visitor does not enter the captcha code correctly, they are taken back to the form page, a message states that they did not fill out the captcha code correctly BUT the form field is blank....
I am wanting to keep the comments in the form field and ask the visitor to re-fill out the captcha code.
Here is the the ASP script:
if Request.ServerVariables("REQUEST_METHOD") = "POST" then
captha = Trim(Request.Form("captha"))
if CheckCAPTCHA(captha) = true then
' Process form...CAPTCHA IS VALID!
else
response.redirect "myform.asp?c=f"
end if
else
response.redirect "myform.asp?v=f"
end if
The variable v=f tells the form that the captcha was not filled out, and to throw back an error. How can I populate the form fields with what the visitor already entered without asking them to type their comments all over again?
I don't know aspcaptcha but two possabilies:
First, does the form post to itself? That is, is the processing script on the same page as the form? If so then just use Request.Form("name")
<input name="frmName" type="text" id="frmName" value="<%
If Request.Form("frmName") <> "" Then Response.Write(Request.Form("frmName"))
%>" />
Second, If the form submits to a second page for processing and then returns the user to the original page, use session variables:
if CheckCAPTCHA(captha) = true then
' Process form...CAPTCHA IS VALID!
else
Session("frmName") = Request.Form("frmName")
response.redirect "myform.asp?c=f"
end if
Then
<input name="frmName" type="text" id="frmName" value="<%
If Session("frmName") <> "" Then Response.Write(Session("frmName"))
%>" />
Hope that helps...
A function usually helps in these cases:
Try Something like:
'Select between Session Value / Form Value
function SelectValue(sessionValue, frmValue)
result = ""
if isnull(frmValue) or frmValue = "" then
result = sessionValue
else
result = frmValue
end if
SelectValue = result
end function
Your form can then use the function as needed:
e.g.
<input name="frmName" type="text" id="frmName" value="<% SelectValue(Session("frmName"), Request.Form("frmName"))%>" />
Related
I have a simple html hidden field as shown below
<input id="currentDirectory" type="hidden" runat="server" value="123"/>
Now I need to pass this hidden field value using query string to asp.net handler ans I have done as shown below but it dosen't pass the value.
In Default.aspx page:
var value = $("#currentDirectory").val();
url: 'Uploader.ashx?myvalue=' + value,
Accessing in Handler.ashx:
Dim myvar As String= System.Web.HttpContext.Current.Session("myvalue")
Can anyone say me where am I going wrong?
I am working on an asp.net webforms project. I have a usercontrol with a boolean public property, named as IsRequired, which can be set either to true or false. I have a text box where the user will enter the social security number (SSN). If IsRequired = true, then the user has to enter the SSN value, otherwise validation should fail and "SSN is required" message should be displayed next to the textbox. If IsRequired = false, then if the user doesn't enter a SSN value, then a messagebox should be displayed with the message, "If you have a SSN, please enter it". Essentially the message encourages the suer to enter SSN. If the click "OK" on the messagebox, then they could enter the value. otherwise they could submit the page, and the validation should not fail.
I am using DevExpress controls, and came up with the following solution:
on ascx page:
<dx:ASPxTextBox ID="SsnTextBox" runat="server" OnValidation="SocialSecurity_Validation">
<MaskSettings Mask="999999999"></MaskSettings>
</dx:ASPxTextBox>
on the codebehind:
if (IsSubscriber && (e.Value == null || (string)e.Value == ""))
{
SsnTextBox.ErrorText = "Required";
e.IsValid = false;
}
else if (!IsSubscriber && (e.Value == null || (string)e.Value == ""))
{
e.IsValid = true;
Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "UserControl",
"confirm('If Social Security Number is available, please enter it!')", true);
}
The problem with the code it works only partially. For example, the message box is displayed, but when either ok or cancle is clicked, the page is posted without giving the option to enter a value. If IsRequired = true, and a value is not entered, validation fails, still the page gets submitted. Any help to resolve this or a better code is appreciated. The solution need not be DevExpress specific, instead it could a generic one.
Thanks
Rather than use the confirm method; which will return either a true or false depending on what the user selects, why not use Alert() instead?
I.E
return Alert('If Social Security Number is available, please enter it!')
There won't be a choice for the user - just an OK button. 'return' will also prevent the postback.
Basically I have a page named dvds.asp with a form that goes to action="process.asp". The process.asp script validates the Name, Surname and Email fields. I would like that if the email does not exist, a redirect will occur to the original page (dvd.asp) and also display a custom error.
The validation of the email field and redirect I know how to do. The only problem is how I am going to get the custom error value in the dvds.asp.
Any help please? :)
FYI:
I am using this code in process.asp to validate:
if txtEmail = "" then
Server.Transfer(Request.ServerVariables("HTTP_REFERER"))
else
Now the error I want to show on the dvds.asp page is: "Please fill in your email" .. How I can do that please?
Hard to say without seeing any source, (For instance, how are you validating the email? What is this "Custom error value"?
Assuming you have a function that validates the email and returns false if it fails and the matching error value is "1" then you could simply include this in your redirect:
Dim EmailAddress
EmailAddress = Request.Form("EmailAddress")
If ValidateEmail(EmailAddress) = False Then
Response.Redirect("dvd.asp?ErrorID=1")
End If
and on the Origional Source page, you could just display errors back to the user as follows:
'#### Output errors
If Trim(Request.QueryString("ErrorID")) <> "" Then
Select Case Request.QueryString("ErrorID")
Case "1" ' #### Email Error
Response.Write("<p><span style=""color: red"">Error: Invalid email address</span></p>")
Case "2" ' #### Bad Name
Response.Write("<p><span style=""color: red"">Error: Invalid Name</span></p>")
Case Else
Response.Write("<p><span style=""color: red"">Error: Unknown / Generic Error</span></p>")
End Select
End If
One small suggestion by the way, its often preferable for pages such as this to post to the same page and include any email functionality you need from a class / SSI. That way you cold auto-populate the form again if an error occurred rather than expecting your end user to re-type the whole form if they get an error.
For some reason when the user click on the submit button and he re-fresh the page the same data get's uploaded again to my SQL Server 2005 database. I do not what this to happen........... Why is this happening? I am making use of a SQL Data Source!!
My code
Try
'See if user typed the correct code.
If Me.txtSecurity.Text = Session("Captcha") Then
If Session("NotifyMe") = "Yes" Then
SendEmailNS()
End If
RaterRate.Insert()
RaterRate.Update()
DisableItems()
lblResultNS.Text = "Thank you for leaving a comment"
LoadCompanyList()
LoadRateRecords()
txtCommentNS.Text = ""
txtSecurity.Text = ""
lblResultNS.Focus()
Else
Session("Captcha") = GenerateCAPTCHACode()
txtSecurity.Text = ""
txtSecurity.Focus()
Validator10.Validate()
End If
Catch ex As Exception
lblResultNS.Visible = True
lblResultNS.Text = ex.Message.ToString
lblResultNS.Focus()
End Try
When user visiting your page, generating new captcha. after when post or else, its checking captcha but not generating new. Move 'captcha set' code to outer scope.
If Me.txtSecurity.Text = Session("Captcha") Then
...
Else
...
End If
Session("Captcha") = GenerateCAPTCHACode()
This is happening because of the nature of WebForms relying on the PostBack model. Every time you click on a button the single form you have inside the page is posted to the server along with the viewstate. When you press F5 you normally get a warning that the form will be reposted and if you click Yes the same action is executed on the server. One way to avoid this is to perform a redirect after you save to the database:
Response.Redirect("~/success.aspx");
Are there any special considerations trying to read up data from an HTML form where the element is an input type="Password"? When a ColdFusion page POSTs my handler with form data I am getting null for the password instead of the typed value.
Here is the key line from the larger block below:
string password = context.Request.Form["strPassword"];
I have an HTTPHandler.ashx code file that performs an upload of a file when posted. Here is the key snippet of this code:
string username = context.Request.Form["strUsername"];
if (String.IsNullOrEmpty(username))
{
IdentifyInvoker = GetUserInfo();
brokerService = new Broker.FileService();
}
else
{
string password = context.Request.Form["strPassword"];
string domain = context.Request.Form["strDomain"];
IdentifyInvoker = GetInvokerInfoFromForm(username, password, domain);
brokerService = new Broker.FileService(username,password,domain);
}
The form from which the above code is posted (from ColdFusion) looks like this:
<b>User Name</b> <input type="text" name="strUsername" id="strUsername" size="13" />
<b>Password</b> <input type="Password" name="strPassword" id="strPassword" size="15" />
<b>Domain</b> <input type="text" name="strDomain" id="strDomain" size="13" value="cbmiweb" />
I was able to trap this with the debugger and was shocked to see that after this:
string password = context.Request.Form["strPassword"];
... password = null
In the immediate window, sure enough:
?context.Request.Form["strPassword"]
null
If I examine the entire Form collection in the debugger, I see the proper values laid out (separated by &) and none of the important data elements is null (but strangely the data contains a plus sign in front of the equal sign)! Here is a snippet from the immed window:
&strUsername=johna&strPassword+=xxxxxxxx&strDomain+=cbmiweb}
I have an ASP.NET client that POSTs to this same HTTPHandler and that works fine. Here the same form data shows without the interfering PLUS signs:
&strUsername=johna&strPassword=xxxxxxxx&strDomain=cbmiweb}
Any ideas on what causes this and how to retrieve the form data when it's formatted with the intervening PLUS signs?
EDIT:
Both the ASP.NET form and the ColdFusion form specify enctype="multipart/form-data" yet the latter embeds these PLUS signs.
Plus sign is the problem, it should not have been there, is your coldfusion forwarding request to your page or it is using its internal http request engine to do so?
Plus sign appears due to white space, please check in your coldfusion if any string concatenation caused white spaces to be inserted in your posted data?