Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

From Ajax Json response to url link?

I have php file which take some filenames and convert them to urls and making them in JSON.

its working well when i have just one url response which look like this {"url":"http:\/\/10.0.0.10\/CareLabels\/Poetry\/UF06,UA76.jpg"}

its a single key with single content , if i have more than one link its look like this and its doesn’t work

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{"url":"http:\/\/10.0.0.10\/CareLabels\/Poetry\/UF06,UA76.jpg"}
{"url":"http:\/\/10.0.0.10\/CareLabels\/Poetry\/UF06.jpg"}

My question is how i can make sure that i have 2 objects and when i have no objects at all .
It’s there is any way to check and count the object i get from the php file ?

I can see thats not working at all with data.length

function searchMe(){
  const findCode = $("#search").val();
  $.ajax({ 
    type: "POST", 
    url: "carelabel-checker.php",  
    data: { searching: findCode }, 
    dataType: "json",   
    success: function(data){ 
      console.log(data);
      if(data.length == 0){ // i  cant check the length here ... 
        $('#showme').empty()
        $('#search').remove()
        document.getElementById("showme-error").innerHTML = "Style Code Not Found";
       }else if(data.length == 1){
         $('#showme-error').empty()
         var redirectWindow = window.open(data.url, '_blank');
         redirectWindow.location;
       }else if(data.length > 1){ //same here i can't check the length 
         alert("too much");
      }
    }
  })
};

Here is my PHP code as well

$itemArr = array();
$garment =   strtoupper(isset($_POST['searching']) ? $_POST['searching'] : null);

if($garment[0] == "U" || $garment[0] == "V"){ 

  $filename = "../SpecS3/poetrycarelabels.txt";
  $contents = file($filename);
}
else if($garment[0] == "W" || $garment[0] == "Z") {

  $filename = "../SpecS3/wrapcarelabels.txt";
  $contents = file($filename);
}

$trimmed_array = array_map('trim', $contents);

//Loop trough txt file
foreach($trimmed_array as $item){

  //search for garment code
  if(strpos($item,$garment)!== false){
    //check if its POETRY
    if($item[0] == "U" || $item[0] == "V"){
      $itemArr[] = [url =>"http://10.0.0.10/CareLabels/Poetry/".$item];
      $item2 = json_encode($itemArr,JSON_UNESCAPED_UNICODE);
      echo $item2;
    }
    //check if its WRAP
    else if($item[0] == "W" || $item[0] == "Z") {
      $itemArr[] = [ url =>"http://10.0.0.10/CareLabels/Wrap/".$item];
      $item2 = json_encode($itemArr,JSON_UNESCAPED_UNICODE);
      echo $item2;
    }
}

}

>Solution :

You’re outputting individual, separate JSON objects each time your loop runs, instead of building up one single array with everything in it, and then encoding that array once after your loop ends.

This logical error results in invalid JSON overall, because jQuery tries to parse the whole output from PHP as one coherent JSON object (or array), which it isn’t. On their own, the separate objects are valid JSON, but when you put them all together they do not form a single piece of valid JSON. You need to wrap them in an array.

This should fix it:

//Loop through txt file
foreach($trimmed_array as $item){

  //search for garment code
  if(strpos($item,$garment)!== false){
    //check if its POETRY
    if($item[0] == "U" || $item[0] == "V"){
      $itemArr[] = [url =>"http://10.0.0.10/CareLabels/Poetry/".$item];
    }
    //check if its WRAP
    else if($item[0] == "W" || $item[0] == "Z") {
      $itemArr[] = [ url =>"http://10.0.0.10/CareLabels/Wrap/".$item];
    }
}

$items = json_encode($itemArr,JSON_UNESCAPED_UNICODE);
echo $items;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading