Phoenix
Phoenix

Reputation: 322

Decoding JSON to save in mysql using PHP and AJAX

I have container with div like this:

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

Basically I have script that is taking note of drag and drop and notes changes in div.step class. See Image for better Understanding

enter image description here

$.ajax({
        type: 'POST',
        url: 'process.php',
        data: {json: JSON.stringify(myArguments)},
        dataType: 'json'            
    });

I don't know how to take this further in process.php

Any way to separate step id and class id as shown in image above so i can insert in database

Thank You.

Edit: Update process.php (Working Now Thanks To 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) {

    } 
}

?>

Upvotes: 0

Views: 388

Answers (1)

Ofir Baruch
Ofir Baruch

Reputation: 10346

Start by decoding the JSON string

$json = $_POST['json'];
$organize = json_decode($json);

$organize structure: (in case of: '{"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"   }
> }

Now, the properties of the $organize class are the steps which are also numbers, so in order to access them you should use:

//$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
}

Please notice you're calling a variable which isn't exist in the 2,3 loops.

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)

Upvotes: 2

Related Questions