gpt4 book ai didi

php - Jquery拖放表单隐藏值插入php mysql

转载 作者:行者123 更新时间:2023-11-30 01:04:17 24 4
gpt4 key购买 nike

我尝试使用以下代码创建表单,但没有成功。当我将可拖动项目之一放入“最喜欢我”字段时,我想输入隐藏字段以捕获值 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>

最佳答案

我修改了你的函数:

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

?>

更新的工作 fiddle :http://jsfiddle.net/robertrozas/qLhke/25/

关于php - Jquery拖放表单隐藏值插入php mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19826075/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com