Save html-page to MySQL DB after being modified with javascript - php

I want to save an entire HTML-page after it's being modified with Javascript. It's a week schedule, written by my predecessor, that has to be saved per user in a MySQL Database. I've been searching on this topic for hours but haven't found anything useful.
Does anyone have a clue if this is possible? And if so, how this can be done easily?

You can get actual page HTML with Javascript:
var html = document.getElementsByTagName('html')[0].innerHTML
And then make a POST request to serverside script which will save that page to database.

Obtain the source from the DOM with javascript:
document.getElementsByTagName('html')[0].innerHTML
Then send an Ajax request to your PHP script that stores the value into the database:
var formData = new FormData();
formData.append("html", document.getElementsByTagName('html')[0].innerHTML);
var oXHR = new XMLHttpRequest();
oXHR.open("POST", "http://foo.com/saveHTML.php");
oXHR.send(formData);
In your PHP script:
$html = $_POST['html'];
... # store it

Related

PHP performance issue require_once AJAX event handler

I have a PHP file that handles my post data from forms however I seem to be having some kind of performance/memory issue when I post a form that uses another PHP file via require_once. The first time the post is handled and the performance is fine but with every subsequent post it gets slower and slower until the browser window hangs and I see some busy php cgi threads on the IIS server.
I am using PHP 5.6 (php-5.6.11-Win32-VC11-x86) and jQuery 2.1.4.
Short of posting the entire code basically when the submit button is pressed jQuery makes an XHR request and the form is passed to a form_handler.php which then decides how to handle the data.
All forms that are processed within that file work fine, however I have a forms that require extended processing I am trying to offload that to another PHP file. The code that manages this looks like this:
if($options[0] == "processor"){
//Data can not be handled and must use processor
$sql = "select processor_file from dbo.processor where reference_id = '".$options[1]."'";
$result = query_to_field($sql);
$processor = '../processors/'.$result;
if(file_exists($processor)) require_once $processor;
else echo "Error Processor Not Found<br/>";
}
The specified file then does some stuff and generates the the HTML that is returned via an echo. I am not sure whether require_once is the issue and I should be looking to use curl or exec to get the output from the other file.
The post received is one field and the processor file is and modified copy of this example.
I have modified it to use preg_match instead of ereg and to build $html instead of all the echo’s and prints so that I can just echo the $html when complete.
Any guidance that can be offered would be appreciated.
As suspected by some of the comments I was indeed looking in the wrong place, and the issue turned out to be the JS that was adding the form event handler. The other forms on the site only really get called and submitted once which is why I hadn't spotted the issue before. Basically as the the event handler was just being re-applied when the content changed is was causing the submit to multiply the AJAX request until the point the client said no more!
Bad Code:
function formhandlers(){
$('form').submit(function(e) {
// get the form data
var id = $(this).attr('id');
// process the form
var dsttarg = document.getElementById(id).getAttribute('target');
var fd = new FormData(document.getElementById(id));
postdata(fd,encodeURI('./include/post_handler.php?function='+id),dsttarg);
e.preventDefault();
});
}
Good code:
function formhandlers(){
$('form').off('submit').on('submit', function(e) {
// get the form data
var id = $(this).attr('id');
// process the form
var dsttarg = document.getElementById(id).getAttribute('target');
var fd = new FormData(document.getElementById(id));
postdata(fd,encodeURI('./include/post_handler.php?function='+id),dsttarg);
e.preventDefault();
});
}
The key difference being that that handler is turn off if it already exists.

Action Script 3. How to get data from swf (as) to php and write to database

I'm creating Flash "memory" game, Idea to discover 2 equal cards. Here is timer which count time in how many seconds all cards are discovered. After complete game timer stops. How can I get this time from swf file and write to database? As I understand I need take this data with php file, but don't have idea how to do It.
As I know I need to do something like that:
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, scoreSaveResponse,false,0,true);
var request:URLRequest = new URLRequest("http://mysite.com/score.php");
var urlVars:URLVariables = new URLVariables();
urlVars.time = timer.currentCount;
urlVars.userName = "yourUserName";
request.method = URLRequestMethod.POST;
urlLoader.data = urlVars;
urlLoader.load(request);
function scoreSaveResponse(e:Event):void {
//whatever you return from the php page is found in urlLoader.data
}
//LondonDrugs_MediaServices's code
But could someone explain me how to do It, because If I use this part of code nothing happens, I don't get any errors, but I don't know how to use this in php or how to write it to database.
My app will be used for Facebook, that's mean username will be used the same as in Facebook. So I think first i need to get time to PHP than get username from Facebook and write both to database.
Thank you for answers.
Try to use AMFPHP.
AMFPHP makes it possible to write a service for your application where you can store your SWF values into a PHP database.
http://www.silexlabs.org/amfphp/

Accessing ArrayBuffer from PHP $_POST after xmlHTTPrequest send()

I'm following the tuitions on XMLHttpRequest 2 from :
https://developer.mozilla.org/en/DOM/XMLHttpRequest/Sending_and_Receiving_Binary_Data
and
http://www.html5rocks.com/en/tutorials/file/xhr2/#toc-send-arraybuffer
They're great tutorials for the client side, and here is a working extract from my script:
var imagebuffer = new ArrayBuffer(size); // create the readonly memory buffer
var imagedata= new Uint8Array(imagebuffer); // create a view to manipulate data
// do some cool stuff with imagedata
var exchange=new XMLHttpRequest();
exchange.open("POST",url,true);
exchange.send(arraybuffer);
So far so good, and I can see from the both client and server control panels that plenty of data is being transferred.
Here's my problem: how do I access the ArrayBuffer with PHP at the server?
I'm used to the $_POST superglobal wanting parameters passing from a HTML form so it can be accessed as an array but I can't find any reference for how to access this binary array and stick it in my MySQL database.
Okay - I've figured it out. My server side PHP opens with:
$data = file_get_contents('php://input');
$mysql_blob = base64_encode($data);
which is now in a format ready for inserting (for example) into MySQL as a BLOB format.
Works like a charm!

string from php script to flash

I'm using facebook php sdk to get friend list of an user. But I'm strugling to get that list to flash.
The idea is that the list is always new, depending from the user loading the application. So what I need is one .html file which is loaded with php script to proccess friend list and embedded flash .swf to which php would pass information.
How can I achieve this? thanks
There are two ways to do it:
Set the flashvars to the data you want to pass to the Flash file. Since the friend list rarely changes, that should work.
Or use a URLLoader:
var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("http://example.com/yourscript.php");
loader.addEventListener("complete", loader_complete);
loader.loader(request);
private function loader_complete(event:Event):void {
var scriptData:String = event.currentTarget.data;
// process data
}

Passing variable FROM flash to HTML/php

I was hoping maybe someone could provide some insight on a problem I'm having a tough time deciding how to solve.
I have a rather simple flash application users can make a quick username when connected, and the username is created inside the flash swf.
Now, I have a cron job deleting inactive usernames every ten minutes (on my mysql database where these usernames are all stored and accessed by the other people online) which is fine. But it can still get cluttered up if a bunch of people sign off at once, there is still that 10 minute window before the cron job clears them.
The users have an option to click log out in the flash application which is fine and works great. But of course many choose not to click log off they just click the browser x.
I've looked into onbeforeunload and jquery's .unload but I still need a way to get the username variable that's IN flash INTO the HTML, then use a php script to run the delete username mysql query.
Is there an easier solution?
If not, any insight on how I might pass the username variable to the html to hold onto it after the user makes their username so it can be involved with the .unload function running the php script?
EDIT::::: Maybe is there a way to create a UNIQUE string of numbers with php then pass that var to flash to include with the mysql row then since i already have that var since it was created on the html side, just along with the unload, have it delete the row that has that unique id?
If anyone things this idea would be the best approach, and if i used something like md5(uniqid(microtime()) . $_SERVER['REMOTE_ADDR'] . $_SERVER['HTTP_USER_AGENT']) to make a random iD how could i go about storing the result in a var i could place in the flash vars param then again in the jquery unload or javascript onbeforeunload if that would be better . im just more familiar with jquery
You could actually invoke an ExternalInterface command to populate a hiddenfield in your HTML coming from your SWF component.
if (ExternalInterface.available)
{
var js:String = "yourJavaScriptFunction";
var valToPass:String;
ExternalInterface.call(js(valToPass));
}
And in your HTML page, you write a javascript function:
<script language="javascript" type="text/javascript">
function yourJavaScriptFunction(valToPass)
{
document.getElementById('yourHiddenField').value = valToPass;
}
And from the unload event fired up by your page, you can access the value which was passed from your SWF.
Take note that you can call the javascript function from your SWF as soon as you get the login credentials of your user.
Hope this helps.
You want to use a combination of URLRequest, URLVariables and URLLoader to do this. See the below.
var myData:URLRequest = new URLRequest("some.php");
myData.method = URLRequestMethod.POST;
var variables:URLVariables = new URLVariables();
variables.username = 'CREATED USERNAME';
myData.data = variables;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.COMPLETE, dataOnLoad);
loader.load(myData);
function dataOnLoad(evt:Event){
trace('Completed');
}
This would then call the 'some.php' file with your username stored in '$_POST['username']'. Your php script can then make an impression on the db linking that username to that session (however you want to do that).

Categories