PHP Jquery ajax class - php

I have been using xajax, as a server side ajax class (all the ajax response is handled with php code) for the longest while.
However I lately fell in love with jquery, and am using it for a project. I would like to know if there is a server side class (like xajax) that handles jquery ajax? or does anyone know of a tutorial/example i can use to create my own?
Thank you in advance

Here are two alternatives:
http://phery-php-ajax.net/
https://github.com/spantaleev/sijax/
Updated link for phery library

Jquery(javascript) is a frontend/public language. Of course you will need to verify any data sent via javascript, on the server-side and/or both on the front-end.
jQuery has a plethora of options concerning ajax. ( $.post/$.get/$.getJSON/$.ajax ). these send/retrieve data via a HTTP request.
If you have leaned towards jQuery, why would you want a php-ajax library? You will do your input validating server-side anyway!
jquery/ajax will send your data either with a post or get request, you just grab it with php and verify.
For a user-friendly experience I would suggest both frontend/backend validating.

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.

Using Javascript to dynamically generate PHP code

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

Using Ajax to send form data

I have a HTML/PHP form using using post to send form data to a script php file. I want to send the data with out the page reloading upon clicking the submit button.
I am wondering how do implement AJAX to allow this functionality.
I have a good grasp of Javascript but have never used ajax technology.
If this question is beyond the scope of this Q&A if anyone could please point me in the right direction to a good tutorial That will allow me to implement this technology with out having to spend a couple of days learning how it all works. I am working with a very short deadline.
Cheers.
If this is your first AJAX experience, I recommend a good toolkit like jQuery.
See: http://api.jquery.com/jQuery.ajax/
you can serialize and then post form data with jquery and optionally handle the response.
the following is an example from jquery's documentation http://api.jquery.com/jQuery.post/:
$.post("test.php", $("#testform").serialize());
You could use this jQuery plugin:
http://www.matiasmancini.com.ar/jquery-plugin-ajax-form-validation-html5.html
It also has form validation and works on IE6.
basically, you can do no wrong by using jquery javascript library with all the cool nifty functions ready, including ajax function which allows you to do ajax call with something as simple as $.post().
but if you're looking for a step by step tutorial, then you might want to look at this.

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