使用PHP SimpleXML向XML文件追加内容的工单问题求助
Fixing Your Ticket System's Comment Addition Issues
Hey there! Let's tackle those two roadblocks you're hitting with adding comments to your ticket system—they're completely fixable with a few targeted tweaks.
Issue 1: Losing $_GET Data After Submitting Comments
The core problem here is that your original ticket identifier (from the $_GET array) isn't being carried over when you submit the comment form. Here's how to lock that data in:
- Embed the ticket ID in your form: Add a hidden input field to your comment form that stores the ticket ID from the initial
$_GETrequest. This ensures the ID travels with the form submission (use POST for this—better security for user-submitted data):<form method="POST" action="process_comment.php"> <input type="hidden" name="ticket_id" value="<?php echo htmlspecialchars($_GET['ticket_id']); ?>"> <label for="comment">Your Comment:</label> <textarea name="comment_content" id="comment" required></textarea> <!-- Add other fields like author name if needed --> <button type="submit">Post Comment</button> </form> - Redirect after processing: Once you've saved the comment, don't leave users on the processing page. Redirect back to the ticket view with the ID in the URL—this restores the
$_GETdata and prevents duplicate submissions from page refreshes:// After successful comment save $ticket_id = $_POST['ticket_id']; header("Location: view_ticket.php?ticket_id=" . urlencode($ticket_id)); exit;
Issue 2: Appending Comments to the Correct XML Ticket Node
To add a new comment only to the target ticket's comment section, you need to locate the specific <ticket> node in your XML and inject the new comment into its <comments> child. Here's a straightforward implementation using PHP's SimpleXML:
- Load your XML file:
$xml = simplexml_load_file('tickets.xml'); if (!$xml) { die("Failed to load ticket data"); } - Find the matching ticket: Use XPath to pinpoint the ticket with the ID from your form submission:
$ticket_id = $_POST['ticket_id']; $target_ticket = $xml->xpath("//ticket[@id='$ticket_id']"); if (empty($target_ticket)) { die("Ticket not found"); } $ticket = $target_ticket[0]; - Create a
<comments>node if missing: If the ticket doesn't have a comments section yet, add it first to avoid errors:if (!isset($ticket->comments)) { $ticket->addChild('comments'); } - Add the new comment: Build a
<comment>node with your desired fields (author, date, content) and attach it to the ticket's comments:$new_comment = $ticket->comments->addChild('comment'); $new_comment->addChild('author', htmlspecialchars($_POST['comment_author'])); $new_comment->addChild('date', date('Y-m-d H:i:s')); $new_comment->addChild('content', htmlspecialchars($_POST['comment_content'])); - Save the updated XML:
if ($xml->asXML('tickets.xml')) { // Redirect back to ticket view (from Issue 1 step) header("Location: view_ticket.php?ticket_id=" . urlencode($ticket_id)); exit; } else { die("Failed to save comment to XML"); }
Quick Best Practices:
- Always use
htmlspecialchars()when inserting user input into XML/HTML to block injection attacks. - Double-check that your XML file has write permissions so the server can save updates.
内容的提问来源于stack exchange,提问作者B.Sabres15
相关产品推荐
相关产品推荐

