2016-04-22 158 views
-1

我試圖保存HTML5 drap並將div放到數據庫中,出現當前狀態。 this shows what I am trying to save保存HTML5拖放元素到db

正如您從圖像中看到的那樣,頁面的左側是將被拖入網格的數據,這是我試圖保存的數據。

目前我可以將項目從左側拖動到特定位置的頁面中心,但是我不知道如何將這些數據保存在它所處的狀態,我正在使用HTMl5並寫了一個連接到數據庫的PHP頁面,

我在網上做過研究,發現我可能需要jQuery,我沒有理解。

在圖片中,您可以看到我已將數據拖動到頁面上的特定位置。

以下是html5源代碼。有人可以幫忙嗎?

<!DOCTYPE html> 
 
<html lang="en"> 
 

 
<head> 
 
    <title>Create a new plan</title> 
 

 
    <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet"> 
 

 
    <?php // redirecting any web user who hasn't logged in so that the page cannot be accessed from the url 
 
\t \t \t session_start(); 
 
\t \t \t 
 
\t \t \t if(!isset($_SESSION['sess_user '])) { 
 
\t \t \t \t header("location:login.php"); 
 
\t \t } \t 
 
\t \t ?> 
 
\t \t 
 
\t \t <!-- this welcomes the session user and gives them the option to log out --> 
 
\t \t <h3>Welcome, <?=$_SESSION['sess_user '];?>! <a href="logout.php">Logout</a></h3> 
 

 
\t <script> 
 
function allowDrop(ev) { 
 
    ev.preventDefault(); 
 
} 
 

 
function drag(ev) { 
 
    ev.dataTransfer.setData("text", ev.target.id); 
 
\t document.getElementById('StudentImageID ').innerHTML = ev.target.id; 
 
\t document.getElementById('StudentName ').innerHTML = ev.target.name; 
 
} 
 

 
function drop(ev) { 
 
    ev.preventDefault(); 
 
    var data = ev.dataTransfer.getData("text"); 
 
    ev.target.appendChild(document.getElementById(data)); 
 
} 
 

 
\t \t \t \t 
 
</script> 
 
\t 
 
    </head> 
 
    <body> 
 
\t <nav class="navbar navbar-default"> 
 
    <div class="container-fluid"> 
 
    <!-- Brand and toggle get grouped for better mobile display --> 
 
    <div class="navbar-header"> 
 

 
     <a class="navbar-brand" href="#">DNP Seating Solutions</a> 
 
\t <ul class="nav navbar-nav navbar-right"> 
 
     <li><a href="LoggedOut.html">Log Out</a></li> 
 
     </ul> 
 
\t </div> 
 
    </div> 
 
    </nav> 
 
    <br> 
 
\t <!--<div id="col-sm-2" ondrop="drop(event)" ondragover="allowDrop(event)"></div> 
 
\t <img id="student" src="student.jpg" draggable="true" ondragstart="drag(event)" width="100" height="100">--> 
 
<br> 
 

 
<div class= "col-md-3 well" style="float: left"> 
 
\t <center><p>Students</p></center> 
 
    <img id="OsamaSaeed" data-student-type="Osama Saeed" src="StudentImages/One.jpg" draggable="true" ondragstart="drag(event)" width="120" height="20"> 
 
    <img id="ThomasAli" data-student-type=" Thomas Ali" src="StudentImages/Two.jpg" draggable="true" ondragstart="drag(event)" width="120" height="20"> 
 
    <img id="AliMohammed" data-student-type="Ali Mohammed" src="StudentImages/Three.jpg" draggable="true" ondragstart="drag(event)" width="120" height="20"> 
 
    <img id="LaurenDent" data-student-type="Lauren Dent" src="StudentImages/Four.jpg" draggable="true" ondragstart="drag(event)" width="120" height="20"> 
 
    <img id="QuintonDial" data-student-type="Quntion Dial" src="StudentImages/Five.jpg" draggable="true" ondragstart="drag(event)" width="120" height="20"> 
 
    <img id="AdeebAli" data-student-type="Adeeb Ali" src="StudentImages/Six.jpg" draggable="true" ondragstart="drag(event)" width="120" height="20"> 
 
    <img id="BabsFab" data-student-type="Babs Fab" src="StudentImages/Seven.jpg" draggable="true" ondragstart="drag(event)" width="120" height="20"> 
 
    <img id="ArtmanSummer" data-student-type="Artman Summer" src="StudentImages/Eight.jpg" draggable="true" ondragstart="drag(event)" width="120" height="20"> 
 
    <img id="BykowskiDerek" data-student-type="Bykowski Derek" src="StudentImages/Nine.jpg" draggable="true" ondragstart="drag(event)" width="120" height="20"> 
 
    <img id="AlisonRose" data-student-type="Alison Rose" src="StudentImages/Ten.jpg" draggable="true" ondragstart="drag(event)" width="120" height="20"> 
 
    <img id="GraySonny" data-student-type="Gray Sonny" src="StudentImages/Eleven.jpg" draggable="true" ondragstart="drag(event)" width="120" height="20"> 
 
    <img id="GregersonLuke" data-student-type="Gregerson Luke" src="StudentImages/Twelve.jpg" draggable="true" ondragstart="drag(event)" width="120" height="20"> 
 
    <img id="AjayGriffin" data-student-type="Ajay Griffin" src="StudentImages/Thirteen.jpg" draggable="true" ondragstart="drag(event)" width="120" height="20"> 
 
    <img id="JohnsonJinny" data-student-type="Johnson Jinny" src="StudentImages/Fourteen.jpg" draggable="true" ondragstart="drag(event)" width="120" height="20"> 
 
    <img id="KazmirScoot" data-student-type="Kazmir Scoot" src="StudentImages/Fifteen.jpg" draggable="true" ondragstart="drag(event)" width="120" height="20"> 
 
    <img id="LattimoreCorey" data-student-type="Lattimore Corey" src="StudentImages/Sixteen.jpg" draggable="true" ondragstart="drag(event)" width="120" height="20"> 
 
    <img id="LeonLeon" data-student-type="Leon Leon" src="StudentImages/Seventeen.jpg" draggable="true" ondragstart="drag(event)" width="120" height="20"> 
 
    <img id="MastersonSoph" data-student-type="Masterson Soph" src="StudentImages/Eighteen.jpg" draggable="true" ondragstart="drag(event)" width="120" height="20"> 
 
    <img id="MurieJenna" data-student-type="Mure Jenna" src="StudentImages/Nineteen.jpg" draggable="true" ondragstart="drag(event)" width="120" height="20"> 
 
    <img id="TomLawrence" data-student-type="Tom Lawrence" src="StudentImages/Twenty.jpg" draggable="true" ondragstart="drag(event)" width="120" height="20"> 
 
    <img id="TrouncAmanda" data-student-type="Trounc Amanda" src="StudentImages/Twentyone.jpg" draggable="true" ondragstart="drag(event)" width="120" height="20"> 
 
    <img id="LittleJohn" data-student-type="Little John" src="StudentImages/Twentytwo.jpg" draggable="true" ondragstart="drag(event)" width="120" height="20"> 
 
    <img id="FrancisStacey" data-student-type="Francis Stacey" src="StudentImages/Twentythree.jpg" draggable="true" ondragstart="drag(event)" width="120" height="20"> 
 
    <img id="DooliteSean" data-student-type="Doolite Sean" src="StudentImages/Twentyfour.jpg" draggable="true" ondragstart="drag(event)" width="120" height="20"> 
 
    <img id="DialQuinton" data-student-type="Dial Quinton" src="StudentImages/Twentyfive.jpg" draggable="true" ondragstart="drag(event)" width="120" height="20"> 
 
    <img id="DentLauren" data-student-type="Dent Lauren" src="StudentImages/Twentysix.jpg" draggable="true" ondragstart="drag(event)" width="120" height="20"> 
 
    <img id="CookeyPamela" data-student-type="Cookey Pamela" src="StudentImages/Twentyseven.jpg" draggable="true" ondragstart="drag(event)" width="120" height="20"> 
 
    <img id="CathNory" data-student-type="Cath Nory" src="StudentImages/Twentyeight.jpg" draggable="true" ondragstart="drag(event)" width="120" height="20"> 
 
    <img id="NorhAhmd" data-student-type="Norh Ahmd" src="StudentImages/Twentynine.jpg" draggable="true" ondragstart="drag(event)" width="120" height="20"> 
 
    <img id="OmarAli" data-student-type="Omar Ali" src="StudentImages/Thirty.jpg" draggable="true" ondragstart="drag(event)" width="120" height="20"> 
 
    </div> 
 

 
<div class="container well" style="float: right; width: 70%;"> 
 

 
<div class="row"> 
 
    <div class="col-sm-2" id="S1" style="background-color:lavender;" ondrop="drop(event)" ondragover="allowDrop(event)">1</div> 
 
    <div class="col-sm-2" id="S2" style="background-color:lavenderblush;" ondrop="drop(event)" ondragover="allowDrop(event)">2</div> 
 
    <div class="col-sm-2" id="S3" style="background-color:lavender;" ondrop="drop(event)" ondragover="allowDrop(event)">3</div> 
 
\t <div class="col-sm-2" id="S4" style="background-color:lavenderblush;" ondrop="drop(event)" ondragover="allowDrop(event)">4</div> 
 
    <div class="col-sm-2" id="S5" style="background-color:lavender;" ondrop="drop(event)" ondragover="allowDrop(event)">5</div> 
 
    </div> 
 
    </center> 
 
    <br> 
 
<div class="row"> 
 
    <div class="col-sm-2" id="S6" style="background-color:lavender;" ondrop="drop(event)" ondragover="allowDrop(event)">6</div> 
 
    <div class="col-sm-2" id="S7" style="background-color:lavenderblush;" ondrop="drop(event)" ondragover="allowDrop(event)">7</div> 
 
    <div class="col-sm-2" id="S8" style="background-color:lavender;" ondrop="drop(event)" ondragover="allowDrop(event)">8</div> 
 
\t <div class="col-sm-2" id="S9" style="background-color:lavenderblush;" ondrop="drop(event)" ondragover="allowDrop(event)">9</div> 
 
    <div class="col-sm-2" id="S10" style="background-color:lavender;" ondrop="drop(event)" ondragover="allowDrop(event)">10</div> 
 
    </div> 
 
    <br> 
 

 
<div class="row"> 
 
    <div class="col-sm-2" id="S11" style="background-color:lavender;" ondrop="drop(event)" ondragover="allowDrop(event)">11</div> 
 
    <div class="col-sm-2" id="S12" style="background-color:lavenderblush;" ondrop="drop(event)" ondragover="allowDrop(event)">12</div> 
 
    <div class="col-sm-2" id="S13" style="background-color:lavender;" ondrop="drop(event)" ondragover="allowDrop(event)">13</div> 
 
\t <div class="col-sm-2" id="S14" style="background-color:lavenderblush;" ondrop="drop(event)" ondragover="allowDrop(event)">14</div> 
 
    <div class="col-sm-2" id="S15" style="background-color:lavender;" ondrop="drop(event)" ondragover="allowDrop(event)">15</div> 
 
    </div> 
 

 
    <br> 
 
    
 
<div class="row"> 
 
    <div class="col-sm-2" id="S16" style="background-color:lavender;" ondrop="drop(event)" ondragover="allowDrop(event)">16</div> 
 
    <div class="col-sm-2" id="S17" style="background-color:lavenderblush;" ondrop="drop(event)" ondragover="allowDrop(event)">17</div> 
 
    <div class="col-sm-2" id="S18" style="background-color:lavender;" ondrop="drop(event)" ondragover="allowDrop(event)">18</div> 
 
\t <div class="col-sm-2" id="S19" style="background-color:lavenderblush;" ondrop="drop(event)" ondragover="allowDrop(event)">19</div> 
 
    <div class="col-sm-2" id="S20" style="background-color:lavender;" ondrop="drop(event)" ondragover="allowDrop(event)">20</div> 
 
    </div> 
 

 
    <br> 
 

 
<div class="row"> 
 
    <div class="col-sm-2" id="S21" style="background-color:lavender;" ondrop="drop(event)" ondragover="allowDrop(event)">21</div> 
 
    <div class="col-sm-2" id="S22" style="background-color:lavenderblush;" ondrop="drop(event)" ondragover="allowDrop(event)">22</div> 
 
    <div class="col-sm-2" id="S23" style="background-color:lavender;" ondrop="drop(event)" ondragover="allowDrop(event)">23</div> 
 
\t <div class="col-sm-2" id="S24" style="background-color:lavenderblush;" ondrop="drop(event)" ondragover="allowDrop(event)">24</div> 
 
    <div class="col-sm-2" id="S25" style="background-color:lavender;" ondrop="drop(event)" ondragover="allowDrop(event)">25</div> 
 
    </div> 
 

 
    <br> 
 

 
<div class="row"> 
 
    <div class="col-sm-2" id="S26" style="background-color:lavender;" ondrop="drop(event)" ondragover="allowDrop(event)">26</div> 
 
    <div class="col-sm-2" id="S27" style="background-color:lavenderblush;" ondrop="drop(event)" ondragover="allowDrop(event)">27</div> 
 
    <div class="col-sm-2" id="S28" style="background-color:lavender;" ondrop="drop(event)" ondragover="allowDrop(event)">28</div> 
 
\t <div class="col-sm-2" id="S29" style="background-color:lavenderblush;" ondrop="drop(event)" ondragover="allowDrop(event)">29</div> 
 
    <div class="col-sm-2" id="S30" style="background-color:lavender;" ondrop="drop(event)" ondragover="allowDrop(event)">30</div> 
 
    </div> 
 
    <br> 
 
    <center> 
 
    <form action="" method="POST"> 
 
    <input type="submit" class="btn btn-success" name="SavePlan" value="Save Plan" /> 
 
    </form> 
 
    </center> 
 
</div> 
 
    </body> 
 
</html>

下面

是連接到數據庫的代碼。

<?php 
$connection=m ysql_connect("mi-linux.wlv.ac.uk", "user", "password"); 
if (!$connection) die("Cannot connect to DataBase"); 
// making a connection to the database mysql_select_db("db1500052", $connection) or die("Can not find DB"); 
?> 

我現在已經成功地寫一個update.php頁面將發送新位置的數據庫,但仍然沒有周圍的HTML的jQuery和阿賈克斯拿到,

<?php 
include("config.php"); 
$array = $_POST['listnumber']; 

如果($ POST [ '更新'] == 「訂單」){

$x = 1; 
foreach ($array as $key => $value) { 
    $query = "UPDATE dragdrop SET listnumber = ". $x . "WHERE id = " .  $value; 
    mysql_query($query) or die(mysql_error()); 
    $x ++; 
    } 
echo 'Saved'; 

} ?>

+1

你有數據庫保存到?像MySQL? –

+0

你好,謝謝你的提問,我有一個phpmyadmin數據庫。 – wlvali

+0

有太多可能的複雜答案。你必須提供你所嘗試的和你的想法。請記住,我們在這裏幫助你學習。 –

回答

1

我們轉到StackOverflow。至少有兩件事你需要學習如何做到這一點。

首先是如何將數據發送到您的服務器。一個好的方法是使用AJAX和HTTP POST。這是來自SO的一個很好的Q & A應該讓你開始。你可以在你的drop事件中使用類似的東西來發送數據到服務器:Sending an HTTP Post using Javascript triggered event

第二個是如何從PHP代碼插入數據到你的mysql數據庫。您的連接示例有點過時 - 您應該使用mysqli界面來提高安全性。從PHP文檔開始學習本文:http://php.net/manual/en/mysqli.query.php

+0

我不認爲AJAX是實現@wlvali想要的。 – Mifeet

+0

@Mifeet - Thanx。您的觀點是 - POST是獲取數據返回服務器的唯一選擇。我編輯了答案來反映這一點。如果您有另一種想法,認爲它更簡單或更好,請將其發佈。 – bitfiddler

+0

我的觀點是,一個普通的表單提交就足夠了 - 請參閱我在該問題下的評論。 – Mifeet