0

I'm working on a help ticket project and I have a good portion of it already working except this one aspect. I would like to display all ticket information (from a XML file) to the screen but then be able to append the comments with new comments to the ticket. I print all the ticket information without a problem it's appending it that is the problem. One, because my $_GET array is gone after submit I lose the ticket information, two I can't figure out how to just add new comments to the comments section for the particular ticket in my XML. Below is the code.

<!DOCTYPE html>
<html lang="en">
<head>
<title>
    Kapsiak's Ticket Closer
</title>
<meta charset="utf-8">
<link rel="stylesheet" href="site.css">
</head>
<body>
<?php include 'header.php'; ?>
<?php include 'nav.php'; ?>
<main>
    <div class="row">
        <div class="sidel">
        </div>
 <main>
    <div class="row">
        <div class="sidel">
        </div>
        <div class="main">
        <?php
            // read all the ticket information to screen
            $xml=simplexml_load_file("tickets.xml") or die ("Error: Cannot Display Tickets");
            foreach($xml->children() as $ticket) {
            if ($ticket['id'] == $_GET['id']) {
                    echo "<div class='helpLeft'>";
                    echo "<h3>Requester</h3>";
                    echo "<p>First Name: " . $ticket->fname . "</p>";
                    echo "<p>Last Name: " . $ticket->lname . "</p>";
                    echo "<p>Phone: " . $ticket->phone . "</p>";
                    echo "<p>School: " . $ticket->loc . "</p>";
                    echo "<p>Room: " . $ticket->room . "</p>";
                    echo "</div>";
                    echo "<div class='helpLeft'>";
                    echo "<h3>Device Information</h3>";
                    echo "<p>Device Type: " . $ticket->device . "</p>";
                    echo "<p>Manufacturer: " . $ticket->manu . "</p>";
                    echo "<p>Model: " . $ticket->model . "</p>";                            
                    echo "<p>Serial: " . $ticket->serial . "</p>";
                    echo "<p>Status: " . $ticket->status . "</p>";
                    echo "</div>";
                    echo "<div class='comments'>";
                    echo "<h3>Comments</h3>";
                    // check for new comments
                    if (!empty($_POST)){
                        $ticket->appendChild('comments', $_POST(comments));
                        }
                    echo "<p>" . $ticket->comments . "</p>";
                    echo "</div>";
                }
            }
            echo "<br>";                    
        ?>
        <div class="comments">              
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
    <textarea name="comments" id="comments" cols="60" rows="10" required> </textarea><br>                
    <select size="1" name="status" id="status" required>
    <option value="open">Select New Status </option>
    <option value="In Progess">In Progress </option>
    <option value="Awaiting Reply">Awaiting Reply    </option>
    <option value="Closed">Closed </option>
    </select>
            <input type="submit" value="Update"/>
            </form>
            </div>
        </div>
        <div class="sider">
        </div>
     </div>
 </main>
 <?php include 'footer.php'; ?>
 </body>
 </html>
7
  • 1
    Have you already tried to put the ticket-ID in the action attribute of your form? <form action="<?php echo $_SERVER['PHP_SELF'] . '?id='. $_GET['id']; ?>" method="POST"> So that the $_GET['id'] is not empty after submitting the form. Commented Apr 21, 2018 at 17:05
  • I can try that ... just trying to understand your example here, why the '?id' Commented Apr 21, 2018 at 17:10
  • 1
    Because you comparing $ticket['id'] == $_GET['id'] so there has to be a GET-Parameter within the url, but in the form-action the calling url does not contain any GET-Paremeter. Commented Apr 21, 2018 at 17:13
  • 1
    secondly, you have to access the submitted comment via $_POST['comments'] not $_POST(comments) after that you can iterate over each comment. Commented Apr 21, 2018 at 17:18
  • I was able to get the ticket information to stay with by using a hidden field and passing the $_GET['id'] .. now my only problem is appending the actual comments in the XML. I have appended the code with your comment above to Commented Apr 21, 2018 at 17:24

1 Answer 1

1

With the help of Blauharley I was able to figure out the rest of this issue. appendchild() and addchild() are not needed for SimpleXML. I had to just write the new variable to the XML using

$ticket->comments = $oldCom .  " " . $_POST['comments'];

I then just had to save the XML using

file_put_contents("tickets.xml", $xml->asXML());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.