• We just launched and are currently in beta. Join us as we build and grow the community.

PHP - Save Image Data By Dragging Using Ajax

purps559

Custom Payload Developer
P Rep
0
0
0
Rep
0
P Vouches
0
0
0
Vouches
0
Posts
123
Likes
33
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial we will create a Save Image Data By Dragging using Ajax. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. Using Ajax, data could then be passed between the browser and the server, using the XMLHttpRequest API, without having to reload the web page. So Let's do the coding...

Getting started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

And this is the link for the jquery that i used in this tutorial https://jquery.com/.

Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_image, after that click Import then locate the database file inside the folder of the application then click ok.
2019-02-04_22_56_52-localhost_127.0.0.1_db_image_phpmyadmin_4.8.3.png

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2. $conn

    =

    mysqli_connect

    (

    "localhost"

    ,

    "root"

    ,

    ""

    ,

    "db_image"

    )

    ;

  3. if

    (

    !

    $conn

    )

    {
  4. die

    (

    "Error: Failed to connect to database!"

    )

    ;
  5. }
  6. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!

    DOCTYPE html>
  2. <

    html lang=

    "en"

    >
  3. <

    meta charset=

    "UTF-8"

    name=

    "viewport"

    content=

    "width=device-width, initial-scale=1"

    />
  4. <

    link

    rel=

    "stylesheet"

    type=

    "text/css"

    href=

    "css/bootstrap.css"

    />
  5. <

    body>
  6. <

    nav class

    =

    "navbar navbar-default"

    >
  7. <

    div class

    =

    "container-fluid"

    >
  8. <

    a class

    =

    "navbar-brand"

    href=

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a>
  9. </

    div>
  10. </

    nav>
  11. <

    div class

    =

    "col-md-3"

    ></

    div>
  12. <

    div class

    =

    "col-md-6 well"

    >
  13. <

    h3 class

    =

    "text-primary"

    >

    PHP -

    Save Image Data By Dragging Using Ajax</

    h3>
  14. <

    hr style=

    "border-top:1px dotted #ccc;"

    />

  15. <

    div class

    =

    "col-md-6"

    >
  16. <

    img src=

    "images/image1.jpeg"

    draggable=

    "true"

    class

    =

    "images"

    id=

    "image1"

    name=

    "image1.jpeg"

    height=

    "100"

    width=

    "120"

    style=

    "border:1px SOLID #000; margin:10px;"

    />
  17. <

    img src=

    "images/image2.jpeg"

    draggable=

    "true"

    class

    =

    "images"

    id=

    "image2"

    name=

    "image2.jpeg"

    height=

    "100"

    width=

    "120"

    style=

    "border:1px SOLID #000; margin:10px;"

    />
  18. <

    img src=

    "images/image3.jpg"

    draggable=

    "true"

    class

    =

    "images"

    id=

    "image3"

    name=

    "image3.jpg"

    height=

    "100"

    width=

    "120"

    style=

    "border:1px SOLID #000; margin:10px;"

    />
  19. <

    img src=

    "images/image4.jpg"

    draggable=

    "true"

    class

    =

    "images"

    id=

    "image4"

    name=

    "image4.jpg"

    height=

    "100"

    width=

    "120"

    style=

    "border:1px SOLID #000; margin:10px;"

    />
  20. </

    div>
  21. <

    div class

    =

    "col-md-6"

    >
  22. <

    div id=

    "drop-zone"

    style=

    "border:5px dotted #ccc; height:200px; padding-top:50px; padding-left:50px;"

    >
  23. <

    h2>

    DROP HERE</

    h2>
  24. </

    div>
  25. </

    div>
  26. <

    br style=

    "clear:both;"

    />
  27. <

    table class

    =

    "table table-bordered"

    >
  28. <

    thead>
  29. <

    tr>
  30. <

    th>

    File

    Name</

    th>
  31. <

    th>

    File

    Type</

    th>
  32. <

    th>

    File

    Path</

    th>
  33. </

    tr>
  34. </

    thead>
  35. <

    tbody id=

    "result"

    ></

    tbody>
  36. </

    table>
  37. </

    div>
  38. <

    script src=

    "js/jquery-3.2.1.min.js"

    >

    </script>
  39. <

    script src=

    "js/script.js"

    >

    </script>
  40. </

    body>
  41. </

    html>

Creating PHP Query

This code contains the php query of the application. This code will store the image data to the database server, and also can retrieve it then display in the table. To do that just copy and write this block of codes inside the text editor, then save it as show below.
save.php
  1. <?php
  2. require_once

    'conn.php'

    ;

  3. $image_name

    =

    $_POST

    [

    'image_name'

    ]

    ;
  4. $image_type

    =

    $_POST

    [

    'image_type'

    ]

    ;
  5. $image_path

    =

    $_POST

    [

    'image_path'

    ]

    ;

  6. mysqli_query

    (

    $conn

    ,

    "INSERT INTO `image` VALUES('', '$image_name

    ', '$image_type

    ', '$image_path

    ')"

    )

    or die

    (

    mysqli_error

    (

    )

    )

    ;

  7. echo

    "Successfully saved!"

    ;
  8. ?>

data.php
  1. <?php
  2. require_once

    'conn.php'

    ;

  3. if

    (

    ISSET

    (

    $_POST

    [

    'res'

    ]

    )

    )

    {

  4. $query

    =

    mysqli_query

    (

    $conn

    ,

    "SELECT * FROM `image`"

    )

    or die

    (

    mysqli_error

    (

    )

    )

    ;
  5. while

    (

    $fetch

    =

    mysqli_fetch_array

    (

    $query

    )

    )

    {
  6. ?>
  7. <tr>
  8. <td><?php

    echo

    $fetch

    [

    'image_name'

    ]

    ?>

    </td>
  9. <td><?php

    echo

    $fetch

    [

    'image_type'

    ]

    ?>

    </td>
  10. <td><?php

    echo

    $fetch

    [

    'image_path'

    ]

    ?>

    </td>
  11. </tr>
  12. <?php
  13. }
  14. }
  15. ?>

Creating The Script

This is where the main function of the application. This code can drag the image in the drop zone, then will get the image data and send ajax request to the server. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. $(

    document)

    .ready

    (

    function

    (

    )

    {
  2. getData(

    )

    ;

  3. $(

    "#drop-zone"

    )

    .on

    (

    'dragenter'

    ,

    function

    (

    e)

    {
  4. e.preventDefault

    (

    )

    ;
  5. $(

    this

    )

    .css

    (

    'background'

    ,

    '#ccc'

    )

    ;
  6. }

    )

    ;

  7. $(

    "#drop-zone"

    )

    .on

    (

    'dragleave'

    ,

    function

    (

    e)

    {
  8. e.preventDefault

    (

    )

    ;
  9. $(

    this

    )

    .css

    (

    'background'

    ,

    '#fff'

    )

    ;
  10. }

    )

    ;

  11. $(

    "#drop-zone"

    )

    .on

    (

    'dragover'

    ,

    function

    (

    e)

    {
  12. e.preventDefault

    (

    )

    ;
  13. }

    )

    ;

  14. $(

    ".images"

    )

    .on

    (

    'dragstart'

    ,

    function

    (

    e)

    {
  15. e.originalEvent

    .dataTransfer

    .setData

    (

    "data"

    ,

    e.target

    .id

    )

    ;
  16. }

    )

    ;

  17. $(

    "#drop-zone"

    )

    .on

    (

    'drop'

    ,

    function

    (

    e)

    {
  18. $(

    this

    )

    .css

    (

    'background'

    ,

    '#fff'

    )

    ;
  19. e.preventDefault

    (

    )

    ;
  20. var

    data =

    e.originalEvent

    .dataTransfer

    .getData

    (

    "data"

    )

    ;
  21. var

    image_name =

    $(

    "#"

    +

    data)

    .attr

    (

    "name"

    )

    ;
  22. var

    image =

    $(

    "#"

    +

    data)

    .attr

    (

    "name"

    )

    ;
  23. var

    splt =

    image.split

    (

    "."

    )

    ;
  24. var

    image_type =

    splt[

    1

    ]

    ;
  25. var

    image_path =

    $(

    "#"

    +

    data)

    .attr

    (

    "src"

    )

    ;
  26. $.ajax

    (

    {
  27. url:

    'save.php'

    ,
  28. type:

    'POST'

    ,
  29. data:

    {

    image_name:

    image_name,

    image_type:

    image_type,

    image_path:

    image_path}

    ,
  30. success:

    function

    (

    data)

    {
  31. alert(

    data)

    ;
  32. getData(

    )

    ;
  33. }
  34. }

    )

    ;
  35. }

    )

    ;
  36. }

    )

    ;

  37. function

    getData(

    )

    {
  38. $.ajax

    (

    {
  39. url:

    'data.php'

    ,
  40. type:

    'POST'

    ,
  41. data:

    {

    res:

    1

    }

    ,
  42. success:

    function

    (

    data)

    {
  43. $(

    '#result'

    )

    .html

    (

    data)

    ;
  44. }
  45. }

    )

    ;
  46. }

There you have it we successfully created Save Image Data By Dragging Using Ajax using Ajax. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

335,022

335,030

Top