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

时间:2018-08-07 08:40:51

标签: php

    <?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."\n".
            "Comment: ".$comment."\n".
            "Mobile: ".$mob."\n".
            "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,我们有一个表单请求回调,为此我们使用html表单并发送邮件功能

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

我要在同一页面上显示此警报框。它被重定向到空白页,然后显示警告框。如果goback()已删除,则它将重定向到空白页面,并且不会返回到上一页。 该如何解决?

1 个答案:

答案 0 :(得分:0)

您缺少什么。如何在不发送任何请求的情况下显示与另一个页面相关的弹出窗口?

首先,我在success页中设置了一个mail.php会话,如果成功发送邮件,则会进行设置。

  <?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."\n".
              "Comment: ".$comment."\n".
              "Mobile: ".$mob."\n".
              "E-mail: ".$email; 
    if(mail($to, $subject, $message)){    
      goback();
    }
  else 
  {
    echo "something went wrong";
  }
  }
  ?>

然后在index.php页中,我们只需要调用此会话即可。如果它的true,那么我们可以显示pupop

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