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

Member Avatar
LastMitch

@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
} 

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

to oxiegen
return false;
is not working.
problem is as it is.

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.

return false is the answer.... ^_^