jQuery不发送POST数据

时间:2019-01-09 10:13:34

标签: jquery ajax

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

现在您看到的是,在提交时,我创建了alert,仅用于编码以检查ID是否正确。收到此警报后,我要设置POST。现在,我只是在box1内发送ID,但是所有这四个ID都应该最后存储在那。

页面重新加载时,我想使用

查看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
      },
    });
  });
});

2 个答案:

答案 0 :(得分:0)

这是因为您没有从表单的默认行为中停止表单。将您的jQuery更改为此:

$('form').submit(function(e)
{
    e.preventDefault(); //this will stop the form submitting and let you do your ajax
    //etc.
});

表单属性action在默认情况下是一个空字符串,它指向发送它的页面。因此,您是将表单发布到自身,甚至不去处理ajax请求。

查看此处:

event.preventDefault

form (scroll to action)

答案 1 :(得分:0)

df.groupby('iD').apply(lambda x: x.loc[(x.b == 'E').idxmax():,:])
                .reset_index(drop=True)

   iD  a  b  c
0  c1  2  E  4
1  c1  2  3  4
2  c2  3  E  5
3  c2  3  4  5

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

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

相关问题