Search code examples
vbscriptasp-classic

asp application button doesn't perform correctly and does not execute the server call


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 

Solution

  • 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>