My webpage allows users to select items from a dropdown list and has a table. Javascript prints out each dropdown-list-item (plus extra info that javascript pulls from the XML file) in a new table row that javascript creates.
I want to be able to write the table information to disk.
If I was using Javascript, I would use document.getElementByID("table") to parse through the nodes and put that info into variables that I could write. However, most documentation I find says Javascript is not used for writing to disk. So, I think I should use php.
What is the php equivalent of Javascript's 'document' object?
In Javascript I don't need to define any new objects to use it, I can use 'document' anytime.
I think in php I have to use new DOMDocument. and then load the html.
Most of the php examples show loadHTML(string) and loadHTMLFile();
I don't want to load HTML from a string that I write inside the tags.
And I don't want to load HTML from the .htm file because that's the original file and Javascript has changed the user's file based on their selections from the dropdown menu.
I want to access the elements of the current document using php.
So, how does php refer to the current document?
The page I'm working on is http://music.collwyncraig.info/hajimama/setlist.htm
I think your thoughts about the architecture are wrong. There is a way how the programming languages interact with each other.
php is a preprocessor and generates - what ever u want - usually html, containing javascript, css, and whatever...
javascript is a dynamic language that interacts in the generated html documents or calls by ajax other services that deliver html, javascript, css or whatever is needed
and html - that is document object that is displayed and can be modified during the runtime by javascript an styled by css, etc...
So when u call in your documents javascript functions - the basic php has already done its job.
Instead of printing all generated contents from php directly to the screen, you can store it first in a var.
For example:
$myContent = "<html><head>...</head><body><h1>Whatever...</h1></body>";
so with that you can do the following to print it on the screen:
print "myContent"; // and it is shown on the screen
or
$myFile = "testFile.html";
$fh = fopen($myFile, 'w') or die("can't open file");
$myContent = "<html><head>...</head><body><h1>Whatever...</h1></body>";
fwrite($fh, $myContent);
$myContent = "Whatever2";
fwrite($fh, $myContent);
fclose($fh);
So the contents of your file would be:
<html><head>...</head><body><h1>Whatever...</h1></body>Whatever2
This generated document can then be called by loadHTML or used in which way you
want.
This just as a hint into which direction you can think - there are quite multiple ways of solution - so without knowing more about what u really want - it is quite not easy to mention a proper solution.
$_SERVER['SCRIPT_NAME'] for the main file.
or
__FILE__ for the full path to the file where this was called.
PHP doesn't have any builtin' DOM Manipulator like jQuery - html output is usually generated through some template engine of your choice or manually.
If you want DOM manipulation like jQuery, you might want to try out PHPQuery
http://code.google.com/p/phpquery/
Related
I want to convert the content of a php file (this file is generated using some query from mysql database along with some image)to html file in order to create a pdf format. I tried converting php file to pdf but could not be succeeded. Kindly help with very short example as I am very new to the area.Thanks in advance.
you can generate a static html file from a url (to the php page) like so:
file_put_contents('static.html', file_get_contents('http://example.com/dynamic.php'));
Though writing the file to disk is probably unnecessary.
Probably your pdf function takes an html sting, in which case include and output buffering might be a suitable solution:
ob_start();
include 'dynamic.php';
$html = ob_get_clean();
create_pdf('mypdf.php', $html);
For a more specific example you would need to show your current code
So I'm making a notepad app in PHP, but I want to add the ability to share the file amongst your peers or something.
It's based on AJAX, and it saves the file automatically, and the file is named to what your IP address is after being hashed in md5.
What I want to do is maybe go to /view/837ec5754f503cfaaee0929fd48974e7, while the actual text file is located at /notes/837ec5754f503cfaaee0929fd48974e7.txt
I know I'll have to use file_get_contents(), but I don't know how to display it on a page.
I could just have it link to the .txt file, but I don't want it raw. I want it to have some style.
How would I go about doing this? Where can I start?
First you would need a way to store a variable in the URL (the file name). This can be easiest done using the querystring.
So the link to a file for your user to see would be '/view/?file=MYFILENAME'
This would then be interpreted by your php (this could also be wrapped in AJAXy goodness) into a path to retrieve the text file from.
view/index.php
//Fetch the file based on the get variable
//Note the relative path
$file = file_get_contents('../notes/'.$_GET['file'].'.txt');
//Print the file. You can also dress it up or wrap it in HTML tags
echo $file;
When displaying the text file, there is some built in functions that will help. Most notable nl2br() which takes the new line characters in a text file and makes them into html <br> tags.
More reading on the GET array can be found here
I'm using PHPPdf library to generate PDFs on the fly in my Symfony2 app. However I could not find a way to dump/write/save PDF raw data to a file.
From Symfony controller I tried this:
$content = $this->render('AcmeBundle:PDF:template.xml.twig');
file_put_contents('documents/123.pdf', $content);
but as it seems I get rendered HTML and not PDF binary data. When I add #Pdf annotation all output is being routed to PDF document and into browser but I need to save raw data instead for later retrieval.
I looked into FacadeBuilder but coudln't find anything useful.
Any help would be much appreciated...
Take a look at this, I think you should use $content = $facade->render($xml); like in the example action.
I'm a beginner here and need help. I have this code, which works and outputs a graph in my browser (if this is the only code in php file). I don't know how to add text below or above just like any other site. When I try, it returns my whole code in the browser. How do I go on about this?
<?php
// content="text/plain; charset=utf-8"
require_once ('jpgraph/src/jpgraph.php');
require_once ('jpgraph/src/jpgraph_line.php');
// Some data
$ydata = array(11,3,8,12,5,1,9,8,5,7);
// Create the graph. These two calls are always required
$graph = new Graph(350,250);
$graph->SetScale('textlin');
// Create the linear plot
$lineplot=new LinePlot($ydata);
$lineplot->SetColor('blue');
// Add the plot to the graph
$graph->Add($lineplot);
// Display the graph
$graph->Stroke();
?>
Thanks in advance!
What you need is quite simply:
<img src="graph.php">
Put that in a separate HTML file or PHP script. You cannot output the image and text in the same script / web page. It needs to be separated.
Don't worry about the .php extension for the image src= attribute. It will display despite the lack of .jpeg extension. (The Graph class already outputs the correct MIME type I assume.)
I'm a bit rusty on PHP, but I believe that all "require" are supposed to be made before any content is output. Otherwise, the normal HTML/XHTML syntax and formatting take precedence.
I haven't actually used this library, but I'm assuming the image is output directly to the browser. It's probably easiest to create a new HTML document (or whatever the rest of your site is powered by) and include this as an image, with the image's src being the name of this script.
I have an RTF file that I want to display inside a web page after tags have been replaced with user input.
I would like to be able to display the RTF file without having to convert it to something before displaying it.
Every time I try it now it gives me the popup open/save box even though I am telling it to display it inline with:
header("Content-type: application/msword");
header("Content-Disposition: inline; filename=mark.rtf");
header("Content-length: " . strlen($output));
echo $output;
Most browsers won't reliably display RTF content. It IS possible to parse the RTF into HTML, and display the HTML content on your web page however.
You need some kind of program to parse RTF and convert it to HTML. I'm assuming it has to be free. I do not know of any reliable free RTF parsing or RTF to HTML libraries in PHP.
I recommend you use a command-line conversion program like RTF2HTML: http://sageshome.net/?w=downloads/soft/RTF2HTML.html
You would need to download and install this program on your webserver, allow the user to upload the file to a temp directory, and then call the command line application from PHP with shell_exec():
$html_output_path = '/path/for/processing/files/'
$html_output_filename = $username . $timestamp;
if (is_uploaded_file($_FILES['userfile']['tmp_name'])
{
shell_exec('rtf2html ' .
escapeshellarg($_FILES['userfile']['tmp_name']) . " " .
$html_output_path . $html_output_filename);
}
$html_to_display = file_get_contents($html_output_path .
$html_output_filename);
Then, parse the results as HTML and display them. Not a bad strategy. Note that you will probably need to remove the head, body and possibly other tags if you're going to display the content inside another web page.
You might want to check out https://github.com/tbluemel/rtf.js for client-side RTF rendering. It's still in its early stages but it renders even embedded graphics. Support for rendering embedded WMF artwork is still very very limited, though, and requires browser support for the tag.
You needed an RTF to HTML converter written in PHP. I think this page contains your solution:
http://www.websofia.com/2014/05/a-working-rtf-to-html-converter-in-php/
First: you've got your content-type wrong. for RTF it's text/rtf
Second: you'll only be able to display in-line this type of content, which can be rendered by the web browser. RTF is not one of these. So you won't be able to display it in-line without converting it, or without some plug-in for the browser. Of course conversion might be on-the-fly.
Web pages can only contain HTML. You would need a browser plugin like flash to display other file types. See Scribd for example.
This isn't exactly an answer to your question, but one thing you may want to do is remove the "filename=mark.rtf" from the header. I've had browsers treat something as a download if I include "filename" in the header, even if the "Content-Disposition" is "inline".
You can't just output a file from within PHP code. You need to extract the data from it, then print the contents inline.
The php function 'file_get_contents' may do what you need. The functions manual is here: http://us2.php.net/filegetcontents
A sample usage is here:
$contents = file_get_contents('yourfile.rtf');