在 php 端更改 div 属性

我正在制作一个简单的 Php 和 javascript 项目,其中我的 css 设计中有一些覆盖设计。现在我有一个按钮,单击它会显示一个名为“myNav”的覆盖 div,其中有一个名为“req_form”的 div 和表单,用户可以在其中填写输入并提交它们,然后我的 php 代码将这些数据存储在我的数据库中。在我的 Php 代码中成功提交数据后,我只是不知道如何替换 div 并在其上显示成功。

我的覆盖 div

<?php

include 'includes/autoloader.inc.php';

?>


<!DOCTYPE html>

<html>

<head>

<script type="text/javascript">

  function openNav() {

    document.getElementById("myNav").style.width="100%";

  }

  function closeNav(){

    document.getElementById("myNav").style.width = "0";

  }

</script>

<link rel="stylesheet" type="text/css" href="css/cssticket.css">

</head>


<body>



<button class="button_a" onclick="openNav()">Create Request</button> //OPENS THE OVERLAY DIV


<div id="myNav" class="overlay"> // THIS IS THE OVERLAY DIV

<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>

<div class="overlay-content">


    <div id="req_form" class="inputs"> // THIS IS THE DIV I WANT TO BE REPLACE BY A MESSAGE SUCCESS

    <div id="error"></div>

    <form id="form" action="includes/enduser.inc.php" method="POST">

        <input type="text" name="userrequester" placeholder="name" >

        <br>

        <label for="reqtype">Request type:</label>

        <select name="priority" required>

            <option value="">Select</option>

            <option value="High">General</option>

            <option value="Low">Urgent</option>

        </select>

        <br>

        <label for="itemtype">Item type:</label>

        <input type="radio" name="typeitem" value="Borrowed" required><label>Borrowed</label>

        <input type="radio" name="typeitem" value="Replace" required></input><label>Replace</label>

        <br>

        <label>Summary :</label>

        <br>

        <textarea name="summary" cols="30" rows="10" required ></textarea>

        <br>

        <button type="submit" name="sendrequest" class="button_a">Submit</button>

        </div>

    </form>

    </div>

    </div>


       </body>

    </html>



我已经尝试过的是在将数据存储在此 php 文件中之后回显一些应该将其更改为成功消息的 javascript。


喵喵时光机
浏览 182回答 1
1回答

摇曳的蔷薇

一旦您成功查询并将输入成功添加到数据库,您将需要创建一个 header() 重定向到您的原始页面。像这样的东西:if (isset($_POST['sendrequest'])) {&nbsp; &nbsp; $date = date ('F d, Y');&nbsp; &nbsp; $enduser =&nbsp; $_POST['userrequester'];&nbsp; &nbsp; $priority = $_POST["priority"];&nbsp; &nbsp; $itemtype = $_POST["typeitem"];&nbsp; &nbsp; $summary = $_POST["summary"];&nbsp; &nbsp; $status = "new";&nbsp; &nbsp; // Pretty sure this can be wrapped in your if statement, may need to test that.&nbsp; &nbsp; // --> $request->createticket($enduser, $priority, $itemtype, $status, $summary, $date);&nbsp;&nbsp; &nbsp; if($request->createticket($enduser, $priority, $itemtype, $status, $summary, $date)){&nbsp; &nbsp; &nbsp; &nbsp; $postMSG = "success"; // sending the success message over url as $_GET&nbsp; &nbsp; &nbsp; &nbsp; $host&nbsp; = $_SERVER['HTTP_HOST']; // SERVER&nbsp; &nbsp; &nbsp; &nbsp; $uri&nbsp; &nbsp;= rtrim(dirname($_SERVER['PHP_SELF']), '/\\'); // Directory&nbsp; &nbsp; &nbsp; &nbsp; $extra = 'ticketformpage.php'; // the page your form is on&nbsp; &nbsp; &nbsp; &nbsp; header("Location: http://$host$uri/$extra?$postMSG"); // header redirect with url post added&nbsp; &nbsp; }现在在您希望显示成功消息的页面上,我们检查 GET 全局是否设置为成功$_GET['success'],如果成功则我们设置变量并显示它们并添加一些 css。<?php&nbsp;$msg = NULL; // we set to NULL for when the message is not neededif(isset($_GET['success'])){&nbsp; $msg = "Thank you for submitting through our ticket system.";}else{&nbsp; $msg = NULL;}注意:我在<span>标签中添加了成功并添加了padding和border radius,limegreen bg和darkgreen颜色以与成功相关联。10px margin-top for form。&nbsp;<div id="req_form" class="inputs">&nbsp; &nbsp; <span class="success"><?=$msg?></span> <!--// add the success variable here-->&nbsp; &nbsp; <div id="error"></div>&nbsp; &nbsp; <form id="form" action="inputtest.php" method="POST">CSS:form {&nbsp; margin-top: 10px;}.success {&nbsp; &nbsp;background-color: limegreen;&nbsp; &nbsp;color: darkgreen;&nbsp; &nbsp;padding: 10px;&nbsp; &nbsp;border-radius: 5px;}
打开App,查看更多内容
随时随地看视频慕课网APP