jQuery拖放表格隐藏值插入PHP MySQL MySQL中 [英] Jquery drag drop form hidden value inserting into php mysql

查看:51
本文介绍了jQuery拖放表格隐藏值插入PHP MySQL MySQL中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过以下代码创建表单,但没有成功.我想输入隐藏字段,以在将其中一个可拖动项放入最喜欢我"字段时捕获值为1,当可拖动项在第二个最喜欢我"中时值为2,为"最喜欢我"排名第3,最不喜欢我"排名第4.可以通过单击此处查看.如何使用以下代码创建示例?我使用PHP和MySQL作为添加到数据库的参考.

I'm trying to create a form from the following code, but not having any success. I want to input hidden fields to capture a value of 1 when I put one of the draggable items into the "Most Like Me" field, a value of 2 when the draggable item is in the "2nd Most Like Me", 3 for "3rd Most Like Me", and 4 for "Least Like Me". Sample can be viewed by clicking here. How would I create this using the following code? I use PHP and MySQL as reference for adding to the database.

<!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>

推荐答案

我修改了您的函数:

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 );
}
}

------------------ UPDATE 2.0 ---------------------------

------------------UPDATE 2.0---------------------------

我只添加了ajax部分,并修改了小提琴:

I just add the ajax part, and modified the 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中:

In the file 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/

这篇关于jQuery拖放表格隐藏值插入PHP MySQL MySQL中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆