单击提交按钮后如何获取成功消息

时间:2019-03-28 06:22:02

标签: javascript php

我有我的代码,提交后应该在其中显示成功消息(单击添加),但是由于某种原因,即使没有添加任何内容,此成功消息也会一直显示,它只是显示在问题是如果我删除了if语句下面的消息,该消息将不会显示。动作正常,这只是成功的消息。您能看看有什么问题吗?

Add.php

<?php
include('header.php');
?>
<link rel="stylesheet" href="../../validation/dist/css/bootstrapValidator.css"/>

<script type="text/javascript" src="../../validation/dist/js/bootstrapValidator.js"></script>
  <!-- =============================================== -->
  <?php
    include('../../form.php');
    $frm=new formBuilder;
  ?>
  <!-- =============================================== -->

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1>
        Add Coming Soon Movie
      </h1>
      <?php
      if(isset($_SESSION['add']))
      {?>
      <div class="alert alert-success">
  <strong>Success!</strong> News added successfully..
</div>
<?php
}?>
      <ol class="breadcrumb">
        <li><a href="index.php"><i class="fa fa-home"></i> Home</a></li>
        <li class="active">Add Coming Soon Movie</li>
      </ol>
    </section>

    <!-- Main content -->
    <section class="content">

      <!-- Default box -->
      <div class="box">
        <div class="box-body">
            <form action="process_add_news.php" method="post" enctype="multipart/form-data" id="form1">
              <div class="form-group">
                <label class="control-label">Movie name</label>
                <input type="text" name="name" class="form-control"/>
                <?php $frm->validate("name",array("required","label"=>"Movie Name")); // Validating form using form builder written in form.php ?>
              </div>
              <div class="form-group">
                 <label class="control-label">Type</label>
                <input type="text" name="type" class="form-control">
                <?php $frm->validate("type",array("required","label"=>"Type","regexp"=>"text")); // Validating form using form builder written in form.php ?>
              </div>

              <div class="form-group">
                <label class="control-label">Release Date</label>
                <input type="date" name="date" class="form-control"/>
                <?php $frm->validate("date",array("required","label"=>"Release Date")); // Validating form using form builder written in form.php ?>
              </div>

              <div class="form-group">
                <label class="control-label">Description</label>
                 <input type="text" name="description" class="form-control">
                 <?php $frm->validate("description",array("required","label"=>"Description")); // Validating form using form builder written in form.php ?>
              </div>
              <div class="form-group">
                  <label class="control-label">Images</label>
              <input type="file"  name="attachment" class="form-control" placeholder="Images">
               <?php $frm->validate("attachment",array("required","label"=>"Image")); // Validating form using form builder written in form.php ?>
              </div>
              <div class="form-group">
                <label class="control-label">Trailer Youtube Link</label>
                <input type="text" name="video" class="form-control"/>
                <?php $frm->validate("video",array("label"=>"Image","max"=>"500")); // Validating form using form builder written in form.php ?>
              </div>
              <div class="form-group">
                <button class="btn btn-success">Add Movie</button>
              </div>
        </form>
        </div>
        <!-- /.box-footer-->
      </div>
      <!-- /.box -->

    </section>
    <!-- /.content -->
  </div>
  <?php
include('footer.php');
?>
<script>
        <?php $frm->applyvalidations("form1");?>
    </script>

processToAdd.php

<?php
    include('../../config.php');
    extract($_POST);
       $uploaddir = '../Coming-soon/';
      $uploadfile = $uploaddir . basename($_FILES['attachment']['name']);
    move_uploaded_file($_FILES['attachment']['tmp_name'],$uploadfile);
    $flname="Coming-soon/".basename($_FILES["attachment"]["name"]);
    mysqli_query($con,"INSERT INTO tbl_news values (NULL,'$name','$type','$date','$description','$flname','$video')");
    $_SESSION['add']=1;
    header('location:add_movie_news.php');
?>

3 个答案:

答案 0 :(得分:1)

我尝试了一下,发现您需要开始会话

您只需要添加session_start();

<?php
    session_start();
    if(isset($_REQUEST['submit_btn']))
    {
       $name = $_POST["names"];
       $_SESSION['add'] = $name;
       print_r($_SESSION);
    }
?>
<html>
  <head>

  </head>
  <body>
    <?php
      if(isset($_SESSION['add'])) {
    ?>
        <div class="">
            <strong>Success!</strong> News added successfully..
        </div>
    <?php
        }
    ?>
    <form action="" method="POST">
       <input type="text" name="names" id="names">
       <input type="submit" value="submit" name="submit_btn">
    </form>
    <script>

    </script>
  </body>
</html>

答案 1 :(得分:0)

问题似乎出在if(isset($_SESSION['add']))行。因此,只要设置了$_SESSION['add'],您就必须看到该消息。您可以在消息后立即将其取消设置,然后在每次单击按钮时将其重置,或者如果您单击的按钮名为“提交”,则只需使用

if(isset($_POST['add'])){
   //output message here
}

答案 2 :(得分:0)

我以您的代码为例,删除验证和其他内容

我还将Add.php重定向到processToAdd.php,然后再次将processToAdd.php重定向到Add.php

请看看并尝试一下。

希望这可以解决您的问题。

Add.php

$_GET['param1']

processToAdd.php

<?php
  session_start();
?>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
  <!-- Content Header (Page header) -->
  <section class="content-header">
    <h1>
      Add Coming Soon Movie
    </h1>
    <?php
      if(isset($_SESSION['add'])) {
    ?>
      <div class="alert alert-success">
        <strong>Success!</strong> News added successfully..
      </div>
    <?php
      }
    ?>

    </section>

    <!-- Main content -->
    <section class="content">

      <!-- Default box -->
      <div class="box">
        <div class="box-body">
            <form action="processToAdd.php" method="post" enctype="" id="form1">
              <div class="form-group">
                <label class="control-label">Movie name</label>
                <input type="text" name="name" class="form-control"/>

              </div>

              <div class="form-group">
                <button class="btn btn-success" type="submit">Add Movie</button>
              </div>
        </form>
        </div>
        <!-- /.box-footer-->
      </div>
      <!-- /.box -->

    </section>
    <!-- /.content -->
</div>