dti3914 2017-05-18 22:21
浏览 74

如何在PHP永久网页内容中返回$ _POST

I have a form that I can post. I also have a sql database that it has a successful connection with. However, when I close the page out, the user input disappears. How can I make the user input part of the page content,almost like a guestbook kind of idea?

<p onclick="myFunction()">Click here to share your personal testimony</p>
<div id="formwindow">
    <form action="http://needjesuskneadjesus.org/perstest.php" method="post">
        Name: <input type="text" name="name">
        <span class="error">* <?php echo $nameErr; ?></span>
        <br>
        Email: <input type="text" name="email">
        <span class="error">* <?php echo $emailErr; ?></span>
        <br>
        Personal Testimony:<br> <textarea name="personalTestimony" rows="10" cols="50"></textarea><br>
        <input type="Submit">
    </form>
</div>
<script>
    function myFunction() {
        document.getElementById("formwindow").style.display = "block";
    }
</script>
</br>
<?php
    echo "Name: " . $_POST['name'];
?>
</br>
<?php
    echo "Email: " . $_POST['email'];
?>
</br>
<?php
    echo "Personal Testimony: " . $_POST['personalTestimony'];
?>
</br>
/* Attempt MySQL server connection. 
    // Check connection
    if($link === false){
        die("ERROR: Could not connect. " . mysqli_connect_error());
    }
    // Escape user inputs for security
    $name = mysqli_real_escape_string($link, $_REQUEST['name']);
    $email = mysqli_real_escape_string($link, $_REQUEST['email']);
    $personalTestimony = mysqli_real_escape_string($link, 
    $_REQUEST['personalTestimony']);
    // attempt insert query execution
    $sql = "INSERT INTO personalTestimony (name, email, testimony) VALUES 
    ('$name', '$email', '$personalTestimony')";
    if(mysqli_query($link, $sql)){
        echo "Thanks for sharing your personal testimony.";
    } else{
        echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    }
    // close connection
    mysqli_close($link);
*/
?>
  • 写回答

1条回答 默认 最新

  • dps43633 2017-05-18 23:00
    关注

    You can use PHP Sessions to store the users review, this will display the old one

    <?php
    session_start();
    
    $name;
    $email;
    $personalTestimony;
    
    if($link === false){
        die('ERROR: Could not connect.' . mysqli_connect_error());
    }
    
    
    
    if (!isset($_POST['name']) && !isset($_POST['email']) && !isset($_POST['personalTestimony'])) {
        $name = $_POST['name']);
        $email = $_POST['email'];
        $personalTestimony = $_POST['personalTestimony']);
    
    
        // attempt insert query execution
        $sql = mysqli_prepare($link, "INSERT INTO personalTestimony (name, email, testimony) VALUES ('$name', '$email', '$personalTestimony'))";
    
        if(mysqli_query($link, $sql)){
            echo 'Thanks for sharing your personal testimony.';
        } else{
          echo 'ERROR: Could not able to execute $sql. ' . mysqli_error($link);
        }
    
    } elseif (!empty($_SESSION['name']) || !empty($_SESSION['email']) || !empty($_SESSION['testimony'])) {
            $_SESSION['name'] = $name;
            $_SESSION['email'] = $email;
            $_SESSION['testimony'] = $personalTestimony;
        }
    
    }
    
    // close connection
    mysqli_close($link);
    ?>
    

    I replaced mysqli_real_escape_string to mysqli_prepare since it's less characters and provides more safety. You can read more about it here.

    This will only work until ether the session expires (You can configure this here) or the client clears their cookies.

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度