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

我正在尝试处理表格。如果有错误,用户将被重定向到表单,错误将在此处显示,用户尝试提交的任何内容都会在表单的 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>


慕尼黑8549860
浏览 178回答 3
3回答

繁星点点滴滴

我认为你需要这样做&nbsp;<form action='addnewthread.php' method='post'>&nbsp; &nbsp; &nbsp; <input type='text' name='thread-title' id='thread-title' placeholder='Type title here' class='user-input'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<?php&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(isset($_SESSION['threadTitle']) && !empty($_SESSION['threadTitle']))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "value='{$_SESSION['threadTitle']}'";&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;?>&nbsp; &nbsp; &nbsp; >&nbsp; &nbsp; &nbsp; <textarea id='threadCont' name='threadCont'>&nbsp; &nbsp; &nbsp; <?php&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(isset($_SESSION['threadCont']) && !empty($_SESSION['threadCont']))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo $_SESSION['threadCont'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; ?>&nbsp;&nbsp; &nbsp; &nbsp; </textarea>&nbsp; &nbsp; &nbsp; <button id='submit' type='submit' name='submit'value='success'>Submit</button></form>

拉风的咖菲猫

只需回显变量,只需注意"and'因为字符串连接。对于输入:&nbsp;<input type='text' name='thread-title' id='thread-title' placeholder='Type title here' class='user-input'&nbsp;<?php&nbsp;&nbsp; &nbsp; if(isset($_SESSION['threadTitle'])&!empty($_SESSION['threadTitle'])&nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp;echo "value='".$_SESSION['threadTitle']."'";&nbsp;&nbsp; }&nbsp; ?>&nbsp; />对于文本区域:&nbsp; <textarea id='threadCont' name='threadCont'>&nbsp; <?php&nbsp;&nbsp; if(isset($_SESSION['threadCont'])&!empty($_SESSION['threadCont'])&nbsp; {&nbsp; &nbsp;echo $_SESSION['threadTitle'];&nbsp; }&nbsp; ?>&nbsp;&nbsp; </textarea>

侃侃尔雅

我检查了您的代码,发现您忘记添加圆括号:<form action='addnewthread.php' method='post'>&nbsp; &nbsp; <input type='text' name='thread-title' id='thread-title' placeholder='Type title here' class='user-input'&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; if(isset($_SESSION['threadTitle'])&&!empty($_SESSION['threadTitle']) <--here&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "value='{$_SESSION['threadTitle']}'";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; >&nbsp; &nbsp; <textarea id='threadCont' name='threadCont'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(isset($_SESSION['threadCont'])&&!empty($_SESSION['threadCont']) <--and here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $_SESSION['threadCont'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </textarea>&nbsp; &nbsp; <button id='submit' type='submit' name='submit'value='success'>Submit</button></form>您还需要替换&为&&.
打开App,查看更多内容
随时随地看视频慕课网APP