为了账号安全,请及时绑定邮箱和手机立即绑定

将用户重定向到页面后如何在输入和tinymce textarea中回显会话变量

将用户重定向到页面后如何在输入和tinymce textarea中回显会话变量

PHP
慕尼黑8549860 2022-07-29 12:27:14
我正在尝试处理表格。如果有错误,用户将被重定向到表单,错误将在此处显示,用户尝试提交的任何内容都会在表单的 tinymce 的 textarea 中回显。这样,用户就不必再次重写所有内容。一切都运行良好,除了用户被重定向后会话变量不会在 tinymce 编辑器中回显。会话变量已设置并具有正确的值。如果我在 tinymce 之外回显会话变量,它会按预期显示。它只是不会出现在文本区域中。我该如何解决?另外,我知道这很容易受到 xss 的影响。我想让用户格式化他们的帖子,所以稍后我将通过 HTML Purifier 运行它。添加新线程.php:<?php session_start(); if($_SERVER['REQUEST_METHOD']==='POST'){   if(isset($_POST['submit'])&&$_POST['submit']==='success'){    if (empty(trim($_POST['thread-title'])))    {        $_SESSION['forum_titErr'] = "<p class='error text-center'>Error message</p>";    }    else    {        $_SESSION['threadTitle'] = $_POST['thread-title'];     }    if (empty(trim($_POST['thread-content'])))    {        $_SESSION['forum_thrContErr'] = "<p class='error text-center'>Error message </p>";     }    else    {        $_SESSION['threadCont'] = $_POST['thread-content'];    }    if((isset($_SESSION['forum_titErr'])&&!empty($_SESSION['forum_titErr']))|| (isset($_SESSION['forum_thrContErr'])&&!empty($_SESSION['forum_thrContErr'])))    {        header("Location: newthread.php?submit=error");     }    else     {    //insert into database and redirect to readtopic.php if insert is successful; else redirect to form and show insert is not successful     }}else{    header("Location: newthread.php");}}else{      exit('invalid request');}?>表单html:<!DOCTYPE html><html><?php       session_start(); ?><head>    <!-- title, meta, stylesheet, etc. -->     <script type="text/javascript" src="jquery.js"></script>    <script src="https://cdn.tiny.cloud/1/no-api-key/tinymce/5/tinymce.min.js" referrerpolicy="origin"></script>    <script>tinymce.init({selector:"#threadCont", height: 300, resize: false    });</script></head>
查看完整描述

3 回答

?
繁星点点滴滴

TA贡献1803条经验 获得超3个赞

我认为你需要这样做


 <form action='addnewthread.php' method='post'>

      <input type='text' name='thread-title' id='thread-title' placeholder='Type title here' class='user-input'

           <?php 

           if(isset($_SESSION['threadTitle']) && !empty($_SESSION['threadTitle']))

           {

                echo "value='{$_SESSION['threadTitle']}'"; 

           }

           ?>

      >

      <textarea id='threadCont' name='threadCont'>

      <?php 

            if(isset($_SESSION['threadCont']) && !empty($_SESSION['threadCont']))

            {

                 echo $_SESSION['threadCont'];

            }

      ?> 

      </textarea>

      <button id='submit' type='submit' name='submit'value='success'>Submit</button>

</form>


查看完整回答
反对 回复 2022-07-29
?
拉风的咖菲猫

TA贡献1995条经验 获得超2个赞

只需回显变量,只需注意"and'因为字符串连接。


对于输入:


 <input type='text' name='thread-title' id='thread-title' placeholder='Type title here' class='user-input'

 <?php 

    if(isset($_SESSION['threadTitle'])&!empty($_SESSION['threadTitle'])

     {

     echo "value='".$_SESSION['threadTitle']."'"; 

  }

  ?>

  />

对于文本区域:


  <textarea id='threadCont' name='threadCont'>

  <?php 

  if(isset($_SESSION['threadCont'])&!empty($_SESSION['threadCont'])

  {

   echo $_SESSION['threadTitle'];

  }

  ?> 

  </textarea>


查看完整回答
反对 回复 2022-07-29
?
侃侃尔雅

TA贡献1801条经验 获得超15个赞

我检查了您的代码,发现您忘记添加圆括号:


<form action='addnewthread.php' method='post'>

    <input type='text' name='thread-title' id='thread-title' placeholder='Type title here' class='user-input'

        <?php

        if(isset($_SESSION['threadTitle'])&&!empty($_SESSION['threadTitle']) <--here

        {

            echo "value='{$_SESSION['threadTitle']}'";

        }

        ?>

    >

    <textarea id='threadCont' name='threadCont'>

          <?php

          if(isset($_SESSION['threadCont'])&&!empty($_SESSION['threadCont']) <--and here

          {

              echo $_SESSION['threadCont'];

          }

          ?>

          </textarea>

    <button id='submit' type='submit' name='submit'value='success'>Submit</button>

</form>

您还需要替换&为&&.


查看完整回答
反对 回复 2022-07-29
  • 3 回答
  • 0 关注
  • 147 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信