user2168287 user2168287 - 1 month ago 7
Ajax Question

Javascript call PHP file scandir() results

I have created a small helpdesk system which allows users to uploads files when creating tickets. The tickets are stored in a folder with an ID number that matches the ticket details that are stored in a database. Now when I open the ticket details I also want it to list the files associated with that ticket so I can open them. So far I can retrieve all the ticket details but I'm stuck with the json_encode($files) and how I can reference them with my current JavaScript code. Any ideas?

I also have the issue with the . and .. in the scandir() array and would like to remove them. When using the commented line you can see in my PHP file it makes the json_encode array look incorrect. Thanks

PHP file (snippet)

$value = $_POST['value'];

$sql = "SELECT * FROM helpdesk WHERE ID = '$value'";
$result = mysqli_query( $conn, $sql);

while( $rowEdit = mysqli_fetch_array($result))
echo json_encode(array($rowEdit['ID'], $rowEdit['DateCreated'], $rowEdit['Name'], $rowEdit['Company'], $rowEdit['Phone'], $rowEdit['Email']));

$dir = 'uploads/' . $value .'/';
$files = scandir($dir);
//$files = array_diff(scandir($dir), array('.', '..'));
echo json_encode($files);

HTML file (JavaScript snippet)

/* Opens selected ticket details */
var modal = document.getElementById('modal');
var output = "";
var value = $("#tblTickets tr.selected td:first").html();
type : "POST",
url : "sql_helpdesk_ticket_details.php",
data : {value:value},
success : function(output) {
var result = $.parseJSON(output);
$(".modal-body #txtID").val(result[0]);
$(".modal-body #txtDateCreated").val(result[1]);
$(".modal-body #txtName").val(result[2]);
$(".modal-body #txtCompany").val(result[3]);
$(".modal-body #txtPhone").val(result[4]);
$(".modal-body #txtEmail").val(result[5]); = 'block';


It sounds like you want to combine the two outputs, from what I can decipher. To do that, create one array by storing the database return in one key called data and the other with the files named files and at the end output to json:

# Trim any spacing
$id = trim($_POST['value']);
# Just die if there are any errors
# I am presuming $_POST['value'] is numeric. If not, you need to bind_param
if(!empty($id)) {
       die(json_encode(array('error'=>'Id not numeric')));
    die(json_encode(array('error'=>'Id can not be empty')));
# As noted, if you are allowing anything but numeric, bind_param is required
$result = mysqli_query( $conn, "SELECT * FROM `helpdesk` WHERE `ID` = '{$id}'");
$data   = array();
# Loop through and save to array
while($rowEdit = mysqli_fetch_array($result)) {               
    $data['data'][] =   array(
# Create directory path
$dir = 'uploads'.DS.$value.DS;
# It is wise to check that it exists first
if(is_dir($dir)) {
    $files = scandir($dir);
    # I just loop through the results, but you can array_diff to filter the dots
    foreach($files as $file) {
        # Save files to array
        $data['files'][] = $file;
# Output all the data

Your javascript will have to be adjusted to accommodate the new keys.