Dear Sir,

I have following codes

    <?php
    $country="";
    $capital="";
    if (isset($_POST['submit'])){
    $country=$_POST['text1'];
    $capital=$_POST['text2'];

    if (empty($country) || empty($capital)){
    die ("<script> alert('Please fill both boxes')</script>");
    }else{
    echo "<script> alert('The capital of  $country is $capital')</script>";
    }
    if (isset($_POST['clear'])){
    $country="";
    $capital="";
    }
    }
    ?>
    <html>
    <head>
    <title>Result on same page</title>
    </head>
    <body>
    <center>
      <form name="form1" action="" method="post">
        Country<input type="text" name="text1" value="<?php echo $country;?>"><br>
        Capital<input type="text" name="text2" value="<?php echo $capital;?>"> <br><br>
        <input type="submit" name="submit" value="Result">
        <input type="submit" name="clear" value="clear">
      </form>
    </center>
    </body>
    </html>

When I enter values in boxes and press Display Button then it shows result on same page
But...
When When I press Display Button without entring any values in textboxes then browser goes to second page.

With empty boxes it show result on next page.

I want it to remain in same brower in both case, Filled textbox or Empty texbexes.

Please help

remove this line
die ("<script> alert('Please fill both boxes')</script>");
to this one
echo ("<script> alert('Please fill both boxes')</script>");

For Page Protection use this on top of the page

session_start();
if($_SESSION[type]!='ACTAdmin'){
header("location:index.php");
exit();
}

please use type of the user.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.