How to run php file in javascript without using ajax? - javascript

I want to run a php file in javascript when the user refreshes or leaves the page. I'm using a window.onbeforeunload function so if I use ajax, the php file would run without the page reloading. That means the php file would run even when a modal pops up and I don't want that. So I want the php file to run when the user refreshes or leaves the page.
window.onbeforeunload = function (e) {
$.ajax({
type: 'POST',
url: 'myfile.php',
success: function(response){
}
});
}

Vanilla Javascript have no access to filesystem, you can't run php from it.
Using ajax just allows you to get text content from specified url, same result you can get, when put this url in your browser.

Related

Can create a file using PHP, but it won't load on the browser for half an hour

So I'm making a browser based game, and in order to create an account for this game I have the JS file call a PHP file (POST) to write an XML file.
This works, I get the file in cPanel, in the right directory, with the right content. Meaning I can open it, but only in cPanel. When I try to access it via browser I get a 404, but only for about 30 min, then it'll just magically start working.
This same PHP file is called later on in the game to update XML files, and the same thing happens. I can confirm that the PHP works exactly as it should, because I can see that the file/directory is perfect.
Here's the interesting bit, if I create an XML file manually or update it manually, it works instantly. It's only the XMLs created by the PHP file that take forever to load.
It's like the server doesn't realize that there was a change on it, until half an hour after the fact. That is, unless I did it manually.
My PHP:
<?php
$filename=$_POST['fileTo'];
$newfile=fopen($filename,"w")or die('Can not open');
$string=$_POST['stuff'];
fwrite($newfile,$string) or die('Could not write');
fclose($newfile);
?>
My AJAX call:
$.ajax({
type: 'GET',
url: writeDirect,
dataType: 'xml',
success: function(result) {
},
cache:false,
error: function(error) {
$.post('PHP/Accounts/creatAcc.php', { fileTo: userWrite, stuff: writeStuff }, function() {
signIn(userATFS, passCe);
});
}
});
Update:
I've decided to access the games directory directly from the browser. This gets even more interesting.
First thing I did was create a new account called testFile, I get the standard error on the GET because the game can't access the newly created account.
Then I opened the directory in Chrome, this is interesting:
The index clearly shows that testFile.xml exists
Then I try clicking on it, but this is where it breaks
The images 404 despite the file clearly existing
And no, changing the permissions on testFile.xml did not change anything.
I believe I've found the answer. I think it was just that server that was weird like that. I was using x10 basic and decided to switch over to another service and now it works.

Wordpress ajax url ... not send

I have a javascript file that is trying to send POST data to a php file that is in a directory above it in my filesystem:
plugin
|js
/script.js
|saveData.php
script.js:
$.ajax({
type: "POST",
url: '/../saveData.php',
data: {
obj
}, //send data if needed
success:function(){
alert("OK sent data!");
}
This however, doesn't seem to work. How should I be referencing this file? I am also a little bit confused as to where this php file should reside to begin - I need to be able to make calls to the $wpdb class. Any help/advice appreciated!
To use Ajax in WP, you can't use your url, you have to use admin_url( 'admin-ajax.php' ). See more in this link: how to use WordPress Ajax.
The URL needs to be relative to the HTML document the JS is running inside, not to the JS file itself.
If it did need to be relative to the JS file then you would want ../saveData.php. Starting with a / goes back to the root of the webserver (and then the ../ tries to go up a level from there, which is impossible, so that bit is ignored).

Refresh another page

I have an application that sends web requests to a .php file that then creates a flatfile database with the web requested data. Another .php file loads the data from the flatfile and creates a mock css file loadout just outside of the php code. An HTML file then treats the .php file as a .css file.
Quick pseudo of the php that acts as css:
<?php
header("Content-type: text/css");
load stuff1;
load stuff2;
?>
p{font-size: <?=stuff1?>;}
h{font-size: <?=stuff2?>;}
Well, you get the idea right? Basically, what I want to do is to refresh the html file whenever this mock css file written in php is changed. The application that I'm using constantly is sending data to my first php file, and I want to refresh the html file whenever the mock css file is updated. Any ideas on how I can sync this up? I also don't know how to refresh an external page. Any help would be appreciated!
Edit: One step further. If I could somehow update the css without refreshing the page, that would be a huge bonus too!
You need to set a date or cache thing to check if the file is newer than it was before. You can check once a while with AJAX if the file is modified and if the result is true you can refresh your page with jquery or javascript.
$.ajax({
url : "URL_TO_FILE",
type: "GET",
data : formData,
success: function(data, textStatus, jqXHR)
{
//data - response from server
},
error: function (jqXHR, textStatus, errorThrown)
{
}
});
Here is a link so you can change the css file.
http://www.sitepoint.com/jquery-change-css-file-2/

Writing to XML from JavaScript using asp or aspx page

I am writing a bookmarklet that pops up a form and allows users to select xml files. I am reading the file using:
$.ajax({
type: "GET",
url: "http://localhost/products/dataSource.xml",
dataType: "xml",
success: function (xml) {
$(xml).find('node').each(function () {
//get values from xml file
});
}
});
This works well.
My problem is that I want to edit and add to the xml and I understand that JavaScript cannot directly write to the file for security reasons.
I understand that I can write to the xml file using an asp or .aspx page.
Is this still the best way to achieve writing to the xml file and what would the code look like?
I'm looking for the simplest way to achieve my goal.
Yes you're right. You can't do that in javascript since it is being executed in the browser on the client. You should do this in asp.net code behind. You can call the c# method with ajax if it helps. In c# you can do it with XDocument.

JS file in script tag's src attribute downloaded but not executed(from ajax call)

I am invoking an ajax call from jquery like this:
$.ajax({
type: 'GET',
url: 'edit.htm',
success: function(data){
container.html(data);
},
});
The data recevied from the ajax call contains script tags that reference other JS files, for ex: angularjs. Wehn I look at firebug I see that the JS files are downloaded but they do not appear in the Script tab one could debug it.
The JS files are downloaded but not executed.
How do I get around this?
The above ajax call and the container element are present in a html file called info.htm.
And edit.htm(the data fetched from the ajax call) has script tags and other html data.
Thanks.
P.S: If it helps: I can see the JS files being downloaded in the firebug 'Console' tab, however, I cant see them listed in the firebug 'Script' tab.
try with jQuery.getScript("url") for more details refer this
It makes ajax call implicitly.
or try something like :
$.ajax({
type: 'GET',
url: 'edit.htm',
success: function(data){
$(data).appendTo(container);
},
});
I had once a problem with CSS - it had incorrect Content-Type header and was not interpreted by the browser. Can that be the problem? Does your server return text/javascript for the requested script?
May be your requirement is kind of odd because server doesn't return client code.
Best practice is load js file at the start, and you can call required method after completion of ajax script.
However, eval may not be a good practise but this is one solution.
To execute javascript code which is in variable, you need to use eval method.
Please take a look at this reference http://www.w3schools.com/jsref/jsref_eval.asp
hope this helps.

Categories