 |
 |
Form output to XML file
|
 |
|
 |
|
Mac Elite
Join Date: Mar 2002
Location: Clogland
Status:
Offline
|
|
I've been generating some xml with a php form, works fine until I want to give the page generated an .xml extension, when it throws up this error (405)
Method Not Allowed
The requested method POST is not allowed for the URL /xml/xml_output.xml.
--------------------------------------------------------------------------------
Apache/1.3.27 Server at localhost Port 80
Anyone know of a workaround (without delving into the php.ini configuration)
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Mar 2002
Location: Clogland
Status:
Offline
|
|
Came to the conclusion that it's a logical problem, an xml file isn't going to be able to behave like a php file, so it's looks like I'll be delving into "fopen()","fgets()","fputs()" etc.
Edit: changing smiley from  to  I got it working!!!!
(Last edited by skalie; Mar 17, 2005 at 03:19 AM.
)
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Nov 1999
Status:
Offline
|
|
How did you get it working?
My solution would have been to change the .xml file extension to .php, and then had the PHP output a "Content-Type: text/xml" header. Assuming you don't need to save the file to your server, this should have worked fine. Unfortunately, Apache isn't smart about file types using anything other than filename extensions.
|
|
You are in Soviet Russia. It is dark. Grue is likely to be eaten by YOU!
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Mar 2002
Location: Clogland
Status:
Offline
|
|
Originally posted by Millennium:
How did you get it working?
I have an "inbetween" .php page, that throws the data into the .xml file.
My solution would have been to change the .xml file extension to .php, and then had the PHP output a "Content-Type: text/xml" header. Assuming you don't need to save the file to your server, this should have worked fine. Unfortunately, Apache isn't smart about file types using anything other than filename extensions.
I will be needing to save the xml on a server, so I'm glad I took that route. (Would have hated it if it was only a matter of changing the header)
I'll post what I produce once it's looking pretty.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Mar 2002
Location: Clogland
Status:
Offline
|
|
OK, as promised, although it is not pretty (beta version, bugz not even looked at yet)....
First the form generator, using an array of nodes, and a function to produce a family of child nodes....
Code:
// first an array and loop for fixed attributes
$staticElements = array("ID", "Module_title", "Module_code", "Author", "Level", "Purpose", "Preferred_entry_level", "Special_requirements");
$numElements = count($staticElements);
for($i=0; $i < $numElements; $i++)
{
echo "<tr><td>";
echo "$staticElements[$i]<br />";
echo "</td><td>";
echo '<input type="text" name=element'.$i.' />';
echo "</td></tr>";
echo '<input type="hidden" name="staticElements'.$i.'" value='.$staticElements[$i].'>';
}
echo '<input type="hidden" name="numElements" value='.$numElements.'>';
// third parameter is the number of parent nodes with child nodes
// done by hand for the moment
xmlSubgroup("General_aims",3,0);
xmlSubgroup("Assignments",4,1);
xmlSubgroup("Units",7,2);
?>
<tr><td></td><td><input type="submit" value="submit" /></td></table>
</table>
</form>
</body>
</html>
the function
Code:
<?php
function xmlSubgroup($title,$number_nodes,$num_subgroups){
echo '<tr><td colpsan="2"><i><br /> '.$title.'</i></td></tr>';
for($i=0;$i<$number_nodes;$i++){
echo "<tr><td>";
echo $title.' '.$i.'<br />';
echo "</td><td>";
echo '<input type="text" name="'.$title.$i.'"><br />';
echo "</td></tr>";
}
$titles = array();
$titles[] = "title".$num_subgroups;
$numbnodes = array();
$numbnodes[] = "numnode".$num_subgroups;
echo '<input type="hidden" name="'.$titles[0].'" value="'.$title.'">';
echo '<input type="hidden" name="'.$numbnodes[0].'" value='.$number_nodes.'>';
echo '<input type="hidden" name=num_subgroups value='.$num_subgroups.'>';
}
?>
The "transfer" php page...
Code:
<?php
include 'xml_functions.php';
if(file_exists("xml_output.xml")){
$holder='<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
// open top node
$holder.='<module>';
// elements from array
for($i=0;$i<$numElements;$i++){
$holder.='<'.$_POST["staticElements".$i.""].'>';
$holder.=$_POST["element".$i.""];
$holder.='</'.$_POST["staticElements".$i.""].'>';
}
// form loop, to make groups of child nodes
for($j=0;$j<$num_subgroups+1;$j++){
$nodes = '$numnode'.$j;
eval("\$nodes = \"$nodes\";");
$vari = '$title'.$j;
eval("\$vari = \"$vari\";");
$holder.='<'.$vari.'>';
// outputting child nodes
for($i=0;$i<$nodes;$i++){
$holder.='<'.$vari.$i.'>';
$holder.=$_POST[$vari.$i];
$holder.='</'.$vari.$i.'>';
}
$holder.='</'.$vari.'>';
}
// closing head node
$holder.='</module>';
$remote_file = fopen("xml_output.xml","w");
fputs($remote_file,$holder);
fclose($remote_file);
}else{
echo "no luck <br />";
}
echo '<br /><a href="xml_output.xml">xml_output</a>';
?>
as long as an "xml_output.xml" file resides in the same directory, it works.
I know I'm probably re-inventing the wheel here, but I couldn't find any form to xml resources on the web, not user friendly looking ones anyhow.
One can see the code in practise here http://www.scraf.com/xml/xml_form_generator.php
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|