Search code examples
javascripthtmlasp-classicsql-server-2000

javascript popup concat not working


I using a simple form where I'm validating a user login. Where I want to pass user name in my javascript POPUP for every successful login. For now it is only displaying Hello .I'm using concatination operator but no luck.

this is what my Javascript looks like

<script language="javascript">
        function show()
        {
            var n = document.getElementById("uname").value;
            alert("Hello "+n);
            return false;
        }
        function noshow()
        {
            alert("Invalid Login");
            return false;
        }
    </script>

HTML & Asp code

<form method="post" name="myform">
            <table border="0" width="100%" cellpadding="0" cellspacing="0">
                <tr>
                    <td>User Name:</td>
                    <td><input type="text" name="uname" value="" /></td>
                </tr>
                <tr>
                    <td>Password:</td>
                    <td><input type="password" name="pass" value="" /></td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <input type="submit" value="Login" name="sub" />
                        <input type="reset" value="Clear" />
                    </td>
                    <%
                        if Request.Form("sub") <> "" then
                            sql="select * from login_tbl where u_name = '" & Request.Form("uname") & "' AND pass= '" & Request.Form("pass") & "'"
                            rs.Open sql,con,1,2
                            if rs.Eof then 
                                Response.Write("<script language='javascript'>noshow();</script>")
                            else
                                'Response.Redirect("http://www.google.com")
                                Response.Write("<script language='javascript'>show();</script>")

                            end if
                            rs.Close
                        end if
                    %>
                </tr>
            </table>
        </form>

Solution

  • You just need to do something like this:

    <td><input type="text" id="uname" name="uname" value="<% Response.Write(Request.Form("uname")) %>" /></td>
    

    After you submit a form, uname became empty, and you need to restore value of uname element on server.

    Also, you are using document.getElementById(), but your uname element has no id. So it will find nothing. In order to avoid that, you need to add id to that input (like in my code above).

    But in this case uname will show username after form is submitted. If you do not need that, you can do something like this:

    <%
                            if Request.Form("sub") <> "" then
                                sql="select * from login_tbl where u_name = '" & Request.Form("uname") & "' AND pass= '" & Request.Form("pass") & "'"
                                rs.Open sql,con,1,2
                                if rs.Eof then 
                                    Response.Write("<script language='javascript'>noshow();</script>")
                                else
                                    'Response.Redirect("http://www.google.com")
                                    Response.Write("<script language='javascript'>show('" & Request.Form("uname") & "');</script>")
    
                                end if
                                rs.Close
                            end if
                        %>
    

    and update your function like this:

    function show(n)
            {
                alert("Hello "+n);
                return false;
            }