在同一网页上显示警告/弹出消息框?

    <?php

function goback()
{ 
    header("refresh:1; url=index.php");
    exit;
}

if(isset($_POST['submit'])){

  $name = $_POST['name'];
  $comment = $_POST['comment'];
  $mob = $_POST['mob'];
  $email = $_POST['email'];

  $to ='xyz@gmail.com';
  $subject= 'Contact form pharma';
  $message ="Name: ".$name."
".
            "Comment: ".$comment."
".
            "Mobile: ".$mob."
".
            "E-mail: ".$email; 
  if(mail($to, $subject, $message)){    
    echo "<script type='text/javascript'>alert('message successfully sent');window.close();</script>";
    goback();
  }
else 
{
  echo "something went wrong";
}
}
?>

Index.php on this page we have a form request acallback for which we use a html form and mail Function

<form>         
<fieldset>
                        <legend>
                         <center>Request a call back</center>
                        </legend>
                      <div class="form-group">
                        <label class="control-label" for="requestid">Your Name</label>
                         <div>
                          <input id="requestid" name="name" placeholder="Name please" class="form-control input-md" required="" type="text">
                         </div>
                      </div>
                      <div class="form-group">
                        <label class="control-label" for="dis">Your Comment</label>
                       <div>
                        <textarea class="form-control" placeholder="Your Comment" id="comment" name="comment"></textarea>
                       </div>
                      </div>
                     <div class="form-group">
                     <label class="control-label" for="dis">Mobile No</label>
                      <div>                       
                      <input placeholder="1234-456-7890" class="form-control input-md" id="phonenum" name="mob" type="tel"  required >
                      </div>
                     </div>
                    <div class="form-group">
                     <label class="control-label" for="dis">E-mail</label>
                      <div>                       
                      <input placeholder="xyz@example.com" class="form-control input-md" id="email" name="email" type="email"  required >
                      </div>
                    </div>
                      <div class="form-group">
                       <div class="text-right">
                       <button id="submit" name="submit" class="btn btn-info">Submit Message</button>
     </div>
                      </div>
                  </fieldset>             
                 </form> 

I want to display this alert box on the same page. It get redirect to blank page then shows alert box . If goback() removed then it redirects to blank page and does no return back to previous page. how to solve this?

You are missing something. How it can be possible to display a popup related to another page without sending any request to it?

First I have set a success session in mail.php page which is set if mail is sent successfully.

  <?php
  session_start();
  function goback()
  { 
      $_SESSION['success'] = 'true';
      header("location:index.php");
  }

  if(isset($_POST['submit'])){

    $name = $_POST['name'];
    $comment = $_POST['comment'];
    $mob = $_POST['mob'];
    $email = $_POST['email'];

    $to ='xyz@gmail.com';
    $subject= 'Contact form pharma';
    $message ="Name: ".$name."
".
              "Comment: ".$comment."
".
              "Mobile: ".$mob."
".
              "E-mail: ".$email; 
    if(mail($to, $subject, $message)){    
      goback();
    }
  else 
  {
    echo "something went wrong";
  }
  }
  ?>

Then in index.php page we just have to call this session. if its true then we can show pupop

    <?php 
if(isset($_SESSION['success']) && $_SESSION['success']=='true')
{
  //show pupop
  $_SESSION['success'] = 'false';
}
 ?>