LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-24-2010, 09:46 AM   #1
eoc92866
LQ Newbie
 
Registered: Oct 2010
Posts: 24

Rep: Reputation: 0
xml form uploading help


hi,

i'm trying to create simple form that will also upload only xml data.

first, the user will need to upload an xml document only by clicking submit, the data will be posted to the server and the user will be redirected to uploader.php. This PHP file is going
to process the form data and do all the work.

PHP Code:
#upload.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd"
>
<
html>

<
body>

<
form enctype="multipart/form-data" action="uploader.php" method="POST">

<
input type="hidden" name="MAX_FILE_SIZE" value="500" />
Please select the correct file that you require to create an XML export.
<
br/>
<
label for="uploadfile">Filename:</label>
<
input name="uploadfile" type="file" size="100"/>
<
input type="submit" value="Upload File" />

</
form>



</
body>

</
html

second, i'd like to process the data and post the information to another php document that will parse the xml document.

this particular file is not getting past the first if statement. i'm not really seeing why???

PHP Code:
#uploader.php

<?php
//File upload restrictions parameters
//Will only restrict to allowing xml documents to be uploaded


if ((($_FILES["file"]["type"] == "application/xml")
    {
    if (
$_FILES["file"]["error"] > 0)
        {
        echo 
"Return Code: " $_FILES["file"]["error"] . "<br />";
        }
    else
        {
        echo 
"Upload: " $_FILES["file"]["name"] . "<br />";
        echo 
"Type: " $_FILES["file"]["type"] . "<br />";
        echo 
"Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
        echo 
"Temp file: " $_FILES["file"]["tmp_name"] . "<br />";


//Save the uploaded files to a directory
//If the file already exists with the same date stamp, do not upload
        
if (file_exists("../upload/" $_FILES["file"]["name"]))
            {
            echo 
$_FILES["file"]["name"] . " already exists. ";
            }
//If the file does not exist, create a new entry
        
else
            {
            
move_uploaded_file($_FILES["file"]["tmp_name"],
            
"../upload/" $_FILES["file"]["name"]);
            echo 
"Stored in: " "../upload/" $_FILES["file"]["name"];
            }
        }
    }
//Create an error messag
else
    {
    echo 
"This is an invalid file. Please make sure an XML document has been selected to upload. Thanks.";
    }


// make an error handler which will be used if the upload fails
function error($error$location$seconds 20)
{
        
header("Refresh: $seconds; URL=\"$location\"");
        echo 
'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"'."\n".
        
'"http://www.w3.org/TR/html4/strict.dtd">'."\n\n".
        
'<html lang="en">'."\n".
        
'       <head>'."\n".
        
'               <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">'."\n\n".
        
'               <link rel="stylesheet" type="text/css" href="stylesheet.css">'."\n\n".
        
'       <title>Upload error</title>'."\n\n".
        
'       </head>'."\n\n".
        
'       <body>'."\n\n".
        
'       <div id="Upload">'."\n\n".
        
'               <h1>Upload failure</h1>'."\n\n".
        
'               <p>An error has occured: '."\n\n".
        
'               <span class="red">' $error '...</span>'."\n\n".
        
'               The upload form is reloading</p>'."\n\n".
        
'        </div>'."\n\n".
        
'</html>';
        exit;
// end error handler


?>

third, once the simple file parses the information.. i'd like to display a dropdown menu that will allow a person to select what entry they'd like to be displayed, namely page to be redirected to.

PHP Code:
#dropdown.php

<?php
$drop 
$_REQUEST['dropdown'];
?>



<html>
<body>

<form action="results.php" method="POST" name="links">
<select name="dropdown" value="options">
<option value"xmlSelection2.php">Select a Format</option>
<option value="1.php">1</option>
<option value="2.php">2</option>
<option value="3.php">3</option>
<option value="4.php">4</option>
<option value="5.php">5</option>
</select>

<br/>
<br/>
<input type="submit" value="Submit Query"/>

</body>
</html>


fourth part I, based on the above information... the user should have a page with a include("livesearch.php") for 1.php, but i will place livesearch.php code below...
PHP Code:
#livesearch.php

$target_path $target_path basename($_FILES['uploadedfile']['tmp_name']);


$xmlDoc=new DOMDocument();
$xmlDoc->load("uploader.ph");

$x=$xmlDoc->getElementsByTagName('link');

//get the q parameter from URL
$q=$_GET["q"];

//lookup all links from the xml file if length of q>0
if (strlen($q)>0)
    {
    
$hint="";
        for(
$i=0$i<($x->length); $i++)
             {
            
$y=$x->item($i)->getElementsByTagName('PPC');
            
$z=$x->item($i)->getElementsByTagName('url');
                if (
$y->item(0)->nodeType==1)
                    {
                    
//find a link matching the search text
                    
if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q))
                        {
                        if (
$hint=="")
                            {
                            
$hint="<a href='" .
                            
$z->item(0)->childNodes->item(0)->nodeValue .
                            
"' target='_blank'>" .
                            
$y->item(0)->childNodes->item(0)->nodeValue "</a>";
                            }
                    else
                            {
                            
$hint=$hint "<br /><a href='" .
                            
$z->item(0)->childNodes->item(0)->nodeValue .
                            
"' target='_blank'>" .
                            
$y->item(0)->childNodes->item(0)->nodeValue "</a>";
                            }
                        }
                        }
            }
    }

// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint=="")
    {
    
$response="no suggestion";
    }
else
    {
    
$response=$hint;
    }

//output the response
echo $response;
?> 

fourth part II, the above code will be part of 1.php... this part of the form will ideally return a livesearch query from the uploaded xml document for the checked boxes by an element's name.

PHP Code:
#form.php

<html>

<head>

<script type="text/javascript">
function showResult(str)
{
if (str.length==0)
  {
  document.getElementById("livesearch").innerHTML="";
  document.getElementById("livesearch").style.border="0px";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("livesearch").innerHTML=xmlhttp.responseText;
    document.getElementById("livesearch").style.border="1px solid #A5ACB2";
    }
  }
xmlhttp.open("GET","livesearch.php?q="+str,true);
xmlhttp.send();
}
</script>

</head>


<body>

<?php

echo "<form>
Please place element1 # to search:
<input type='text' size='30' onkeyup='showResult(this.value)' />
<div id='livesearch'></div>
</form>"
;


// This is to collect box array value as global_variables is set off in PHP5 by default
//
// variable that will send $box information from the checkboxes named getXMLbox to
// an variable array
$box=$_POST['getXMLbox'];
        if(empty(
$box))
                {
                echo(
"These are the boxes that WERE not selected: $box");
                }
        else
                {
                
$N count($box);
                echo(
"These are the boxes that ARE selected: $N ");
                        for(
$i=0$i<$N$i++)
                                {
                                echo(
$box[$i] . " ");
                                }
                }

while (list (
$key,$val) = @each ($box))
        {
        echo 
"$val,";
        }

echo 
"<form method=post action='simple.php'>";
echo 
"<table border='0' cellspacing='0' style='border-collapse: collapse' width='100' >

<tr bgcolor='#ffffff'>
<td width='25%'><input type=checkbox name=getXMLbox[] value='element1'></td>
<td width='25%'>&nbsp;element1</td>
</tr>

<tr bgcolor='#ffffff'>
<td width='25%'><input type=checkbox name=getXMLbox[] value='element2'></td>
<td width='25%'>&nbsp;element2</td>
</tr>

<tr>
<td><br/></td>
</tr>

<tr>
<td colspan =6 align=center><input type=submit name=SubmitForm value='Submit Query'></form></td>
</tr>
</table>"
;

?>

finally, i'd like to parse the data with a php5 function and display the xml information onto the screeen or have a dialog box open up for a person to save as.

PHP Code:
#simple.php

<?php
/*
we will use simpleXML to transfer xml data
to parse and read xml data
*/

//load the xml file to $file
$file = ($_FILES["file"]["name"]);

#load xml data
$xml simplexml_load_file($file) or die ("The connection to could not load the XML document. Please try again. \n");

#display the entire document
echo ('$xml');
?>

so basically, i'm a bit lost and not getting the right outcome that i'd like. can someone please assist?

many thanks.
 
Old 12-11-2010, 02:42 PM   #2
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
Code:
if ((($_FILES["file"]["type"] == "application/xml")
Why don't the brackets match up ?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Conversion of Arabic presentation form to normal (isolated) form graemef Programming 0 09-07-2008 05:48 PM
PHP: build query from form entry, then display results in the same form tonedeaf1969 Programming 4 06-22-2007 07:55 AM
shell script works form command line but not form crontab saifee General 1 10-14-2004 10:27 AM
how do I copy a whoel folder form one directory to another form the command line? zwyrbla Linux - Newbie 8 08-24-2004 06:40 PM
I want Linux source code for FAT file system in user readable form not in binary form ramya272 Linux - Newbie 5 02-05-2004 07:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration