我想将我在 javascript 函数内的变量中获得的内容传递给另一个 php 页面

我的包含summernote编辑器的表单:


<form class="form-group" action="upload.php" style="width: 700px;" method="post" enctype="multipart/form-data">

        <label> Title: </label>

        <input name="title" class="form-control" type="text" required placeholder="Title"/><br><br>

        <label> Header Image: </label>

        <input class="form-control" type="file" name="file" id="file"><br><br>

        <label> Body: </label><div id="summernote"></div>


        <button class="btn btn-primary" onclick="getContent()" name="submit"> Submit </button>


    </form>

获取编辑器内容的脚本:


 <script>

    $(document).ready(function() 

    {

        $('#summernote').summernote();


    });

    function getContent(){$(document).ready(function() 

    {

        var content = $('#summernote').summernote('code');

        content=document.getElementById('content').value;});

    }

保存summernote内容的php代码:


$uname= $_SESSION['id'];

$title=$_POST['title'];

$path= "uploads/".$name;

$body= ;

我试图将summernote的内容保存在另一个名为upload.php的文件中的变量$body中


泛舟湖上清波郎朗
浏览 141回答 2
2回答

慕工程0101907

而不是 div 使用 text area 。<textarea name="content" id="summernote"></textarea>形式。&nbsp; <form class="form-group" action="upload.php" style="width: 700px;" method="post" enctype="multipart/form-data">&nbsp; &nbsp; &nbsp; &nbsp; <label> Title: </label>&nbsp; &nbsp; &nbsp; &nbsp; <input name="title" class="form-control" type="text" required placeholder="Title"/><br><br>&nbsp; &nbsp; &nbsp; &nbsp; <label> Header Image: </label>&nbsp; &nbsp; &nbsp; &nbsp; <input class="form-control" type="file" name="file" id="file"><br><br>&nbsp; &nbsp; &nbsp; &nbsp; <label> Body: </label>&nbsp; &nbsp; &nbsp; &nbsp; <!--Instead of div use text area .-->&nbsp; &nbsp; &nbsp; &nbsp; <textarea name="content" id="summernote"></textarea>&nbsp; &nbsp; &nbsp; &nbsp; <button type="submit" class="btn btn-primary" name="submit"> Submit </button>&nbsp; &nbsp; </form>那么就不需要使用 getContent。只为编辑器调用summernote。&nbsp;<script>&nbsp; &nbsp; $(document).ready(function()&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $('#summernote').summernote();&nbsp; &nbsp; });&nbsp; &nbsp; </script>在upload.php中你可以通过$_POST['content']获取内容<?php$title=$_POST['title'];$path= "uploads/".$name;echo $body=$_POST['content'];?>

素胚勾勒不出你

在这方面,我并不是真正的专业人士,但我会向表单添加一个隐藏的输入并将内容 (getContent) 放在该字段中。<form class="form-group" id="theForm" action="upload.php" style="width: 700px;" method="post" enctype="multipart/form-data">&nbsp; &nbsp; <label> Title: </label>&nbsp; &nbsp; <input name="title" class="form-control" type="text" required placeholder="Title"/><br><br>&nbsp; &nbsp; <label> Header Image: </label>&nbsp; &nbsp; <input class="form-control" type="file" name="file" id="file"><br><br>&nbsp; &nbsp; <label> Body: </label><div id="summernote"></div><input id="content-catcher" type="hidden" name="contentOfEditor" />&nbsp; &nbsp; <button class="btn btn-primary" onclick="getContent()" name="submit"> Submit </button></form>脚本:function getContent(){$(document).ready(function()&nbsp;{&nbsp; &nbsp; var content = $('#summernote').summernote('code');&nbsp; &nbsp; content=document.getElementById('content').value;});&nbsp; &nbsp; $('#content-catcher').val(content);&nbsp; &nbsp; $('#theForm').submit();}PHP:$body= $_POST['contentOfEditor'];
打开App,查看更多内容
随时随地看视频慕课网APP