简体   繁体   English

使用PHP将数据从表单传输到XML文件

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

I am working on a project for school, so needless to say I am a beginner at all of this. 我正在为一个学校项目工作,所以不用说我是所有这方面的初学者。 I am having trouble passing my form data to my XML file. 我无法将表单数据传递到XML文件。 I am receiving a 我正在收到

Fatal error: Call to a member function writeXML() on a non-object from save-xml.php line 53 致命错误:从save-xml.php第53行调用非对象上的成员函数writeXML()

which points to my define-classes.php . 指向我的define-classes.php I have defined the ticket class in the define-classes file. 我已经在define-classes文件中定义了票证类。 I am writing just a help ticket site where users can input and view tickets. 我只是在写一个帮助票证网站,用户可以在其中输入和查看票证。 below is the code. 下面是代码。

<?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>

The define file 定义文件

// 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;
}
}
?>

The save file 保存文件

    <?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;
 }
 }

?>  

And the XML File 和XML文件

<tickets.xml>
</tickets.xml>

Talk about re-inventing the wheel, I tried using the above code as most of it was provided by my instructor for us to incorporate, however, I found that all this was for not as three simple php commands allowed to me to append into my XML no problem. 谈论重新发明轮子,我尝试使用上面的代码,因为大多数代码是由我的讲师提供给我们并入的,但是,我发现这并不是因为三个简单的php命令允许我追加到我的代码中XML没问题。 I ended up using a mixture of CreateElement(), appendChild(), and createAttribute(). 我最终混合使用了CreateElement(),appendChild()和createAttribute()。 Thank you for the looking at this with me though guys. 谢谢你们和我一起看这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM