Using Javascript to dynamically generate PHP code - php

Is it possible with Javascript to dynamically generate php code and run it without reloading?

Well, technically, you could use ajax to send the code to backend that would then save it and another ajax call to run it but that would be a tremendous security hole, i.e. DONT DO IT

You have to understand something, Javascript is Client-Side language, and PHP is Server-Side, hence you can't use Javascript to "generate" PHP code.
What you can do is send data through a request via POST or GET using AJAX to a PHP file, which will do something with that data and then return a response without reloading the page.

Yes you can do it but it is pointless and dangerous. You can "generate" php code for some purposes /eg automated php script modification/ but you should never allow server side to directly execute unescaped input.
Looking at your question I suppose you dont clearly make the difference between Client and Server sides scripting. So make sure you realize it!
I suggest you think again and find better solution to your problem /or ask if you cant/.
Some further information : XSS

Related

How do you write a value returned by external javascript to a file using php?

I have a table with a button which onClick calls an external javascript file, which calculates some values and returns them to the table. I need to then write that value back to a file using PHP without leaving the current page. Is there any way to do this? I thought about trying onChange, but I wasn't sure how ot make that work with php since it's usually a javascript call.
PHP is executed on your server
the javascript file, even if external, is run on the client machine.
You can't use PHP on the client machine! You must use javascript to call a php script, stored in your server, that do what you need to.
You're on the good track, you need to use AJAX calls in your javascript.
don't use onclick, search for unobtrusive javascript;
you'd be using jQuery, so you can use its ajax handler;
find out a good reading about the whole topic, before going heads down on writing code.
This sounds like something you could solve by using AJAX. Basically you would communicate with your server in the background, send the data to it so it can process it. You send a separate request in the background which happens without reloading the page.
Please be aware of the distinction between the client (where the JavaScript is run) and the server (where PHP generated the page run by the client).
If you want to use jQuery, that library has some decent functions that makes it easier to implement cross-browser AJAX requests. However I would suggest first researching the topic.

Execute Javascript server side

I was interested in obfuscating my JS code, but I realized arround forums that it is useless. I would like to obfuscate my code anyway. So I was wondering, is that possible to execute JS code on server side (with an app in node.js for example), and just call via Ajax function with context (like dom, or something else), execute on server side, then give back result to page.
It could be very usefull for me, that could permit to just show basic JS functions, but not core of my app...
Perhaps a solution already exists, but I found nothing on Web...
EDIT :
I thought that with node.js, a solution was existing. I meant for example a simple JS function in client side like : call_func('function_name', context); that call a server side JS dispatcher function with ajax, that returns JS object containing results.
Perhaps I am dreaming ? :)
Thanks for your help.
You can either rewrite your calculations in PHP or if you need to use them dynamically/get access to the DOM, you can use AJAX to calculate on the server side using PHP and then recieve the ouput of the PHP script without reloading the page.
You can read about AJAX here (I would recommend using jQuery for it as it is much simpler than trying to understand HTTP requests):
http://api.jquery.com/jQuery.ajax/
While I agree that you should probably use a serverside language and get the information that you need from the dom via ajax, it is not true that you have to do that. You can simulate a dom on the server using e.g. jsdom which you will find here https://github.com/tmpvar/jsdom for nodejs. Similar packages exist for other languages.
That dispatch thingy you were dreaming about could be achieved using nowjs http://nowjs.com/.
I dont have experience with either tool, so I cannot comment on how well they work.
AJAX is the way to go. Send function name in xhr.send('func=myfunc').
First create a dummy div.
<div id="dummy"></div>
Then create a switch case in the js code and call the function:
switch(<?echo $_POST['func'];?>){
case 'myfunc':
document.getElementById("dummy").innerHTML=myfunc();
break;
}
Then just use xhr.responseText.

Load an embeded js into a page using php to hide it in the source?

ive got a script i need to load into a page but its sensitive so i dont want to have to reveal the source of the script. Im aware that js runs client side, but is there some way i could load it server side with php / or just embed it server side to still be run on the users system ?
The script itself is a simple external js script like this :
<script type="text/javascript" src="myscripts.js"></script>
cheers
If you want your browser to run a script, it must receive the source code for that script. Your statement "embed the script server side" doesn't make much sense, nor does "loading it server side with .php". Either the browser receives the script code to execute (and thus, it is visible to the end-user), or it doesn't.
You can try using various obfuscation methods to make the source code harder to understand, but anyone with sufficient determination will get around this.
You could conceal the source location of the script that way, but not the source code. If you want it to execute on the client then you must sent it to the client. If you send it to the client then the user can execute it. Don't put secret business logic on the client side, handle it server side.
No there is not. The best you can do is to obfuscate your code to make it hard to reverse-engineer for non experienced user.

The best way to change the text on a webpage without refreshing

I'm trying to emulate the upvote/downvote system used on the SE sites. Each of my pages have a score which users can upvote or downvote.
The arrows are images with onclick links to javascript functions. I need to find a way to dynamically change the score without refreshing the page and then run a script (probably PHP) to increment the score in the server's data files.
Is javascript the best way to do this? I'm not that big of a fan of letting users see the source for my functions.
This is only possible using Javascript.
Don't worry about users seeing the source; as long as the server is secure and well-designed, it won't do any harm.
You should only implement display and validation logic in Javascript; everything must be validated again on the server.
Welcome to AJAX.
The easiest way to do that is to use jQuery and its $.ajax method.
See http://api.jquery.com/jQuery.ajax/
It's as simple as
$('a.upvote-button').click(function() {
$.ajax('/posts/123/upvote', {type: 'post'});
return false;
});
Yes, you need javascript (or something considerably less sane) to do this. Namely, you need an AJAX callback.
I'm not that big of a fan of letting users see the source for my functions.
The client-side source of your "function" would be ridiculously simple. It could be as simple as:
$.post('/1234567/vote/up')
You may use a direct link to a php script (and form post values) instead, and without a javascript library it would be a few more lines, but you shouldn't need to expose anything of value in your javascript.
Use javascript. JQuery is an excellent choice for manipulating on screen content and interacting with a server via asynchronous calls.

PHP live updating

I was wondering if there is a way to use php to return the values from a search without having to reload the whole webpage or using iframes or anything like that. I've tried searching for it but I always end up with AJAX and I was wondering if there is a PHP way for it...
I suggest you read up on AJAX and what it is, as it is exactly what you are describing.
What AJAX is generating a request on the browser with javascript, sending the request to a server, generating content with whatever technology you want (be it PHP, .NET, etc.) and returning it to the browser, without the page ever 'reloading'. That's all it is, and that's what you want.
I recommend you check out something like jQuery as it is far away the most popular javascript library. It makes doing AJAX requests a piece of cake.
AJAX is what you're looking for. It means using JavaScript (on the browser) to initiate a request to the server (which may be running PHP, or any other language).
PHP is a server-side technology, and what you describe is mostly a client-side issue.
Every technology that does what you want is going to be very close to Ajax, so I suggest to just take a little time and get yourself going with Ajax. There are plenty of javascript frameworks around that make life easier for you as an Ajax programmer.
PHP is server-side. It can't do anything unless a web request is made (i.e. the user clicks on a link, requesting a page). This is why AJAX exists. The javascript on the client side is able to initiate a web request in the background and decide what to do with the response.
Check out jQuery. It makes AJAX a snap:
http://docs.jquery.com/Ajax
Yes I did the same using PHp and Mysql. What you can do is first create a PHP search page1 with a text box and write down some jQuery function for the onkeyup event of text box. Pass the value of the Text Box to the PHP search page2 and display its data in another blank DIV tag on your search page1. Let me know if you were able to get the concept, else I will forward you some link for that. infact I found a youtube video for this. Its not a difficult task.

Categories