1

A table I extract from a DB, have a column dedicated to the path of XML file. I need to open these xml from within the php.

First the column is shown:

<td><a href=\"#\" data-value=\"{$row[3]}\" onclick=\"submitdata(event)\">$row[2]</a></td>

Then a function submitdata(event) is called:

<script type='text/javascript'>
function submitdata(event){
var el=typeof( event.target )!='undefined' ? event.target : event.srcElement;
var data=document.getElementById('data');
data.value=el.dataset.value;
data.parentNode.submit();
}
</script>

And in the php:

if (function_exists('submitdata')){
    $data = addslashes(strip_tags(@$_POST["data"]));
    $myfile = fopen("$data", "r") or die("<h1>Unable to open file!</h1>");
    $xml =  fread($myfile,filesize("$data"));
    $dom = new DOMDocument;
    $dom->preserveWhiteSpace = TRUE;
    $dom->loadXML($xml);
    $dom->formatOutput = TRUE;
    $xml_out = $dom->saveXML();

    echo "<pre class=\"brush: xml\">";
    echo htmlspecialchars($xml_out);
    echo "</pre>";
    fclose($myfile);
?>
<script type="text/javascript">
SyntaxHighlighter.all()
</script>
<?php

} 

This code was working in a old server but I was not using the function_exists() because the xml was shown in another php page. This page's code was exactly the same code wrote above.

The request:

How do I open a XML file, through a link? Or, are there better alternatives?

EDIT:

I'm trying with Ajax, no luck so far. I really appreciate any help in building the Ajax/jquery that can get the name of the file from the <href> (or any other) and open the corresponding xml file in the same bootstrap's column.

aPugLife
  • 989
  • 2
  • 14
  • 25
  • the php method `function_exists()` will of course only find php-functions, not js-functions! – Jeff Oct 05 '16 at 13:05
  • not really, if I invert the function to if `(!function_exists('submitdata')){}` It will print "Unable to open file!". – aPugLife Oct 05 '16 at 13:10
  • Forgot to write, that if I do not invert it but add a ELSE condition like 'ELSE echo "Print something";' - it will "Print something" - quite sure the function_exist() works :/ - thanks anyway for pointing that out! – aPugLife Oct 05 '16 at 13:13
  • Yes, the function works. It will just never be true, cause it will never find 'submitdata'. – Jeff Oct 05 '16 at 13:30
  • Your problem is, that you expect the form beeing submitted without a reload. change `if (function_exists('submitdata')){` to `if(is_set($_POST['data'] {` – Jeff Oct 05 '16 at 13:32
  • Tried, did not work. It didn't even load the page (I am already inside an `if(isset($_POST['submit'])){` but I do not think this is a problem). I reverted like it was before just to have the page showing up, thanks for the explanation by the way! – aPugLife Oct 05 '16 at 13:38
  • @Jeff I corrected your IF with the closing parenthesis, I missed them before while copying your code. The page now loads, of course, but still it can not open the xml. – aPugLife Oct 05 '16 at 14:03
  • Solved thanks to this: http://stackoverflow.com/questions/8169027/how-can-i-submit-a-post-form-using-the-a-href-tag – aPugLife Oct 06 '16 at 12:37

0 Answers0