Send image from localstore to mysql with ajax message

I am trying to send an image saved in localstore using javascript, but cannot load and display it.

Javascript part:

liste['pic'] = localStorage['pic'];

            $.ajax({
                type: "POST",  
                url: "save.php",  
                data: { pic : liste['pic'] },
                dataType: "json", 
                success: function(data) {
                    if(data) {
                        alert("Picture sent succesfully");
                    }
                }  
                });

The php part that receives the data:

require "lib/connect.php";
$pic = $_POST['pic'];
$insert_query = "INSERT INTO liste ( `pic` ) VALUES ( '".$pic."' );";
$result = mysql_query($insert_query);

The php part that shows pic. There is something in the table, but since it is a blob, I cannot verify the data is correct.

$select_query = "Select   `pic` From liste;";
$result = $dbhandle->query($select_query);

echo "<table border='1'>
<tr>
<th>Image</th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td><img src=\"" . $row['pic'] . "\" width=\"200\"/><br/><br/></td>";
  echo "</tr>";
  }
echo "</table>";
$result->closeCursor();
mysqli_close($dbhandle);

from this i get a broken image. What is missing? The text works, but not the image, why?

+4
source share
1 answer

What you need to know is that when sending values ​​encoded in jsonthrough POST, these values ​​are not in the variables $_POST.

POST application/x-www-form-urlencoded multipart/form-data.

, 'php://input'.

.

$data = json_decode(file_get_contents('php://input'), true);
$text = print_r($data, true);

, .

0

Source: https://habr.com/ru/post/1537263/


All Articles