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/
Related
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.
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.
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).
I am trying to add some emails taken from an inputbox into a .txt file present on my webserver. Here is the code :
email = document.getElementById("mail").value;
$.ajax({
url: 'maillist.txt',
datatype: 'text',
type: 'PUT',
data: email + '; ',
success: function(data) {
alert('Should have work yay!');
}
});
but that doesn't work on any browser. :(
I have tried using javascript classic methods but it was a no go as well...
I would need either a PUT or POST method, either jQuery or JS, to be able to do this on internet explorer 8 and up as well as firefox and chrome. Emails should appear in the text file as
email1#cooldomain.com; email2#cooldomain.com; .....
Just so it works with our in-house VBA Macro. :)
Also, could there be a method for dropping data into XML files (aka create a new XML entry with form data)? And also, is it possible to upload a file from client side to server side using jQuery? Because i would need users to fill up forms and drop their data into an XML file, and link up a file they choose with that. That way they could add stuff into the XML themselves and they would show up brand new into the webpage.
Kindoff "reddit" or "4chan" like if you know the references.
Thanks for your time, really appreciated!
You can't post from a browser to a text file on the server side. You need to have some sort of code on the server side that will receive the HTTP PUT, and persist the data to a file local to the server.
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.