首页 > 解决方案 > jQuery 不发送 POST 数据

问题描述

我有一个页面 quiz.php,它显示测验问题。我想使用 AJAX 发送问题类型的数据。我返回到同一页面,其中加载了一个新问题。

现在您看到的是,在提交时我创建了一个alertjust for while 编码以检查 ID 是否正确,它们是正确的。在此警报之后,我想设置 POST。现在我只是将 Id 发送到里面box1,但最后所有四个都应该在那里。

当我的页面重新加载时,我想查看使用的 ID

$test = isset($_POST['box']);  
var_dump($test);

请参阅下面的代码

<?php
  //logtime.php
  $test = isset($_POST['box']);
  var_dump($test);
  var_dump($_REQUEST);
  //rest of code that uses $uid
?>

  <form method="POST" autocomplete="off" id="form">
    <?php showPhoto($question[1], $question[0]); ?>
    <div class="row createWhitespaceBottom" id="fotoblok">
      <div class="col-sm-12 col-md-offset-3">
        <div class="input-group">
          <input type="hidden" name="questionId" class="form-control" id="questionId" value="<?php echo $question[0]; ?>">
          <input type="hidden" name="questionType" class="form-control" id="questionType" value="<?php echo $question[1]; ?>">
        </div>
      </div>
    </div>
    <div class="col-sm-3">
      <div class="dragBox" id="box1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    </div>
    <div class="col-sm-3">
      <div class="dragBox" id="box2" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    </div>
    <div class="col-sm-3">
      <div class="dragBox" id="box3" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    </div>
    <div class="col-sm-3">
      <div class="dragBox" id="box4" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    </div>
    <span class="input-group-btn createWhitespaceTop">
      <button type="submit" onclick="myFunction()"  id="antwoordblok2" class="btn btn-primary">Opslaan</button>
     </div>
    </form>
</div>
$(document).ready(function() {
  $("form").submit(function() {
    var box1 = $("#box1 :first-child").attr('id');
    var box2 = $("#box2 :first-child").attr('id');
    var box3 = $("#box3 :first-child").attr('id');
    var box4 = $("#box4 :first-child").attr('id');
    alert(box1 + " " + box2 + " " + box3 + " " + box4);
    $.ajax({
      type: 'POST',
      url: 'quiz.php',
      data: {
        box: box1
      },
    });
  });
});

标签: jqueryajax

解决方案


<button type="button" onclick="myFunction()" id="antwoordblok2" class="btn btn-primary">Opslaan</button>

将类型“提交”更改为“按钮”并将其重定向或重新加载成功。

它将异步调用 ajax,当调用成功时,您可以采取所需的操作。


推荐阅读