【问题标题】:Why form submit sends get request not post request?为什么表单提交发送获取请求而不是发布请求?
【发布时间】:2018-08-29 00:05:54
【问题描述】:

仍然有很多相同的文章,但没有一篇对我有用。 这是PHP代码。

<?php
        if($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['preview-form-comment'])){
            echo $_POST['preview-form-comment'];
            $_SESSION['currentcode']=$_POST['preview-form-comment'];
          }
    ?>

这是表单的html代码。

<form id="preview-form" method="POST">
            <textarea required class="codemirror-textarea" rows="100" name="preview-form-comment" id="preview-form-comment"><?php echo $comment ?></textarea>
            <br>
            <button type="button" id="preview-form-submit">Submit</button>
            <!-- <input type="submit" onclick="savedata();" id="preview-form-submit" value="Submit"> -->
          </form>

这是脚本。

<script>
      $('#preview-form-submit').on('click',function(){
        savedata();
        $('#preview-form').submit();
      });
    </script>

【问题讨论】:

标签: php html post get


【解决方案1】:

刚刚尝试了这段代码,它正在工作。

<?php
if($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['preview-form-comment'])){
    echo $_POST['preview-form-comment'];
    $_SESSION['currentcode']=$_POST['preview-form-comment'];
}
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
    <meta charset="utf-8">
    <title></title>
</head>
<body>
<form id="preview-form" method="POST">
    <textarea required class="codemirror-textarea" rows="3" name="preview-form-comment" id="preview-form-comment"></textarea>
    <br>
    <button type="button" id="preview-form-submit">Submit</button>
    <!-- <input type="submit" onclick="savedata();" id="preview-form-submit" value="Submit"> -->
</form>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script>
    $('#preview-form-submit').on('click',function(){
        $('#preview-form').submit();
    });
</script>
</html>

请尝试将其放入一个简单的 .php 文件中。

注意:我删除了您的 savedata(); 功能,因为您没有发布它。但是请随时将其添加到此帖子中,我将更改我的代码

【讨论】:

    猜你喜欢
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 2015-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多