Update Image from database using ajax, jquery,and PHP not working

Ayuktia

I wanna update profile picture of user that has logged in to my website. I use ajax, jquery, php to update data, in order to update data without refresh the page. This code just working to upload image into folder, but when I use this code to update image from database, it only sent null into database.

jquery and ajax script

$("#form-ava").on('submit',(function(e) {
    e.preventDefault();
    $.ajax({
        url: "../config.inc/upload.php",
        type: "POST",
        data:  new FormData(this),
        contentType: false,
        cache: false,
        processData:false,
        beforeSend : function()
        {
            //$("#preview").fadeOut();
            $("#err").fadeOut();
        },
        success: function(data)
        {
            if(data=='invalid')
            {
                // invalid file format.
                $("#err").html("Invalid File !").fadeIn();
            }
            else
            {
                // view uploaded file.
                $("#preview-ava").html(data).fadeIn();
                $("#form-ava")[0].reset();
      $("#hidden-list").hide();
            }
        },
        error: function(e)
        {
            $("#err").html(e).fadeIn();
        }
   });
}));

And it's the php syntax upload.php

<?php
require_once 'koneksi.php';
 if($_POST)
{
  $id_user= $_POST['id_user'];
  $img = $_FILES['image']['name'];
  $tmp = $_FILES['image']['tmp_name'];

$valid_extensions = array('jpeg', 'jpg', 'png', 'gif', 'bmp'); // valid extensions
$path = '../images/ava/'; // upload directory
  try {

// get uploaded file's extension
$ext = strtolower(pathinfo($img, PATHINFO_EXTENSION));

// can upload same image using rand function
$final_image = rand(1000,1000000).$img;

// check's valid format
if(in_array($ext, $valid_extensions))
{
    $path = $path.strtolower($final_image);

    if(move_uploaded_file($tmp,$path))
    {
        echo "<img src='$path' />";

}
else
{
    echo 'invalid';
}
$update = $db_con->prepare("UPDATE tb_users SET image=:img WHERE id_user=:id_user");
$update->bindparam(":id_user", $id_user);
$update->bindparam(":img", $image);
$update->execute();
$count = $update->rowCount();
if($count>0) {
echo "success";
}else {
echo "can't update!";
}
}
}catch(PDOException $e) {
echo $e->getMessage();
}
}
?>

HTML syntax

<form id="form-ava" action="../config.inc/upload.php" method="post" enctype="multipart/form-data">
         <input type="hidden" id="id_user" name="id_user" value="<?php echo $row->id_user;?>">
                <input id="ava-img" type="file" name="image" value="<?php echo $row->image;?>"/>
                    <input id="button" type="submit" value="Simpan" name="update"></br>
         <a href="#" class="btn btn-large btn-success" id="cancel-act"></i> &nbsp; Batal</a>
     </form>
       <div id="err"></div>
Ayuktia
<form id="form-ava" action="../config.inc/upload.php" method="post" enctype="multipart/form-data">
     <input type="hidden" id="id_user" name="id_user" value="<?php echo $row->id_user;?>">
            <input id="ava-img" type="file" name="image" value="<?php echo $row->image;?>"/>
                <input id="button" type="submit" value="Simpan" name="update"></br>
     <a href="#" class="btn btn-large btn-success" id="cancel-act"></i> &nbsp; Batal</a>
 </form>
   <div id="err"></div>


<script type="text/javascript">
$("#form-ava").on('submit',(function(e) {
e.preventDefault();
$.ajax({
    url: "../config.inc/upload.php",
    type: "POST",
    data:  new FormData(this),
    contentType: false,
    cache: false,
    processData:false,
    beforeSend : function()
    {
        //$("#preview").fadeOut();
        $("#err").fadeOut();
    },
    success: function(data)
    {
        if(data=='invalid')
        {
            // invalid file format.
            $("#err").html("Invalid File !").fadeIn();
        }
        else
        {
            // view uploaded file.
            $("#preview-ava").html(data).fadeIn();
            $("#form-ava")[0].reset();
  $("#hidden-list").hide();
        }
    },
    error: function(e)
    {
        $("#err").html(e).fadeIn();
    }
});
}));
</script>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Update Image from database using ajax, jquery,and PHP not working

From Dev

Image Upload in PHP using AJAX Jquery not working

From Dev

Image Upload in PHP using AJAX Jquery not working

From Dev

retriving image from from database through mysqli using php is not working

From Dev

Get data from mysql database using php and jquery ajax

From Dev

Get data from database using PHP, JQuery and AJAX in JSON format

From Dev

PHP unlink image from ajax request is not working

From Dev

Using AJAX to dynamically update text from a database

From Dev

Update database from bootstrap modal using AJAX

From Dev

How to display an image from a database using ajax

From Dev

AJAX update MYSQL database using function called from HTML generated from PHP

From Dev

AJAX update MYSQL database using function called from HTML generated from PHP

From Dev

update mysql with ajax and php (using jquery)

From Dev

Mysql UPDATE using PHP and AJAX, can't update database

From Dev

JQuery AutoComplete TextBox from database using AJAX PageMethods in ASP.Net is not Working in Internet Explorer

From Dev

Upload multiple image using AJAX, PHP and jQuery

From Dev

Update blob image in Database with UPDATE MySQL using PHP

From Java

Actions links not working while fetching data from database using ajax in php

From Dev

Update data from form to database using php

From Dev

Fetching row from database using ajax and php

From Dev

jQuery ajax post to php script & update mysql database

From Dev

Display an image from a MySQL database using PHP

From Dev

Display an image from a MySQL database using PHP

From Dev

update database with jquery and php

From Dev

JQUERY + PHP + AJAX not working

From Dev

jQuery : update the database with all of the data from the ajax-request

From Dev

How to get data from database and assign it to inputs values using PHP and jQuery AJAX

From Dev

How to call set values into a <label> from database using php, ajax and jquery

From Dev

using mysql NOW() function to update row in database not working in PHP query

Related Related

  1. 1

    Update Image from database using ajax, jquery,and PHP not working

  2. 2

    Image Upload in PHP using AJAX Jquery not working

  3. 3

    Image Upload in PHP using AJAX Jquery not working

  4. 4

    retriving image from from database through mysqli using php is not working

  5. 5

    Get data from mysql database using php and jquery ajax

  6. 6

    Get data from database using PHP, JQuery and AJAX in JSON format

  7. 7

    PHP unlink image from ajax request is not working

  8. 8

    Using AJAX to dynamically update text from a database

  9. 9

    Update database from bootstrap modal using AJAX

  10. 10

    How to display an image from a database using ajax

  11. 11

    AJAX update MYSQL database using function called from HTML generated from PHP

  12. 12

    AJAX update MYSQL database using function called from HTML generated from PHP

  13. 13

    update mysql with ajax and php (using jquery)

  14. 14

    Mysql UPDATE using PHP and AJAX, can't update database

  15. 15

    JQuery AutoComplete TextBox from database using AJAX PageMethods in ASP.Net is not Working in Internet Explorer

  16. 16

    Upload multiple image using AJAX, PHP and jQuery

  17. 17

    Update blob image in Database with UPDATE MySQL using PHP

  18. 18

    Actions links not working while fetching data from database using ajax in php

  19. 19

    Update data from form to database using php

  20. 20

    Fetching row from database using ajax and php

  21. 21

    jQuery ajax post to php script & update mysql database

  22. 22

    Display an image from a MySQL database using PHP

  23. 23

    Display an image from a MySQL database using PHP

  24. 24

    update database with jquery and php

  25. 25

    JQUERY + PHP + AJAX not working

  26. 26

    jQuery : update the database with all of the data from the ajax-request

  27. 27

    How to get data from database and assign it to inputs values using PHP and jQuery AJAX

  28. 28

    How to call set values into a <label> from database using php, ajax and jquery

  29. 29

    using mysql NOW() function to update row in database not working in PHP query

HotTag

Archive