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

时间:2018-04-14 22:56:51

标签: php html xml

我正在为学校做一个项目,所以不用说我是这方面的初学者。我将表单数据传递给XML文件时遇到问题。我收到了

  

致命错误:从save-xml.php第53行调用非对象的成员函数writeXML()

指向我的public class activity_titoli extends AppCompatActivity { Button btt_backHome; ListView lV_titoli; EditText eT_search; private ArrayAdapter<CharSequence> adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_titoli); btt_backHome = (Button) findViewById(R.id.btt_backHome); lV_titoli = (ListView) findViewById(R.id.lV_titoli); eT_search = (EditText) findViewById(R.id.eT_search); adapter = ArrayAdapter.createFromResource(this, R.array.titoli, android.R.layout.simple_list_item_1); lV_titoli.setAdapter(adapter); final Intent to_Home = new Intent (this , Activity_Main.class); eT_search.addTextChangedListener(new TextWatcher() { @Override public void afterTextChanged(Editable arg0) { // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub } @Override public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { (activity_titoli.this).adapter.getFilter().filter(arg0); } }); btt_backHome.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { startActivity(to_Home); } }); lV_titoli.setOnItemClickListener(new AdapterView.OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Log.e("Element: " , Integer.toString(i)); } }); } } 。我在define-classes文件中定义了ticket类。我正在写一个帮助票据站点,用户可以在其中输入和查看票证。下面是代码。

define-classes.php

定义文件      

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

和XML文件

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

?>  

1 个答案:

答案 0 :(得分:0)

谈论重新发明轮子,我尝试使用上面的代码,因为我的教师为我们提供了大部分内容,但是,我发现所有这些都不是因为三个简单的PHP命令允许我附加到我的XML没问题。我最终使用CreateElement(),appendChild()和createAttribute()的混合。感谢你们和我一起看这个。