AJAX
function ajax_json_gallery(folder) {
alert(folder);
var thumbnailbox = $('#thumbnailbox');
$.ajax({
type: "POST",
url: "json_gallery_data.php",
contentType: "application/x-www-form-urlencoded",
dataType: "json",
data: "folder=" + folder,
success: function(d) {
for (var obj in d) {
if (d.hasOwnProperty(obj)) {
alert(d[obj]); //access data//
alert(d[obj].src); //undefined//
}
}
}
});
}
PHP
header('Content-Type: application/json');
$folder = "Img/Company1/Jersey1";
$dir = $folder."/";
$dirHandle = opendir($dir);
$i = 0;
$directoryfiles = array();
while ($file = readdir($dirHandle)) {
if(!is_dir($file) && preg_match("/.jpg|.gif|.png/i", $file)){
$i++;
$src = "$dir$file";
$directoryfiles[] = '"img'.$i.'":{ "num":"'.$i.'","src":"'.$src.'", "name":"'.$file.'" },';
}
}
closedir($dirHandle);
echo json_encode($directoryfiles);
console.log(d)
[""img1":{ "num":"1","src":"Img/House1/Type1/Image1.png", "name":"Image1.png" },",
""img2":{ "num":"2","src":"Img/House1/Type1/Image2.png", "name":"Image2.png" },",
""img3":{ "num":"3","src":"Img/House1/Type1/Image3.png", "name":"Image3.png" },",
""img4":{ "num":"4","src":"Img/House1/Type1/Image4.png", "name":"Image4.png" },"]
x3
i am using ajax to get all image inside the folder directory , and return to ajax but when i tried to access the d[o].src it return undefined ,i had no idea what am i missing here.
Don't try to write JSON text yourself. Just create an associative array or stdClass object, add the appropriate key/values, then add that to $directoryfiles
. json_encode
will then do the proper encoding
$directoryfiles = array();
while ($file = readdir($dirHandle)) {
if(!is_dir($file) && preg_match("/.jpg|.gif|.png/i", $file)){
$i++;
$src = "$dir$file";
$temp = new stdClass;
$temp->num = $i;
$temp->src = $src;
$temp->name = $file;
$directoryfiles["img".$i] = $temp;
}
}
closedir($dirHandle);
echo json_encode($directoryfile);
change the format of json to,
{"img1":{ "num":"1","src":"Img/House1/Type1/Image1.png", "name":"Image1.png" },
"img2":{ "num":"2","src":"Img/House1/Type1/Image2.png", "name":"Image2.png" },
"img3":{ "num":"3","src":"Img/House1/Type1/Image3.png", "name":"Image3.png" },
"img4":{ "num":"4","src":"Img/House1/Type1/Image4.png", "name":"Image4.png" }};
then u use the code,
console.log(d.img1.src);
Firebase Cloud Functions: PubSub, "res.on is not a function"
TypeError: Cannot read properties of undefined (reading 'createMessageComponentCollector')
How do I collapse side panel when viewed on smaller screens, so that "panel-header" becomes a toggle button. I.
I need to create an assosiative array in jQuery from PHP. .
I've read in chosen jquery at http://harvesthq. github.
I just want to allow users make modification offline with insecure data. Namely modifying their profile name, add new content (page), photos and so forth.