簡體   English   中英

使用PHP將數據從表單傳輸到XML文件

[英]Transfer Data from form to XML file using PHP

我正在為一個學校項目工作,所以不用說我是所有這方面的初學者。 我無法將表單數據傳遞到XML文件。 我正在收到

致命錯誤:從save-xml.php第53行調用非對象上的成員函數writeXML()

指向我的define-classes.php 我已經在define-classes文件中定義了票證類。 我只是在寫一個幫助票證網站,用戶可以在其中輸入和查看票證。 下面是代碼。

<?php
// load required files
require "define-classes.php";
require "load-xml.php";
require "save-xml.php";

// save form to xml file
save_xml( $_POST, "tickets.xml", "append" );
?>
<link rel="stylesheet" href="site.css">
</head>
<body>
<?php include 'header.php'; ?>
<?php include 'nav.php'; ?>
<main>
    <div class="row">
        <div class="sidel">
        </div>
        <div class="main">
            <h2>Create a New Ticket</h2>
            <form name="newTicket" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
            <fieldset id="custInfo">
                <legend><h3>Customer Information</h3></legend>
                <label>First Name:<br> 
                <input type="text" name="fname" id="fname" size="40"   required></label>
                <br>
                <label>Last Name: <br>
                <input type="text" name="lname" id="lname" size="40" required></label>
                <br>
                <label>Phone: <br>
                <input type="text" name="phone" id="phone" size="40" required></label>
                <br>
                <label>School / Building: <br> 
                <input type="text" name="loc" id="loc" size="40" required></label>
                <br>
                <label>Room Number: <br>
                <input type="text" name="room" id="room" size="40" required></label>
            </fieldset>

            <fieldset id="devInfo">
                <legend><h3>Device Information</h3></legend>
                Device Type: <br>
                <select size="1" name="device" id="device" required>
                    <option size="30" value="">Select Device </option>
                    <option value="Laptop">Laptop</option>
                    <option value="Desktop">Desktop</option>
                    <option value="Touch TV">Touch TV</option>
                    <option value="Projector">Projector</option>
                    <option value="Phone">Phone</option>
                    <option value="iPad">iPad</option>
                    <option value="Chromebook">Chromebook</option>
                </select>
                <br>
                <label>Manufacturer: <br>
                <input type="text" name="manu" id="manu" size="40"></label>
                <br>
                <label>Model:<br>
                <input type="text" name="model" id="model" size="40"></label>
                <br>
                <label>Serial Number: <br>
                <input type="text" name="serial" id="serial" size="40"></label>
                <br><br><br>
            </fieldset>

            <fieldset id="description">
                <legend>Description</legend>
                Let Us Know What You Are Experiencing:<br>
                <textarea name="comments" id="comments" cols="60" rows="10" required>Enter the Description of your Ticket here!</textarea>
                <input type="hidden" name="status" value="open">
                <input type="submit" value="Create New Ticket">
                <input type="reset" value="Clear Form">
            </fieldset>
            </form>
        </div>
        <div class="sider">
        </div>
    </div>
</main>
<?php include 'footer.php' ?>
</body>
</html>

定義文件

// Define the tickets class

class ticket
{
// required id property
var $id;

// define sub properties 
var $fname;
var $lname;
var $phone;
var $loc;
var $room;
var $device;
var $manu;
var $model;
var $serial;
var $status;
var $comments = array();


// constructor 
function ticket ($newID)
{
    $this->id = $newID;
}

// convert object to XML string
function writeXML()
{
    // Define some special characters to help format output
    $tab = "\t";
    $newline = "\n";
    // opening tag 
    $xml_string = $newline . $tab . '<ticket id="' . $this->id . '">' . $newline;

    // sub tags
    $xml_string .= $tab . $tab . '<fname>' . $this->fname . '</fname>' . $newline;
    $xml_string .= $tab . $tab . '<lname>' . $this->lname . '</lname>' . $newline;
    $xml_string .= $tab . $tab . '<phone>' . $this->phone . '</phone>' . $newline;
    $xml_string .= $tab . $tab . '<loc>' . $this->loc . '</loc>' . $newline;
    $xml_string .= $tab . $tab . '<room>' . $this->room . '</room>' . $newline;
    $xml_string .= $tab . $tab . '<device>' . $this->device . '</device>' . $newline;
    $xml_string .= $tab . $tab . '<manu>' . $this->manu . '</manu>' . $newline;
    $xml_string .= $tab . $tab . '<model>' . $this->model . '</model>' . $newline;
    $xml_string .= $tab . $tab . '<serial>' . $this->serial . '</serial>' . $newline;
    $xml_string .= $tab . $tab . '<status>' . $this->status . '</status>' . $newline;

    // loop for comments array 
    foreach ($this->comments as $comments)
    {
        $xml_string .= $tab . $tab . '<comments>' . $comments . '</comments>' . $newline;
    }
    // closing tag
    $xml_string .= $tab . '</ticket>' . $newline;

    // return xml string
    return $xml_string;
}

// function to add data to properties 
function addData ($prop, $new_value)
{
    // convert property to lower case
    $prop = strtolower($prop);

    // check if the property has been defined as an array
    if ( is_array($this->$prop) )
    {
        // if array add a new element to the end of the array
        $temp_array = array( $new_value );
        $this->$prop = array_merge ( $this->$prop, $temp_array );
    }
    else
        $this->$prop = $new_value;
}
}
?>

保存文件

    <?php

// You can call the function save_xml() to output an array of objects into 
// a file in XML format.  The function requires three arguments.
// The first argument should be the name of the PHP array to save.  
// The second argument is the name of the XML file to save the content in. 
// The third argument should be either "overwrite" or "append", depending on
// whether you wish to replace the existing file with just the contents 
// of the current array, or if you want to add the contents of the current
// array to the end of the existing file.
function save_xml ($object_array, $filename, $type_of_save)
{
// if you are appending data, open the existing file and remove the 
// closing root tag so that more data objects can be appended.
if ($type_of_save == "append")
{
    // read in the old contents as a single string
    $old_file_contents = file_get_contents($filename);

    if (!$old_file_contents)
    {
        die("Error opening file $filename!");
    }

    // find the position of the closing root tag, and if found,
    // make a substring starting at the beginning and ending 
    // before the closing root tag, then output it back to the file. 
    $end_tag_position = strpos( $old_file_contents, "</$filename>");
    if (!$end_tag_position === false)
    {
        $new_file_contents = substr( $old_file_contents, 0, $end_tag_position );
        file_put_contents($filename, $new_file_contents);
    }

    // re-open the file to append new content.
    $fp = fopen($filename, "a+b");
    if (!$fp) { die("Error opening file $filename."); }
}
else
{
    // if the type_of_save is not append, open the file and overwrite it.
    $fp = fopen($filename, "w+b");
    if (!$fp) { die("Error opening file $filename."); }

    // output the XML declaration and the opening root element tag.
    write_line($fp, "<?xml version=\"1.0\"?>\n\n");
    write_line($fp, "<$filename>\n");
}

// output the array objects to the file, using the writeXML method of the class.
foreach ($object_array as $current_object) 
{
    write_line($fp, $current_object->writeXML());
}

// output the closing root tag
write_line($fp, "\n</$filename>");

fclose($fp);
}


// This function writes the content to the specified file, and provides
// an error message if the operation fails.
function write_line ($fp, $line)
{
if (!fwrite($fp, $line)) 
    die ("Error writing to file!");
}

// The function file_put_contents from the PHP web site does not exist 
// in our server's version of PHP.  This creates it.
if (!function_exists('file_put_contents')) 
{
define('FILE_APPEND', 1);
function file_put_contents($filename, $content, $flags = 0) 
{
   if (!($file = fopen($filename, ($flags & FILE_APPEND) ? 'a' : 'w')))
       return false;
   $n = fwrite($file, $content);
   fclose($file);
   return $n ? $n : false;
 }
 }

?>  

和XML文件

<tickets.xml>
</tickets.xml>

談論重新發明輪子,我嘗試使用上面的代碼,因為大多數代碼是由我的講師提供給我們並入的,但是,我發現這並不是因為三個簡單的php命令允許我追加到我的代碼中XML沒問題。 我最終混合使用了CreateElement(),appendChild()和createAttribute()。 謝謝你們和我一起看這個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM