0

My requirement is, when i press on button
1)it should check whether the textbox is blank it should show alertbox to enter value and stay on that page.
2)if value is correct than check value and display message on next page.

i have developed two files they are as following.
file 1:(html & javascript code)

<html>

    <head>

        <title>Java Script Practical</title>

            <script type="text/javascript">
            function valid()    
            {
                if(form_valid.txt1.value.length==0 || form_valid.txt1.value=="" )
                {
                    alert("Textbox should not be blank");
                }
            }
            </script>
    </head>

    <body>

        <form name="form_valid" method="get" onSubmit="valid()" action="check.php">

            Enter No: <input name="txt1" type="text" />

            <br />

            <input name="btn" type="submit" value="Submit" />
        </form>
    </body>
</html>

file 2: (php file)

<html>
<body>
    <?php 
        $a=$_GET['txt1'];
        if($a<0)
        {
            echo "no is less than 0";
        }
        else if($a>0)
        {
            echo "no is greater than 0";
        }
        else
        {
            echo "no is zero";
        }
    ?>
</body>
</html>

this code works. but when i press on button it shows alertbox and display message on next page also. i want to make it work as my requirement as i mentioned in the starting of discussion.

please someone help to solve the problem.
thank you
Akash

5
Contributors
6
Replies
7
Views
5 Years
Discussion Span
Last Post by ome2012
0

@akashbarot51

it should check whether the textbox is blank it should show alertbox to enter value and stay on that page.

You can used this for the alertbox to stay on that page:

if (isset($message)){ 
echo $message; 
}else{
//form goes here
} 
0

Add return false; in your javascript code after displaying the alert box.
That normally stops the form from submitting.

0

problem got solved by one of my friend.
`<html>

<head>

    <title>Java Script Practical</title>

        <script type="text/javascript">
        function valid()    
        {
            if(form_valid.txt1.value.length==0 || form_valid.txt1.value=="" )
            {
                alert("Textbox should not be blank");
                return false;
            }
        }
        </script>
</head>

<body>

    <form name="form_valid" method="get" onSubmit="return valid(form_valid)" action="check.php">

        Enter No: <input name="txt1" type="text" />

        <br />

        <input name="btn" type="submit" value="Submit" />
    </form>
</body>

</html>
`
this code work as i required.
thanks for replying.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.