1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

reply to staff on support ticket

Discussion in 'PHP' started by Ian Haney, Mar 22, 2020.

  1. Ian Haney

    Ian Haney Member

    Messages:
    126
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    33
    #21
    Sorry will need on it unfortunately as tested it again and still not adding the info the db table and updating the other db table
    SEMrush
    I did get bit lost in terms of what curly bracket closed the if check line and got bit lost following the other code
     
    Ian Haney, Mar 23, 2020 IP
    SEMrush
  2. Ian Haney

    Ian Haney Member

    Messages:
    126
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    33
    #22
    I have had another look again and trying to concentrate more on it and altered the coding and am slowly getting somewhere as I now get a success message shown on the page and checked the ticket_replies db table and it's got the reply message added in there but has not added in the ticket_id for some reason, it's added 0 in that column instead of the ticket id, for example the ticket I am working on ticket_id 66

    Just checked the support_tickets db table and it's not updated the ticket_status column to PENDING SUPPORT

    The new code is below


    if (isset($_POST['submit'])) {
               
    // File upload configuration
    $targetDir = "support-ticket-images/";
    $allowTypes = array('pdf','doc','docx','jpg','png','jpeg','gif');
       
    $statusMsg = $errorMsg = $insertValuesSQL = $errorUpload = $errorUploadType = '';
       
        // Escape user inputs for security
    $reply_text = strip_tags($_POST['reply_text'], ENT_QUOTES); 
    $username = htmlentities($_SESSION["user_name"], ENT_QUOTES);     
    $user_id = htmlentities($_SESSION["user_id"], ENT_QUOTES);
    
    $fileNames = array_filter($_FILES['files']['name']);
        if(!empty($fileNames)){
            foreach($_FILES['files']['name'] as $key=>$val){
                // File upload path
                $fileName = basename($_FILES['files']['name'][$key]);
                $targetFilePath = $targetDir . $fileName;
                
                // Check whether file type is valid
                $fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION);
                if(in_array($fileType, $allowTypes)){
                    // Upload file to server
                    if(move_uploaded_file($_FILES["files"]["tmp_name"][$key], $targetFilePath)){
                        // Image db insert sql
                        $insertValuesSQL.="('".$fileName."', '$ticket_id', '".$username."','".$user_id."'),";
                    }
                }
            }
        }
       
        if( strlen($reply_text)>3 ){
            $sql = "INSERT INTO ticket_replies (ticket_id, reply_text,user_type, user_id) VALUES ('$ticket_id','$reply_text','customer', '$user_id');";
    $link->query($sql);
    
    if( strlen($insertValuesSQL)>3 ){
    $sql= "INSERT INTO support_ticket_files (file_name, ticket_id, user_name, user_id) VALUES $insertValuesSQL";
    $link->query($sql);
    }
    
    $sql="UPDATE support_tickets set ticket_status = 'PENDING SUPPORT' where ticket_id ='$ticket_id'";
    $link->query($sql);
        }
            echo "Reply successfully submitted";
        } else{
    $errorMessage=" Enter a message ";
    }
    PHP:
    Is getting there hopefully just needs bit more tweaking to get it fully working, once it is, I'll then test the file upload as well just to make sure that works as well and then will focus on the new support ticket issue but think I can probably copy/paste some code from the file I am working on at the mo
     
    Ian Haney, Mar 23, 2020 IP
  3. JEET

    JEET Notable Member

    Messages:
    3,452
    Likes Received:
    413
    Best Answers:
    16
    Trophy Points:
    235
    #23
    Use this one:

    <?php

    if (isset($_POST['submit'])){

    // File upload configuration
    $targetDir = "support-ticket-images/";
    $allowTypes = array('pdf','doc','docx','jpg','png','jpeg','gif');


    $statusMsg = $errorMsg = $insertValuesSQL = $errorUpload = $errorUploadType = '';

    // Escape user inputs for security
    $reply_text = strip_tags($_POST['reply_text'], ENT_QUOTES);
    $username = htmlentities($_SESSION["user_name"], ENT_QUOTES);
    $user_id = htmlentities($_SESSION["user_id"], ENT_QUOTES);
    $fileNames = array_filter($_FILES['files']['name']);

    if(!empty($fileNames)){
    foreach($_FILES['files']['name'] as $key=>$val){
    //File upload path
    $fileName = basename($_FILES['files']['name'][$key]);
    $targetFilePath = $targetDir . $fileName;
    // Check whether file type is valid
    $fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION);

    if(in_array($fileType, $allowTypes)){
    // Upload file to server
    if(move_uploaded_file($_FILES["files"]["tmp_name"][$key], $targetFilePath)){
    // Image db insert sql
    $insertValuesSQL.="('".$fileName."', '$ticket_id', '".$username."','".$user_id."'),";
    }//move ends
    }//file type check ends
    }//foreach ends
    }//empty file check ends



    if(!empty($insertValuesSQL)){
    $insertValuesSQL = trim($insertValuesSQL, ',');
    }

    //file checks done


    //now add comment to table

    if( strlen($reply_text)>3 ){

    $sql = "INSERT INTO ticket_replies (ticket_id, reply_text,user_type, user_id) VALUES ('$ticket_id','$reply_text','customer', '$user_id');";
    $link->query($sql);

    if( strlen($insertValuesSQL)>3 ){
    $sql="INSERT INTO support_ticket_files (file_name, ticket_id, user_name, user_id) VALUES $insertValuesSQL ";
    $link->query($sql);
    }


    $sql="UPDATE support_tickets set ticket_status = 'PENDING SUPPORT' where ticket_id ='$ticket_id'";
    $link->query($sql);


    }else{
    echo "Error: Enter a message";
    }

    //can display any error messages here


    }//submit
     
    JEET, Mar 23, 2020 IP
  4. Ian Haney

    Ian Haney Member

    Messages:
    126
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    33
    #24
    Just updated the code to the above and it's not adding the ticket_id still to the ticket_replies db table and not updating the support_tickets db table ticket_status column
     
    Ian Haney, Mar 23, 2020 IP
  5. sarahk

    sarahk iTamer Staff

    Messages:
    26,399
    Likes Received:
    3,942
    Best Answers:
    111
    Trophy Points:
    665
    #25
    Where is the script getting $ticket_id from?
    I can't see anywhere that sets up that variable.
     
    sarahk, Mar 23, 2020 IP
  6. JEET

    JEET Notable Member

    Messages:
    3,452
    Likes Received:
    413
    Best Answers:
    16
    Trophy Points:
    235
    #26
    sarahk
    Its in the form, a hidden text field.
     
    JEET, Mar 23, 2020 IP
  7. Ian Haney

    Ian Haney Member

    Messages:
    126
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    33
    #27
    Sorry yeah is a hidden input field just under the form tag as below


    <form action="" method="post" class="form-block ticketform" enctype="multipart/form-data">
    <input type="hidden" name="ticket_id" value="<?php echo $_GET['ticket_id']; ?>"/>
    HTML:
    Do I still need to add the variable in using the code below?

    $ticket_id = htmlentities($_POST["ticket_id"], ENT_QUOTES);
    PHP:
     
    Ian Haney, Mar 23, 2020 IP
  8. JEET

    JEET Notable Member

    Messages:
    3,452
    Likes Received:
    413
    Best Answers:
    16
    Trophy Points:
    235
    #28
    Ian Haney

    Just noticed this error
    Change this line:
    $reply_text = strip_tags( $_POST['reply_text'], ENT_QUOTES );

    to this:
    $reply_text = strip_tags( htmlentities( $_POST['reply_text'], ENT_QUOTES ));


    Then change all the below lines:

    $link->query($sql);

    to this:

    $link->query($sql) or die( $link->error );

    See if an error is thrown.

    Also, how are you checking that nothing is going in DB?

    Are you seeing rows in phpmyadmin,
    or are you viewing replies on this script page itself?
     
    JEET, Mar 23, 2020 IP
  9. JEET

    JEET Notable Member

    Messages:
    3,452
    Likes Received:
    413
    Best Answers:
    16
    Trophy Points:
    235
    #29
    If you are checking replies on this php script itself,
    then do one more modification.

    Add a header location just above the }else{ statement, just after the update query.


    header("location: your_script_name.php?ticket_id=$ticket_id");

    }else{
    echo "Error: Enter a message";
    }
     
    JEET, Mar 23, 2020 IP
  10. sarahk

    sarahk iTamer Staff

    Messages:
    26,399
    Likes Received:
    3,942
    Best Answers:
    111
    Trophy Points:
    665
    #30
    Yes, you do, otherwise your script won't have a value to put into the insert string.

    This is so if the user leaves their result open and they refresh the tab they don't insert the message over and over. I'd leave this for now but it's definitely something you want to do.

    There's a philosophy about separating working from layout and your code is very much intermingling.

    If your working code had feedback for the user it gets saved to the session and then displayed with the next page and removed from the session.

    Essentially the flow would be:
    1. display the ticket view page
    2. go to the update page
    3. save the new ticket reply & set a success/fail message
    4. redirect to the ticket view page & display any success/fail messages
     
    sarahk, Mar 23, 2020 IP
  11. Ian Haney

    Ian Haney Member

    Messages:
    126
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    33
    #31
    I have amended the code and tested again and still the same unfortunately. I am actually checking in phpmyadmin and it's adding the row in the ticket_replies db table but still not adding the ticket_id to the column and not updating the ticket_status column in the support_tickets db table

    Also it's not throwing any error on the page itself
     
    Ian Haney, Mar 23, 2020 IP
  12. Ian Haney

    Ian Haney Member

    Messages:
    126
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    33
    #32
    It's working now, I added in $ticket_id = htmlentities($_POST["ticket_id"], ENT_QUOTES);

    and is adding the ticket_id now to the relevant column and is updating the ticket_status column in the other db table to PENDING SUPPORT

    Wish me luck as will now try to include a file
     
    Ian Haney, Mar 23, 2020 IP
    sarahk likes this.
  13. Ian Haney

    Ian Haney Member

    Messages:
    126
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    33
    #33
    Update: it has added the file all ok

    One quick question, is it possible to add the file upload onto the reply as well submitted by the user, I have attached a screenshot of where it would be good to have the filename of the uploaded file or files for the specific reply
     

    Attached Files:

    Ian Haney, Mar 23, 2020 IP
  14. JEET

    JEET Notable Member

    Messages:
    3,452
    Likes Received:
    413
    Best Answers:
    16
    Trophy Points:
    235
    #34
    It won't be possible to show the file near the reply itself, because you are not storing the ID of the ticket_reply table anywhere in the support_files_ticket table.
    You are only storing the ticket_id from the support_ticket table.
     
    JEET, Mar 23, 2020 IP
  15. Ian Haney

    Ian Haney Member

    Messages:
    126
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    33
    #35
    Ok, how easy is it to add the reply_id into the ticket_replies so can show the file_name for the reply itself, does it mean changing quite a lot of coding?
     
    Ian Haney, Mar 23, 2020 IP
  16. sarahk

    sarahk iTamer Staff

    Messages:
    26,399
    Likes Received:
    3,942
    Best Answers:
    111
    Trophy Points:
    665
    #36
    very easy and recommended.

    Use this thread as an example. You've created the ticket and we've all added bits of code. What if we couldn't tie those bits of code to a reply or view them in order?

    The same could happen with the photos, screenshots or whatever your users are attaching.

    Adding the column to the table is really easy.
    Adding it into your code will be a few lines at most. You need to get the reply_id back when it's inserted and then add that into insertValuesSQL, we'll use a placeholder and then swap it out when we know the value
    $insertValuesSQL .= "('{$fileName}', '{$ticket_id}','###', '{$username}','{$user_id}'),";
    Code (php):
    $sql = "INSERT INTO ticket_replies (ticket_id, reply_text,user_type, user_id) VALUES ('$ticket_id','$reply_text','customer', '$user_id');";
            $link->query($sql);
    /* GET THE ID FROM HERE */
            $ticket_reply_id = ??????
    
            if (strlen($insertValuesSQL) > 3) {
                $insertValuesSQL = str_replace('###', $ticket_reply_id, $insertValuesSQL);
                $sql = "INSERT INTO support_ticket_files (file_name, ticket_id, ticket_reply_id, user_name, user_id) VALUES {$insertValuesSQL} ";
                $link->query($sql);
            }
    Code (php):
     
    sarahk, Mar 23, 2020 IP
  17. Ian Haney

    Ian Haney Member

    Messages:
    126
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    33
    #37
    I have added reply_id into the support_ticket_files db table and put it after the ticket_id column. Do I now copy the code you have put above Sarah?
     
    Ian Haney, Mar 23, 2020 IP
  18. sarahk

    sarahk iTamer Staff

    Messages:
    26,399
    Likes Received:
    3,942
    Best Answers:
    111
    Trophy Points:
    665
    #38
    That should work - just edit the variable name so that it's reply_id and not ticket_reply_id
     
    sarahk, Mar 23, 2020 IP
  19. Ian Haney

    Ian Haney Member

    Messages:
    126
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    33
    #39
    Ok thank you, what do I put in where the question marks are on line $ticket_reply_id = ??????

    Also do I leave the ### as they are on line $insertValuesSQL .= "('{$fileName}', '{$ticket_id}','###', '{$username}','{$user_id}'),";
     
    Ian Haney, Mar 24, 2020 IP
  20. sarahk

    sarahk iTamer Staff

    Messages:
    26,399
    Likes Received:
    3,942
    Best Answers:
    111
    Trophy Points:
    665
    #40
    Leave the hashes, we use a str_replace on them later.

    I don't know the mysqli command for getting the id of the new record so you need to work that one out. That code replaces the ???
     
    sarahk, Mar 24, 2020 IP