1
<canvas id="c" name="c"></canvas>
<input style="display:none;" type="file" name="nextImage" id="nextImage"/>

Above code is canvas where edited image is placed, below is input field where i want the canvas image value.

jQuery('document').ready(function(){

  jQuery('#svconvac').click(function(e){
      e.preventDefault();
      console.log("Click");
      var canvas = document.getElementById('c');
      var dataURL = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
    //jQuery("#nextImage").val(dataURL);

    console.log(dataURL);
    //return false;
    jQuery.ajax({
      type: "POST",
      url: "editMap.php",
      data: { 
        imgBase64: dataURL
      },
      success: function(data) {
        data['dataURL'] = document.getElementById("nextImage").value;
        console.log('saved'); 
      } 
    });


  });

});

So far what i tried using this ajax function but no chances to be successful, am i calling wrong data in ajax? where it says:

editedMap = $data['dataURL']; 

My Function for saving to database Please also look at this:

public function mapUpdate($data, $file, $id)
        {
            $floor_name =$this->fm->validation($data['floor_name']);

            $floor_name =mysqli_real_escape_string($this->db->link, $data['floor_name']);

            $permited  = array('jpg', 'jpeg', 'png', 'gif');
            $file_name = $file['nextImage']['name'];
            $file_size = $file['nextImage']['size'];
            $file_temp = $file['nextImage']['tmp_name'];

            $div = explode('.', $file_name);
            $file_ext = strtolower(end($div));
            $unique_image = substr(md5(time()), 0, 10).'.'.$file_ext;
            $uploaded_image = "uploads/maps/".$unique_image;

            if($floor_name == '')
            {
                $msg = "<span class='error'> Fields must not be empty !! </span>";
                return $msg;
            }
            else{
                if (!empty($file_name)) 
                {

                    if ($file_size >1048567) 
                    {
                        echo "<span class='error'>Image Size should be less then 1MB! </span>";
                    } 

                    elseif (in_array($file_ext, $permited) === false) 
                    {
                        echo "<span class='error'>You can upload only:-".implode(', ', $permited)."</span>";
                    }


                    else
                    {
                        $delImageQuery = "SELECT * FROM tbl_map WHERE mapId='$id'";
                        $getData = $this->db->select($delImageQuery);

                        if ($getData) 
                        {
                            while ($delImg =$getData->fetch_assoc()) 
                            {
                                $delLink =  $delImg['mapImg'];
                                unlink($delLink);
                            }
                        }


                        move_uploaded_file($file_temp, $uploaded_image);

                        $query= "UPDATE tbl_map SET floorName = '$floor_name', mapImg = '$uploaded_image' WHERE mapId ='$id' ";

                        $update_row =$this->db->update($query);
                        if($update_row)
                        {
                            $msg ="<span class='success'> Map Update successfully. </span>";
                            return $msg;
                        }

                        else 
                        {
                            echo "<span class='error'>Map Not Update !</span>";
                        }
                    }

                } 

                else
                {                               
                    $query= "UPDATE tbl_map SET floorName = '$floor_name' WHERE mapId ='$id' ";

                    $update_row =$this->db->update($query);

                    if($update_row)
                    {
                        $msg ="<span class='success'> Map Update successfully. </span>";
                        return $msg;
                    }

                    else 
                    {
                        echo "<span class='error'>Map Not Update !</span>";
                    }
                }
            }
   }// Update function ends here

Hoping for some help.

1 Answers1

0

Input type file does not accept base64 data as value, you can use input type hidden like below.

HTML

<canvas id="c" name="c"></canvas>
<input type="hidden" name="nextImage" id="nextImage"> 

JS

jQuery('document').ready(function(){

  jQuery('#svconvac').click(function(e){
      e.preventDefault();
      console.log("Click");
      var canvas = document.getElementById('c');
      var dataURL = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
    //jQuery("#nextImage").val(dataURL);

    console.log(dataURL);
    //return false;
    jQuery.ajax({
      type: "POST",
      url: "editMap.php",
      data: { // [1]
        imgBase64: dataURL
      },
      success: function(data) { // [2]
        // data at [1] and [2] are not same, data [2] is variable that hold values of response from "editMap.php" API, you may also change this name to any name like response.
        data['dataURL'] = document.getElementById("nextImage").value;
        console.log('saved'); 
      } 
    });


  });

});

Praveen Patel
  • 349
  • 7
  • 24
  • Not working for me, I attached my form can you please also look at this? – Moiz Sharif Jan 07 '20 at 04:49
  • i solved it with ajax, can you please check is it right way of storing ajax data to variable : success: function(data) { data.dataURL = document.getElementById("nextImage").value; console.log('saved'); } – Moiz Sharif Jan 07 '20 at 13:04
  • what do you want to achieve? please explain. It is better to add you code in https://jsfiddle.net/ – Praveen Patel Jan 07 '20 at 13:21
  • i have variable in ajax called ( dataURL ) and i want that variable data should be place to my input file called ( nextImage ). That's it. What i tried is: data.dataURL = document.getElementById("nextImage").value; Din't worked. – Moiz Sharif Jan 07 '20 at 13:30
  • 1
    try like this, document.getElementById("nextImage").value = editedMap, You can not add string value in input type file, you need to change it to hidden or text – Praveen Patel Jan 07 '20 at 13:33
  • didn't work. can you check my JS code i have updated. – Moiz Sharif Jan 08 '20 at 05:41
  • 1
    what is data['dataURL'] in success function? it means you are updating server (editMap.php) response value. data['dataURL'] will will get valuue of this input field document.getElementById("nextImage").value – Praveen Patel Jan 08 '20 at 06:16
  • if i change file type to text or hidden then i can't save that image inside my save function where i'm getting image like this see: $file_name = $file['nextImage']['name']; $file_size = $file['nextImage']['size']; $file_temp = $file['nextImage']['tmp_name']; – Moiz Sharif Jan 08 '20 at 06:33
  • i have added my function as well check my question. – Moiz Sharif Jan 08 '20 at 06:35
  • 1
    remember canvas.toDataURL("image/png") return base64 data (string data) you can get this data in server side as a string and create a file with this data to store in server directory Ref : https://stackoverflow.com/a/11511605/9009397 – Praveen Patel Jan 08 '20 at 06:44
  • in my case now i have to replace dataURL with ref url u sent? – Moiz Sharif Jan 08 '20 at 06:50
  • i have sent ref url to convert your base64 string into image file. – Praveen Patel Jan 08 '20 at 06:53
  • is there any chance for you to check it via anydesk or teamviewer – Moiz Sharif Jan 08 '20 at 07:24
  • no, I've already add enough code, descriptions and links, Please follow link to convert base64 data to image in server side. This is not a big issue. Are you beginner in php and javascript? – Praveen Patel Jan 08 '20 at 07:36