Jump to content

php form uploading xml help


eo92866

Recommended Posts

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.

 

 

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

 

 

#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.

 

 

#dropdown.php

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



<html>
<body>

<form action="results.php" method="POST" name="links">
<select name="dropdown" value="options">
<option value"form.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...

 

#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.

 

#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%'> element1</td>
</tr>

<tr bgcolor='#ffffff'>
<td width='25%'><input type=checkbox name=getXMLbox[] value='element2'></td>
<td width='25%'> 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.

 

 

#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.

Link to comment
Share on other sites

OK, take this one step at a time...  You can validate the file in the first page (upload.php) before it even gets sent to the next page with javascript:

Add this to your <form> tag:

onSubmit="return validateForm();"

Add this to your header:

<SCRIPT language="JavaScript">
function validateForm(){
if(document.forms.uploadfile.value.lastIndexOf(".xml")==-1) {
	alert("You can't upload a non .xml file!");
	return false;
}
</SCRIPT>

I just copied that from one of my pages, I change the file element's name but you may have to do something with the "forms" part or give your form a name, I never tried doing this with a nameless form.  There also maybe better or easier ways to do this but this works for me and should do what you want it to.

Link to comment
Share on other sites

i'm trying to output to a mysql db and to the screen, so a person can save the xml data.

 

i'd like to save the entire xml document to the db, but only output certain elements to the screen. i was hoping to pair that information down utilizing the livesearch.php file. i'm not sure if that would be the ideal way???

 

finally, i believe i have the print to screen statement in the simple.php file.

echo ('$xml');

 

does this help???

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.