我有像这样的div容器:
<div class="container">
<div class="step" id="1">
<h2 class="title">Step 1</h2>
<div class="image" id="1">Item 1</div>
<div class="image" id="2">Item 2</div>
<div class="image" id="3">Item 3</div>
</div>
<div class="step" id="2">
<h2 class="title">Step 2</h2>
<div class="image" id="4">Item 4</div>
<div class="image" id="5">Item 5</div>
<div class="image" id="6">Item 6</div>
</div>
<div class="step" id="3">
<h2 class="title">Step 3</h2>
<div class="image" id="7">Item 7</div>
<div class="image" id="8">Item 8</div>
<div class="image" id="9">Item 9</div>
</div>
</div>
基本上我有一个脚本,它记录了拖放和div.step类中的注释变化。请参阅图片以获得更好的理解
$.ajax({
type: 'POST',
url: 'process.php',
data: {json: JSON.stringify(myArguments)},
dataType: 'json'
});
我不知道如何在process.php中进一步了解这一点。
如上图所示分隔步骤ID和类ID的任何方法,以便我可以在数据库中插入
谢谢。
编辑:更新process.php (感谢Ofir Baruch立即工作)
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testdb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$json = $_POST['json'];
$organize = json_decode($json);
foreach($organize->{1} as $pos => $div){
$pos1 = 1;
$sql = "INSERT INTO process VALUES (DEFAULT,'".mysqli_real_escape_string($conn,$pos1)."','".mysqli_real_escape_string($conn,$div)."')";
if ($conn->query($sql) === TRUE) {
}
//Insert to the Database:
// Step: 1
// image_id: $div
// position: $pos
}
foreach($organize->{2} as $pos => $div){
//Insert to the Database:
// Step: 2
// image_id: $div
// position: $pos
$pos1 = 2;
$sql = "INSERT INTO process VALUES (DEFAULT,'".mysqli_real_escape_string($conn,$pos1)."','".mysqli_real_escape_string($conn,$div)."')";
if ($conn->query($sql) === TRUE) {
}
}
foreach($organize->{3} as $pos => $div){
//Insert to the Database:
// Step: 3
// image_id: $div
// position: $pos
$pos1 = 3;
$sql = "INSERT INTO process VALUES (DEFAULT,'".mysqli_real_escape_string($conn,$pos1)."','".mysqli_real_escape_string($conn,$div)."')";
if ($conn->query($sql) === TRUE) {
}
}
?>
答案 0 :(得分:2)
首先解码JSON字符串
$json = $_POST['json'];
$organize = json_decode($json);
$组织结构: (如果:'{“1”:[“1”,“2”],“2”:[“3”,“4”]}';)
> object(stdClass)#1 (2) {
>
> ["1"]=> array(2) {
> [0]=> string(1) "1"
> [1]=> string(1) "2" }
> ["2"]=> array(2) {
> [0]=> string(1) "3"
> [1]=> string(1) "4" }
> }
现在,$ organiz类的属性也是数字的步骤,所以为了访问它们,你应该使用:
//$organize->{1} //Access step 1 arrays of "divs"
foreach($organize->{1} as $pos => $div){
//Insert to the Database:
// Step: 1
// image_id: $div
// position: $pos
}
foreach($organize->{2} as $pos => $div){
//Insert to the Database:
// Step: 2
// image_id: $div
// position: $pos
}
foreach($organize->{3} as $pos => $div){
//Insert to the Database:
// Step: 3
// image_id: $div
// position: $pos
}
请注意你正在调用2,3循环中不存在的变量。
foreach($organize->{2} as $pos => $div){
$pos1 = 2; //change to $pos2 = 2;
$sql = "INSERT INTO process VALUES (DEFAULT,'".mysqli_real_escape_string($conn,$pos2)
....
....
foreach($organize->{3} as $pos => $div){
$pos1 = 3; //change to $pos3 =3 ;
$sql = "INSERT INTO process VALUES (DEFAULT,'".mysqli_real_escape_string($conn,$pos3)