Keep image, when editing a form?

0

Hello good afternoon everyone, maybe it is a very simple problem but the truth is that I have days stuck, the detail is that I have a user form that is able to upload image and saves it in the database all right, the problem is that when I edit the user and I give save the image, then use the console firefox and I realized that although in the input file, the value that I put is the database, when doing the shipment does not send the value and keeps erasing, please help me I need it thanks to all

    
asked by Cesar Vieyra 11.03.2017 в 00:24
source

3 answers

0

As @javiertapia tells you, the solution is simpler. When, for example, a user edits his profile and this includes his avatar; by default this is shown in a section of preference encoded in base64:

$id = $_SESSION["user"]["id"];

// consulta

$data = array();
$row = $result->fetch_assoc();
$entry = array(
  "name" => $row["name"],
  "lastname" => $row["lastname"],
  "email" => $row["email"],
  "avatar" => base64_encode($row["avatar"])
);

array.push($data, $entry);

echo json_encode($data);

In the code above, the user information has been converted to an array. The image has been encoded from a base64 blob by means of base64_encode and has been sent this information by JSON (it can be as an array and I send it to the view to iterate in this one).

Then, in the client you would get this JSON:

  

Note: In this example I use the new API for AJAX fetch . You can use jQuery#ajax or the classic XMLHttpRequest . If you use this method be sure to include the polyfill in your HTML.

fetch('/profile')
  .then(res => res.json())
  .then(user => {
    updateEditForm(user);
  });

function updateEditForm (user) {
  let form = document.getElementById('user-edit-form');
  form.name.value = user.name;
  form.lastname.value = user.lastname;
  form.username.value = user.username;
  form.email.value = user.email;

  setPhoto(user.avatar);
}

function setPhoto (imgdata) {
  let img = document.getElementById('avatar');
  img.src = imagedata;
}

Now, how to know if the photo has been changed? Simple, creating an object FormData and verifying if the key avatar has a valid value. If you do not have it, that object's entry is deleted.

function updateUser (e) {
  e.preventDefault();
  let form = document.getElementById('user-edit-form');
  let newData = new FormData(form);

  // elimina la entrada 'avatar' si no se ha escodigo un archivo
  if (!newData.get('avatar')) {
    newData.delete('avatar');
  }

  fetch('/profile', {
    method: 'POST',
    body: newData
  })
  .then(() => {
    alert('Perfil actualizado');
  });
}

In PHP, you should only proceed to update with the data you have received.

    
answered by 11.03.2017 / 15:43
source
2

Do not put the value of the image in input file . Better, when you have submitted the form, verify the input file . If it's empty, do not do anything. But it has data (which means that the user selected an image), upload it to the server and replace what corresponds in the database.

    
answered by 11.03.2017 в 00:28
0

The most advisable thing is that if you only modify the user's information but not all the content, put it in another form that is not where you enter the user at the beginning where you assign an image, or if you want to do the update In the same form, I recommend that you handle conditional in the fields.

<?php
//valor que se envia para relizar la consulta y modificar los datos
if(isset($_GET['id'])){
$id = $_GET['id'];
//consulta
$slq =$conexion->query("select * from tabla where id='$id'");

}
?>
<label>usario</label>
<input type="text" value="<?php if(isset($id)){ echo $nombre; } ?>" />

in the case of the field of the image you can hide it so that it does not appear or you can put it with the current route in the database, when you perform update you do not upload it to the server so that you do not have repeated information and in the table you could overwrite the route that would be the same.

    
answered by 11.03.2017 в 14:52