0

I'm working on a classic asp application ( I know, not by choice). I have a button in my form that is supposed to do a Search based on criteria that user enters it.

To give a basic structure here's what it looks like:

<form action="IncidentMain.asp" method="post" name="frmAdd" onSubmit="return checkform( this )">

<input TYPE="image" SRC="Include/Search.gif"  ALT="Search" VALUE="submit" id="IMAGE4" name="IMAGE2" onclick="javascript: document.frmAdd.txtaction.value = 'search';" >

</form>

Upon clicking on the button, as can be seen in the form action ="IncidentMain.asp", On submit I call a function, which actually gets called properly.

function checkform ( form )
{

    if (form.txtIncidentNumber.value == "") {
        alert( "Please Enter Incident Number" );
        form.txtIncidentNumber.focus();
        return false ;
    }
    alert("IM HERE");
    return true ;
}

So it definitely gets through this function, then I have my last piece of code to actually do the search which is this:

<%
        IF Request.Form("txtaction") = "search" THEN
        'IT NEVER GETS HERE
        'SeLECT DATA FROM SQL SERVER
        End if
  %>

I do not understand why it happens like that. IT basically looks like it does not want to communicate with the server, it stops short somewhere. Is there any reason why this code would not work?

EDIT:

<form method="post" action="Incident.asp" name="frmuser" onsubmit="return checkform( this )">
     <input type="submit" value="Save / Submit" name="btnSubmit" 
     id="SaveButton"  >
</form>

this code here doesn't hit the checkform(this) function but it gets to my VB code which starts like this:

<%
    if Request.Form("btnSubmit") ="Save / Submit" THEN 
user692942
  • 16,398
  • 7
  • 76
  • 175
Koosh
  • 876
  • 13
  • 26
  • You have the submission also triggering the update of a input programmatically using the `onclick` handler, so how are you suppose to guarantee the javascript is ran before the page is submitted? Where is the `txtaction` input in the HTML structure you've posted? – user692942 Nov 28 '18 at 20:07
  • Too many assumptions being made, remove the `If` in `IncidentMain.asp` and just use `Response.Write()` to check the submission reaches it. – user692942 Nov 28 '18 at 20:11
  • Possible duplicate of [How to check form submission ASP classic](https://stackoverflow.com/questions/10674933/how-to-check-form-submission-asp-classic) – user692942 Nov 28 '18 at 20:12
  • Does the `alert()` not fire? You also should have your `txtIncidentNumber` text input field defined inside the `form`, the script is probably erroring in the background because it can’t find `form.txtIncidentNumber`. Check your web dev tools (usually F12 depending on browser). – user692942 Nov 29 '18 at 14:32
  • 1
    somehow its working now. thanks for your help – Koosh Nov 29 '18 at 15:13

3 Answers3

1

The approach could be a lot simpler if you just want to check that a search has been submitted you could use;

Dim is_submit: is_submit = (Len(Request.Form & "") > 0)
If is_submit Then
  'We have a POST form submission do something.
End If

or even;

Dim request_method: request_method = LCase(Request.ServerVariables("REQUEST_METHOD") & "")
If request_method = "post" Then
  'We have a POST request (likely a form submission).
End If

Storing hidden input values just clutters the HTML and in a lot of cases is unnecessary, when you also have to update them using event handlers in the page you add an extra layer of unnecessary complexity.

However for any of this to work you have to have something to contain the search, the very simplest of forms should look something like;

<form action="IncidentMain.asp" method="post">
  <input type="text" name="txtIncidentNumber" value="" />
  <input type="image" name="IMAGE2" src="Include/Search.gif" alt="Search" value="submit" />
</form>
user692942
  • 16,398
  • 7
  • 76
  • 175
  • Thank you for your answer. I am struggling with this since I have not really worked with Classic ASP. The thing is, each form has this line:
    Basically on Submit button, I want it to run the checkform(this) function before running the actual VB code. Here's where I struggle. At times, It goes into the checkform(this) function which does validations, but it never hits the vb code. Other times it hits vbCode but never hits the checkform(this) function
    – Koosh Nov 29 '18 at 14:14
  • You should still be able to use the `onsubmit` handler you just don’t need the `onclick` handler on the image input button. – user692942 Nov 29 '18 at 14:22
  • can you please see my edit. Thank you. I literally don't undestand why it's not working. I have another form that I copied for this particular form. The other one has the same code and everything works. This one doesnt – Koosh Nov 29 '18 at 14:25
0

Add a hidden txtaction input field into the form.

user692942
  • 16,398
  • 7
  • 76
  • 175
0

The form is missing the hidden field "txtaction". Add the field and enter the value="search". And remove the javascript that should have done that.

N F
  • 1
  • 2