Saving page as .html and upload to ftp - html

I allow myself to ask the question here cause I looked for what to do regarding my problem but didn't find anything helpful.
Im having a webpage where users can modify information such as text, images etc, and I would like to add a button which would save the page as an .html in its current state (reloading the pas reset everything) and upload it on a ftp so they could have access to it online.
At this point, I know how to get all the html content using js .innerHTML, and how to send a file to the ftp using php, but I don't know what to look for to generate the .html file with the innerHTML content in it.
So here I am, asking for your help to guide me toward the right direction.
Thanks a lot!
Following theCNG27's suggestion, I think now the issue comes from my php script.. Sorry about that but Im really not used to php yet.
here is the php script:
<?php
$handle = fopen("filename.html", "w+");
fwrite($handle, $_POST['HTML']);
$dossier = 'endpoint/';
$fichier = basename($_FILES['HTML']['name']);
if(!isset($erreur))
{
$fichier = strtr($fichier,
'ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ',
'AAAAAACEEEEIIIIOOOOOUUUUYaaaaaaceeeeiiiioooooouuuuyy');
$fichier = preg_replace('/([^.a-z0-9]+)/i', '-', $fichier);
if(move_uploaded_file($_FILES['HTML']['tmp_name'], $dossier . $fichier))
echo 'Upload done !';
}
else
{
echo 'Upload failed !';
}
}
else
{
echo $erreur;
}
?>
it says "no data received"
thanks a lot for the help

So you have Text (in your case the HTML code) in your javascript that you want to transfer to your php script and save it as a file!? Is that correct? If so, I would recommend this:
Since javascript is client-side and php is server-side you should send by POST.
I would use a form like this:
<form name="myform" action="url/to/php/script.php" method="POST">
<button type="submit">Submit!</button>
<input type="hidden" name="HTML" id="htmlsubmit" value="">
</div>
</form>
Now in the js-script you have to update the value of your hidden input like this:
document.getElementById("htmlsubmit").value = htmlcode;
htmlcode represents your HTML code you got through .innerHTML
Now in your PHP script you just have to receive the code and write it to a file:
$handle = fopen("filename.html", "w+");
fwrite($handle, $_POST['HTML']);
Since you already know how to upload a file to ftp via php, just do that with your file "filename.html" ;)
Hope that helps
EDIT: You edited your question and said you need help with the php script... This would be a possible complete script you could use:
<?php
$fname = "filename.html";
if(!isset($_POST['HTML']))
{
echo "Error: No data received...";
}else{
$handle = fopen($fname, "w+");
fwrite($handle, $_POST['HTML']);
fclose($handle);
echo "HTML Code written to file: ".$fname."";
}
?>
Is that all you need in your script?

Using JavaScript, calling innerHTML on the document element will give you the markup of the page as a string, including changes made to the DOM.
var markup = document.documentElement.innerHTML;
Then, you could post this markup to an endpoint using AJAX. The endpoint can receive the string, save it as a html file and then upload it somewhere via FTP.

Related

Reading a webpage with perl's LWP - output differs from a downloaded html page

I try to access and use different pages in NCBI such as
http://www.ncbi.nlm.nih.gov/nuccore/NM_000036
However, when I used perl's LWP::Simple 'get' function, I do not get the same output I get when I save the page manually (with the firefox browser 'save as html' option). What I do get from the 'get' function lacks the data I require.
Am I doing something wrong?
Should I use another tool?
My script is :
use strict;
use warnings;
use LWP::Simple;
my $input_name='GENES.txt';
open (INPUT, $input_name ) || die "unable to open $input_name";
open (OUTPUT,'>', 'Selected_Genes')|| die;
my $line;
while ($line = <INPUT>)
{
chomp $line;
print OUTPUT '>'.$line."\n";
my $URL='http://www.ncbi.nlm.nih.gov/nuccore/'.$line;
#e.g:
#$URL=http://www.ncbi.nlm.nih.gov/nuccore/NM_000036
my $text=gets($URL);
print $text."\n";
$text=~m!\r?\n\r?\s+\/translation="((?:(?:[^"])\r?\n?\r?)*)"!;
print OUTPUT $1."\n";
}
Thanks in advance!
The page at http://www.ncbi.nlm.nih.gov/nuccore/NM_000036 does a lot of JavaScript processing and also loads a bunch of stuff dynamically. LWP::UserAgent does not do that for you as it cannot run JavaScript.
I suggest you take a look at what is happening in your browser, with Firebug or the Chrome Developer Tools. You'll see it does an XHR request to this URL: http://www.ncbi.nlm.nih.gov/sviewer/viewer.fcgi?val=289547499&db=nuccore&dopt=genbank&extrafeat=976&fmt_mask=0&retmode=html&withmarkup=on&log$=seqview&maxdownloadsize=1000000
Now I am not sure how these params translate to the NM_000036, but you should be able to figure that out by looking at some of the JS code that is being run on the page, or trying multiple pages ans looking at the URLs of the XHR calls.
Since this is probably a public service, and I'm assuming you are allowed to take that data, you should consider asking if they have a proper API that you can hit instead of screen scraping the stuff off of their website.
Content you're searching is generated by JavaScript. You need to parse your HTML (from the first response) and find ID for the data you want:
<meta name="ncbi_uidlist" content="289547499" />
Next you need to make another request to the URL in the form: http://www.ncbi.nlm.nih.gov/sviewer/viewer.fcgi?val=ID_YOU_HAVE
Something like this (untested!):
my $URL='http://www.ncbi.nlm.nih.gov/nuccore/'.$line;
my $html=gets($URL);
my ($id) = $html =~m{name="ncbi_uidlist" \s+ content="([^"]+)"}xi;
if ($id) {
$html=gets( "http://www.ncbi.nlm.nih.gov/sviewer/viewer.fcgi?val=" . $id );
$text=~m!\r?\n\r?\s+\/translation="((?:(?:[^"])\r?\n?\r?)*)"!;
print OUTPUT $1."\n";
}

Making on Offline form using JotForm

i'm a beginner and just learning about HTML. I just need a point in the right direction here. Any tutorials you can point me to would be greatly appreciated.
I am trying to create an offline version of this form I created on JotForm-http://form.jotform.us/form/40855701907154
I would like it to save the data a user inputs into an excel file.
I have tried saving the page offline by clicking 'save page as' but the 'submit' button doesn't appear.
I've been trying to understand this tutorial-http://diveintohtml5.info/offline.html.
Do I open the HTML file in notepad and try to edit it? If so, how do i edit it?
If you want to save the data to the Excel file.. you must store it first in database, then display your database in php with this header:
<?php
// excel raw data
header("Content-type: application/vnd-ms-excel");
// define the name "results.xls"
header("Content-Disposition: attachment; filename=results.xls");
// echo your database
include 'database.php';
?>
that is the simplest way for begginer... let me know if this work for you...

Load new HTML page in Parent from iFrame Child

I have an HTML page with an <iframe> that loads an HTML form. The form inside the iFrame posts to a PHP script.
Upon completion of the PHP script, how can it load an entirely different HTML page outside of the iFrame?
not in php, php syntax is
header( 'Location: url' ) ;
but it only works if you havent done anything else yet.
but you can do it with javascript like so
document.location.replace("url")
or
document.location.href = "url";
so with php you can write (after form is submitted)
echo "<script>document.location.replace('url');</script>";
or
echo "<script>document.location.href = 'url';</script>"
edit
i changed window (redirects iframe) to document (redirects page) i should work now, sorry about that.
you can also use top.window.location i think.
a simpler way to do this is in the actual html <form action="url" target="_top">. however this will skip form-validation.

making html web page

I am making a web page with html. I made a choose button that the users can select a file and upload it. I want them to be able to upload a and if the file that they chose is wrong . Is it possible to do that with html syntax what else I should add more?
Thank you,
You can use PHP to do that, make the code to upload a file and if the choice is wrong send a PHP header with you status code :
<?php header("HTTP/1.1 402 Payment Required"); ?>
you need server side code, e.g. in php you can use: http://php.net/manual/de/function.http-response-code.php

What do I have to do for opening and editing existing file with PHP?

I have different HTML files. I want to open, edit and then save changes with PHP (NOT OOP) in admin panel by using HTML textarea tag. What do I have to do for that? Do I need to create new mysql database? Could you please show me an example?
You can read the contents of the HTML file using file_get_contents:
$html = 'example.html';
$currentContents = file_get_contents($html);
// set the textarea text to $currentContents
To write the changes, you will have to post the textarea to a PHP script (through an HTML form) and then do something like:
$newContents = $_POST['textareaName'];
$html = 'example.html';
$fh = fopen($html, 'w') or die("File could not be opened.");
fwrite($fh, $newContents);
fclose($fh);
There are some security things you need to worry about it, but this is a basic example of how to achieve your goal. Good luck!
http://us.php.net/file_get_contents
http://us.php.net/fwrite