Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

180
Vistas
Where do PHP echos go when you are posting to a page?

This might be a dumb question. I'm fairly new to PHP. I am trying to get a look at some echo statements from a page I'm posting to but never actually going to. I can't go directly to the page's url because without the post info it will break. Is there any way to view what PHP echos in the developer console or anywhere else?

Here is the Ajax:

function uploadImage(image) {
  
      var data = new FormData();
      data.append("image", image);

      imgurl = 'url';
      filepath = 'path';
      $.ajax({
        url: imgurl,
        cache: false,
        contentType: false,
        processData: false,
        data: data,
        type: "post",
        success: function(url) {
          var image = $('<img class="comment_image">').attr('src', path + url);
          $('#summernote').summernote("insertNode", image[0]);
        },
        error: function(data) {
          console.log(data);
        }
      });
    }

And here is the php file:

<?php
  $image = $_FILES['image']['name'];
  $uploaddir = 'path';
  $uploadfile = $uploaddir . basename($image);
  if( move_uploaded_file($_FILES['image']['tmp_name'],$uploadfile)) {
    echo $uploadfile;
  } else {
    echo "Unable to Upload";
  }
?>

So this code runs fine but I'm not sure where the echos end up and how to view them, there is more info I want to print. Please help!

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Here is a basic example...

HTML (Form)

<form action="script.php" method="POST">
    <input name="foo">
    <input type="submit" value="Submit">
</form>

PHP Script (script.php)

<?php

    if($_POST){
        echo '<pre>';
        print_r($_POST); // See what was 'POST'ed to your script.
        echo '</pre>';
        exit;
    }

    // The rest of your PHP script...

Another option (rather than using a HTML form) would be to use a tool like POSTMAN which can be useful for simulating all types of requests to pages (and APIs)

about 4 years ago · Juan Pablo Isaza Denunciar

0

You already handle the response from PHP (which contains all the outputs, like any echo)

In the below code you have, url will contain all the output.

To see what you get, just add a console.log()

$.ajax({ 
    ...
    success: function(url) {
        // Output the response to the console
        console.log(url);

        var image = $('<img class="comment_image">').attr('src', path + url);
        $('#summernote').summernote("insertNode", image[0]);
    },
    ...
}

One issue with the above code is that if the upload fails, your code will try to add the string "Unable to upload" as the image source. It's better to return JSON with some more info. Something like this:

// Set the header to tell the client what kind of data the response contains
header('Content-type: application/json');

if( move_uploaded_file($_FILES['image']['tmp_name'],$uploadfile)) {
    echo json_encode([
        'success' => true,
        'url'     => $uploadfile,
        // add any other params you need
    ]);
} else {
    echo json_encode([
        'success' => false,
        'url'     => null,
        // add any other params you need
    ]);
}

Then in your Ajax success callback, you can now check if it was successful or not:

$.ajax({ 
    ...
    dataType: 'json', // This will make jQuery parse the response properly
    success: function(response) {
        if (response.success === true) {
            var image = $('<img class="comment_image">').attr('src', path + response.url);
            $('#summernote').summernote("insertNode", image[0]);
        } else {
            alert('Ooops. The upload failed');
        }
    },
    ...
}

If you add more params to the array in your json_encode() in PHP, you simply access them with: response.theParamName.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda