【问题标题】:Jquery drag drop form hidden value inserting into php mysqlJquery拖放表单隐藏值插入到php mysql中
【发布时间】:2013-11-07 01:05:05
【问题描述】:

我正在尝试从以下代码创建一个表单,但没有任何成功。我想输入隐藏字段以在我将可拖动项目之一放入“最喜欢我”字段时捕获值 1,当可拖动项目在“第二个最喜欢我”时,值为 2,3 表示“ “最喜欢我”第三名,“最不喜欢我”第四名。示例可以通过clicking here.查看我将如何使用以下代码创建它?我使用 PHP 和 MySQL 作为添加到数据库的参考。

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style type="text/css">
/* Add some margin to the page and set a default font and colour */

body {margin: 30px;font-family: "Georgia", serif;line-height: 1.8em;color: #333;}

/* Give headings their own font */

h1, h2, h3, h4 {
  font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
}

/* Main content area */

#content {
  margin: 80px 70px;
  text-align: center;
  -moz-user-select: none;
  -webkit-user-select: none;
  user-select: none;
}

/* Header/footer boxes */

.wideBox {
  clear: both;
  text-align: center;
  margin: 70px;
  padding: 10px;
  background: #ebedf2;
  border: 1px solid #333;
}

.wideBox h1 {
  font-weight: bold;
  margin: 20px;
  color: #666;
  font-size: 1.5em;
}

/* Slots for final card positions */

#cardSlots {
  margin: 50px auto 0 auto;
  background: #ddf;
}

/* The initial pile of unsorted cards */

#cardPile {
  margin: 0 auto;
  background: #ffd;
}

#cardSlots, #cardPile {
  width: 910px;
  height: 120px;
  padding: 20px;
  border: 2px solid #333;
  -moz-border-radius: 10px;
  -webkit-border-radius: 10px;
  border-radius: 10px;
  -moz-box-shadow: 0 0 .3em rgba(0, 0, 0, .8);
  -webkit-box-shadow: 0 0 .3em rgba(0, 0, 0, .8);
  box-shadow: 0 0 .3em rgba(0, 0, 0, .8);
}

/* Individual cards and slots */

#cardSlots div, #cardPile div {
  float: left;
  width: 150px;
  height: 78px;
  padding: 10px;
  padding-top: 40px;
  padding-bottom: 0;
  border: 2px solid #333;
  -moz-border-radius: 10px;
  -webkit-border-radius: 10px;
  border-radius: 10px;
  margin: 0 0 0 10px;
  background: #fff;
}

#cardSlots div:first-child, #cardPile div:first-child {
  margin-left: 0;
}

#cardSlots div.hovered {
  background: #aaa;
}

#cardSlots div {
  border-style: dashed;
}

#cardPile div {
  background: #666;
  color: #fff;
  font-size: 20px;
  text-shadow: 0 0 3px #000;
}

#cardPile div.ui-draggable-dragging {
  -moz-box-shadow: 0 0 .5em rgba(0, 0, 0, .8);
  -webkit-box-shadow: 0 0 .5em rgba(0, 0, 0, .8);
  box-shadow: 0 0 .5em rgba(0, 0, 0, .8);
}

</style>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js"></script>

<script type="text/javascript">

// JavaScript will go here

$( init );

function init() {

  // Create the pile of shuffled cards
  {
    $('#cardPile div').draggable( {
      containment: '#content',
      stack: '#cardPile div',
      cursor: 'move',
      revert: true
    } );
  }

  // Create the card slots
  {
    $('#cardSlots div').droppable( {
      accept: '#cardPile div',
      hoverClass: 'hovered',
      drop: handleCardDrop
    } );
  }

function handleCardDrop( event, ui ) {
    var slotNumber = $(this);
    var cardNumber = ui.draggable;

    if ( cardNumber == cardNumber ) {
        ui.draggable.addClass ( 'correct' );
        ui.draggable.draggable ( 'disable' );
        $(this).droppable( 'disable' );
        ui.draggable.position( {of: $(this), my: 'left top', at: 'left top' } );
        ui.draggable.draggable( 'option', 'revert', false );
    }
}



}

</script>
</head>

<body>

<div id="content">
  <div id="cardPile">
  <div>Controlling</div>
  <div>Motivating</div>
  <div>Realistic</div>
  <div>Organized</div>
  </div>

  <div id="cardSlots">
  <div>Most Like Me</div>
  <div>2nd Most Like Me</div>
  <div>3rd Most Like Me</div>
  <div>Least Like Me</div>
  </div>

</div>

</body>
</html>

【问题讨论】:

    标签: php jquery mysql


    【解决方案1】:

    我修改了你的函数:

    function handleCardDrop( event, ui ) {
    var slotNumber = $(this);
    var cardNumber = ui.draggable;
    var choice = cardNumber.html();
    var aux = 0;
    if (choice == 'Controlling')
    {
        aux = 1;
    }
    if (choice == 'Motivating')
    {
        aux = 2;
    }
    if (choice == 'Realistic')
    {
        aux = 3;
    }
    if (choice == 'Organized')
    {
        aux = 4;
    }
    var droped= slotNumber.html();
    
    if(droped == 'Most Like Me')
    {
        $("#uno").val(aux);
    }
    if(droped == '2nd Most Like Me')
    {
        $("#dos").val(aux);
    }
    if(droped == '3rd Most Like Me')
    {
        $("#tres").val(aux);
    }
    if(droped == 'Least Like Me')
    {
        $("#cuatro").val(aux);
    }
    if ( cardNumber == cardNumber ) {
        ui.draggable.addClass ( 'correct' );
        ui.draggable.draggable ( 'disable' );
        $(this).droppable( 'disable' );
        ui.draggable.position( {of: $(this), my: 'left top', at: 'left top' } );
        ui.draggable.draggable( 'option', 'revert', false );
    }
    }
    

    ------------------更新 2.0---------------

    我只是添加了ajax部分,并修改了fiddle:

      $("#saveResult").click(function(){
    
    var uno = $("#uno").val();  
    var dos = $("#dos").val();  
    var tres = $("#tres").val();  
    var cuatro = $("#cuatro").val();
    var params = {
    
                "uno" : uno,
    
                "dos" : dos,
    
                "tres" : tres,
    
                "cuatro": cuatro
    
        };    
     $.ajax( {
      type: "POST",
      url: 'myphpfile.php',
      data: params,
      success: function( response ) {
        $("#result").html(response);
      }
    } );
    
    }); 
    

    在文件myphpfile.php中:

    <?php
    $uno = $_POST['uno'];
    $dos = $_POST['dos'];
    $tres = $_POST['tres'];
    $cuatro = $_POST['cuatro'];
    
    //Here the code to insert in your database
    
    ?>
    

    更新的工作小提琴:http://jsfiddle.net/robertrozas/qLhke/25/

    【讨论】:

    • 您好罗伯特,感谢您的更新。不过,表单仍然没有获取值。我收到一条错误消息,指出 MySQL 列已映射到不存在的表单元素。如何将上述代码插入 MySQL 数据库?
    • 告诉我你的确切表结构,有了这些信息,我可以制作 ajax 代码部分来插入数据。
    • 表名是 DISCflex_Scores。我要填充的四个字段是 F1_1、F1_2、F1_3、F1_4。这些都被列为INT。这四个字段与作为值输入的 uno to cuatro 相关联。
    • 嗨罗伯特,它正在将表单推送到数据库,但它没有捕获任何值。我在下一条评论中按照 php 设置了表格。这就是我在 myphpfile.php 上的内容
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-09
    • 1970-01-01
    • 2016-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-21
    相关资源
    最近更新 更多