How to go through an arrangement that is sent from php to Javascript

3

Well doing this ajax that I send to a processing form which returns an Array that brings parameters and an array of names I will leave here my example of php code;

This is an array of names which I fill with a for that I do

$nombre[]= $nuevoNombre; 

    $respuesta=array('IdImagen'=>$nextID,'Nombre'=>$nombre,'error'=>$dpError);
          $dpResult= json_encode($respuesta);
          echo $dpResult;


    $.ajax({


        cache: false,
        url:ruta,
        type:"POST",
        data: formData,
        contentType:false,
        processData:false,
        dataType: "json",
        success:function(respuesta){
        var res = new String(respuesta.Nombre);
        for (x=0; x< res.length ;x++){
        var strHTML = "<img src='media/thumbs/" +res.replace(/,/g,"")[x]+ "' width='100' height='100' alt=''>";
        }
        alert(strHTML);
    }
    });

and when I print it on the alert it returns this to me

<img src='media/thumbs/153.jpeg154.jpeg155.jpeg' width='100' height='100' alt=''>

and I would like to know if it is possible that it could come out like this

<img src='media/thumbs/153.jpeg width='100' height='100' alt=''>
<img src='media/thumbs/154.jpeg width='100' height='100' alt=''>
<img src='media/thumbs/154.jpeg width='100' height='100' alt=''>

In order to show a preview of photos that have been uploaded!

I look forward to your help and that I have already been clear on what I am asking Graciaaaaaas!

    
asked by Miguel Calles 02.03.2018 в 01:01
source

3 answers

4

The problem is that you are not concatenating the strings, but that you are always stepping on the value assigned to each loop loop.

Solution:

  • You must define the variable strHTML outside the loop for .
  • To concatenate you can use:

    • The operator of addition ( += ). Example:

      var strHTML = "";
      for (x=0; x< res.length ;x++) {
        strHTML += 'string';
      }
      alert(strHTML);
      
    • Step up value by adding the previous value. Example:

      var strHTML = "";
      for (x=0; x< res.length ;x++) {
        strHTML = strHTML + 'string';
      }
      alert(strHTML);
      
    • An array, push() to add them and then < a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Referencia/Objetos_globales/Array/join"> join() to generate the result. Example:

      var strHTML = [];
      for (x=0; x< res.length ;x++) {
        strHTML.push('string');
      }
      alert(strHTML.join(''));
      
    • Etc.
answered by 02.03.2018 / 13:49
source
0

You can use a for in to traverse an array in the following way;

	var res = {"nombres":"John"};
	var strHTML = "";
	for (var nombres  in res ){
    strHTML += "<img src='media/thumbs/" +res['nombres']+ "' width='100' height='100' alt=''>";
    console.log(strHTML);
}

Where res is your fix and nombres is your local variable in for in

You can get more information in the documentation: link

    
answered by 02.03.2018 в 01:23
0

I think it would be more convenient if you used the json format to pass the response from php to js.

-This would be putting the following line in the php file:

echo json_encode($resultado); 

$resultado is the array that contains the answers.

-And reconvert it to array in the js file with the following command:

var resp = JSON.parse(respuesta);  

respuesta is the variable that you receive from php.

Once this is done, you can use it as a normal array and take each value out.

    
answered by 02.03.2018 в 05:21