Jump to content
MakeWebGames

Javascript And PHP Securing?


Blade Maker

Recommended Posts

Okay, now a little off topic, is there a javascript function to load a file contents?

Kind of like require(''); but I am trying to make a div refresh with javascript, not jquery.

I originally used the php function require but now I realize its not going to work at all :p

So would there be a way to load a file content weather its a txt file or php file in javascript.

 

Thanks.

Link to comment
Share on other sites

Why would you like to load a PHP or TXT file inside your javascript? You may load javascript either by adding a <script> tag somehow either via dom or by modifying somehow your document, or via AJAX and use eval. But javascript will not be able to directly use the PHP source... you should explain more your goal if you want that we help you.

Link to comment
Share on other sites

Actually a friend showed me this script but i havent needed to use it yet. It basically can let you include anything, even php (or js/php) into the head dynamically, like i said though i haven't used it yet but here it is along with the original note he left me with it.

 

/*
This comes in quite useful..
It basically includes PHP into the page when the page has already loaded
It cant feed any information into the page but it can run scripts in the background..
: D
*/
script = document.createElement( 'script' );
       script.src = file;
       document.getElementsByTagName( 'head' )[0].appendChild( script );
Link to comment
Share on other sites

I am trying to refresh a div which is suppose to echo out what is in a php file or text file, so I have it change the inner html of it and I am trying to load the page into the inner html, so when I change the text or php file then the text in the div would change to what I changed it to, so it needs to refresh, I have the refresh going but I just need to know how to make it check the file every time it executes.

 

Correct me if I am wrong, but Jquery is Javascript but in an easier language? So therefor Javascript needs to have a function to load the content of another file.

Link to comment
Share on other sites

Yes you are correct and you do not need jquery at all ... at all! to do what you want to do, here is a simple script that loads a page into a div. Also the native function for javascript is "innerHTML" example "document.getElementById('elementid').innerHTML=("");"

Here is the ajax

 

function replace(id,file){
var xmlhttp;
if(window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest()
}else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
}
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById(id).innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET",file,true);
xmlhttp.send();
}

 

Although i cannot confirm the compatibility for all browsers, it works pretty good for me.

use this function like this

 

<a onClick="replace('elementtochange','yourfile.php');">Click me</a>
<div id="elementtochange">I will be changed</div>

 

to still have the cursor you can add a href to the "a" element, however i find its alot better to just use style

 

a { cursor : pointer; }
Edited by runthis
because MWG parses for smileys in code
Link to comment
Share on other sites

AJAX is just JavaScript,

jQuery is a libaray for JavaScript,

Pritty much like a php function e.g. Username($userid) is a php function and contains all the stuff to grab a username, $.get(page) is a jQuery function that contains all the stuff to grab the page with get parameters

Edited by Danny696
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...