【问题标题】:Can't get post data from jQuery UI sortable (sent to the same file)无法从可排序的 jQuery UI 获取发布数据(发送到同一文件)
【发布时间】:2015-09-16 08:08:25
【问题描述】:

我有一个用 php 编写的小测验,有两种类型的问题:通过单选按钮的单选答案和通过复选框的多选答案。代码在单个文件(“quiz.php”)中运行,该文件处理问题的显示以及答案的处理。

一切都很顺利,直到我不得不为答案的“可排序列表”添加第三种类型——人们必须重新排序选择列表。

经过一番研究,我找到了我认为的答案:jQuery UI 的“可排序”交互。简单、直接且易于实施……即使我似乎无法让它发挥作用!

我的问题是,如果我对 PHP/MySql 有一点了解,我对 Javascript 的了解就很少,当然也没有足够的知识来正确编写或调试它。

这是代码,从所有用于显示问题和答案过程的 php 开始:

// File quiz.php
<?php session_start();

// if the form has just been submitted, handle the answers
if (isset($_POST['submit'])) {

// Do something, on a case by case basis :
    switch ($_SESSION['questionType']) {
    case 1 : // for radio buttons
        // some code to process and save the answer to the database
    break ;
    case 2 : // for checkboxes
        // some code to process and save the answer to the database
    break ;
    case 3 : // for sortable lists
        // some code to process and save the answer to the database
    break ;
    }
  }
// Here, get the elements to show from the DB, including
// the question type for the switch below 
// questionType = 1 for single choice answer
// questionType = 2 for multiple choice answer
// questionType = 3 for sortable choice answer
?>

接下来是显示问题的html:

<!DOCTYPE html> 
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1"> 
  <title>Quiz</title> 
<link rel="stylesheet" href=./quiz.css">
   <script src="./js/jquery-1.11.3.min.js"></script>
   <script src="./js/jquery-ui.min.js"></script>
</head>
<body> 
    <?php // A bunch of code to echo the question ?>

    <form action="quiz.php" method="POST">
 <?php  // A switch to adapt the display to the type of questions :
    switch ($_SESSION['questionType']) {
        case 1 : // for single choice answer
            // A bunch of code to echo the possible choices w/ radio buttons
        break ;
        case 2 : // for multiple choice answer
            // A bunch of code to echo the possible choices w/ checkboxes
        break ;
        case 3 : // for sortable lists ?>
    <!-- the following would normaly be generated via php with info from the DB  -->
    <ul id="sort">
      <li id="choice_1">Item 1</li>
      <li id="choice_2">Item 2</li>
      <li id="choice_3">Item 3</li>
      <li id="choice_4">Item 4</li>
      <li id="choice_5">Item 5</li>
    </ul>
<?php   break ; ?>
<?php  } ?>
    <input type="submit" name="submit" value="Submit">
    </form>

这就是它崩溃的地方

 <script type="text/javascript">
  $(function() {
    $("#sort").sortable({
      var newOrder = $(this).sortable('serialize'); 
      // the 'quiz.php' file called below is the one we're in
      $.post('quiz.php', {sort: newOrder };
    });
    $("#sort").disableSelection();
  });
    </script>
    </body>
</html>

让我难过的是,不仅在提交表单后结果没有发送到 $_POST ——所以什么也没有发生——而且 $("#sort").sortable( ) 函数完全破坏了交互行为,即列表不可排序并且可以选择文本。

任何提示,帮助?

[编辑]:感谢 Jordan,我已经运行了可排序部分,但数据仍未发送。

现在使用的代码:

  <script type="text/javascript">
   $(function() {
    $('#sort-form').on('submit',function(event){
        // Prevent the form from being submitted normally
        event.preventDefault();
        var newOrder = $(this).sortable('serialize');
        $.post('quiz.php', {sort: newOrder };
    }); 
 </script>

不会产生任何结果,Chrome 中的控制台现在说在结束标记之前有一个“未捕获的语法错误:输入的意外结束”。 我不知道它是否相关,但它让我认为 要么我在某处遗漏了一个错字,要么该函数需要更多参数......

[编辑 2] 将 &lt;li&gt; id 上的“-”替换为下划线(更符合 jQuery UI 指南)

【问题讨论】:

  • 嘿顺便说一句,您在最后一个之后又错过了另一个 });,这就是为什么您会收到意外的输入错误结束

标签: javascript php jquery jquery-ui post


【解决方案1】:

序列化和 ajax 调用在 sortable 函数中不起作用,因为您试图将它们定义为可排序组件本身的选项。您应该在按下按钮时进行监听,然后获取序列化并调用 ajax 函数

所以你可以在表单中添加一个id

<form action="quiz.php" method="POST" id="sort-form">

然后在 jQuery 中监听表单何时提交

$('#sort-form').on('submit',function(event){
    // Prevent the form from being submitted normally
    event.preventDefault();
    var newOrder = $('#sort').sortable('serialize', {
        key: 'sort'
    });
    $.post('quiz.php', newOrder);
});

现在当您调用可排序组件时,只需像这样调用它

$("#sort").sortable();
$("#sort").disableSelection();

更新

如果您不想使用 Ajax 提交表单数据,您可以尝试设置隐藏输入以将值设置为序列化排序数据,类似这样

$('#sort-form').on('submit',function(event){
    // Prevent the form from being submitted normally
    event.preventDefault();
    var newOrder = $('#sort').sortable('serialize', {
        key: 'sort'
    });
    $('.hidden-input-class').val(newOrder);
    // Submit the form like normal
    $(this).submit();
});

【讨论】:

  • 好的,谢谢您的信息。不过,让我哑口无言,因为我不知道将答案的“听 jQuery”部分放在哪里:在与可排序组件或其他地方相同的 $(function(){} 中?
  • 是的,$(function){} 部分是所有 jQuery 都应该进入的包装器。我建议您在阅读过程中阅读一些 jQuery 基础知识,它将帮助您了解正在发生的事情以及如何设置 jQuery 插件等
  • 好的,我已经阅读了一些关于如何以及在何处放置有关 jQuery 的内容,现在这部分似乎还可以。问题是数据仍然没有发送:print_r($_POST) 返回一个只有[submit] =&gt; Submit 的数组,没有别的。
  • 我更新了答案,我查找了可排序函数的正确 API 并相应地更新了它。我也有这个记录序列化数据的 jsbin,这样你就可以看到你应该看到的内容。 jsbin.com/qozibov/edit?js,console,output
  • 再次感谢,但仍然没有骰子......我在最后发现了一个丢失的});,所以 SyntaxError 现在已经消失了,但唯一的其他影响——无论你的代码版本是什么——是现在提交似乎根本没有触发:不仅 $_POST 数组是空白的,而且可排序列表保持在新顺序中,这让我相信页面没有重新加载。
【解决方案2】:

好吧,我还是把它删除了,因为我不应该把它放在这里。我想应该多考虑一下。我相信这是相关的,但可能根本不清楚。但是无论如何,为了避免在我的服务器上发生更多奇怪的事情,我删除了它,所以考虑到它从未说过。祝你好运,希望你能尽快得到答案。

【讨论】:

  • (??) 很抱歉,但我认为您一定回答错了问题,因为我看不出您的意见与手头问题的相关性 (??)
  • 无论如何,谢谢,但这确实是用锤子拍苍蝇的情况,我担心:你的回答远远超出了我的需要(以及我的水平和薪酬等级,现在我想关于它)。再次感谢您抽出宝贵时间。
  • mm 也许我也应该读得更好一些。我看到了序列化和发布方法,我认为您在使用 json 对象从数据库中获取对象时遇到问题。我使用基于 api 的模型,所以我猜这种方法是不同的。
猜你喜欢
  • 1970-01-01
  • 2018-12-31
  • 1970-01-01
  • 2015-11-24
  • 2013-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多