i'm a beginner to php. i need to use php function which process some other pages and to display
dynamic result as javascript when a HTML button click is triggered .Is it possible?...
Yes. This is possible through a collection of technologies known as AJAX, i.e. using Javascript's asynchronous communication ability.
There are already a number of pre-built libraries that allow you to do this with ease. One of the most common is Prototype, also just google AJAX and you will find hundereds of libraries, tutorials and guides to help you.
Jamie Lewis's answer is a good one, but I will say that he is addressing to JavaScript while answering. I think the pure PHP solution is following:
You create a PHP file (myFile.php) where you get some arguments with GET method. And Your HTML form should have a button that calls following:
myFile.php?agrument1=10&argument2=20
SO HTML will pass arguments to your PHP file and you should get that arguments and call a function with that arguments.
For more details see (sending arguments with GET/POST methods ).
Related
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.
I'm working on a simple Javascript app, and I need a way to take a string an determine a value by entering it into a calculator. (i.e. if the string is "pi^2 sqrt(3)" you'll get 17.0946563")
Rather than reinventing the wheel, I was hoping there was a way to implement Google's calculator to grab the result. Basically, I want to call http://www.google.com/ig/calculator?hl=en&q= followed by the string, and have Google output the result. The only example I've seen is the one here but it's in PHP. I don't know much PHP, so I was wondering if there was any way to either call the PHP function from within Javascript or implement the function directly with Javascript. Thanks in advance for any advice.
You should use a library like jQuery to make an AJAX call to grab and parse the results. You'll need to build up the URL yourself and, if using jQuery, call the $.getJSON method.
I saw an article here: http://code.lancepollard.com/automatically-publish-posts-to-stumbleupon-with-ruby
I don't know Ruby, but the following lines are pretty self explanatory:
page = agent.get("http://www.stumbleupon.com/submit?url=#{url}&title=#{title}")
form = page.forms.first
form.radiobuttons_with(:name => "sfw").first.check
page = agent.submit(form)
I'm guessing Ruby can fetch the webpage, check a checkbox, then submit the form. Is that possible using PHP?
The Ruby code you referenced actually uses a third party library called Mechanize.
Something similar for PHP is The SimpleTest Scriptable Browser. It's not as feature rich as Mechanize but can get the job done and it can be used independently of the SimpleTest framework.
You probably would want something like:
http://simplehtmldom.sourceforge.net/
PHP's internal support is sufficient, but would be more cumbersome to use than a third party library.
Not out of the box. Possibly there is a third-party library that can do it for you. One that might help is PHPQuery to loop over a fetched page and select the form and its values. The submit then would have to be done using Curl or the like...!
More info:
fetching a page: http://php.net/manual/en/function.file-get-contents.php
JQuery for PHP: http://code.google.com/p/phpquery/wiki/Basics for a basic intro
Submitting a form with Curl: http://davidwalsh.name/execute-http-post-php-curl
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.
Have any bridge libraries been developed for PHP that provide access to the jQuery framework? Ideally it would be nice to have something fairly extensible so that creating jQuery-based content using PHP code would be fairly easy and customizeable. Does such a thing exist yet?
pquery
jqpie
jquery-php
There's a warmup list.
So far I've found one that seems to fit the description. I haven't tried it out yet, so if anyone has any feedback or experience with this or other ones don't hesitate to post!
PQuery
jQPie might be what you're after.
What can jQPie do?
Easily request and process data from php using $.getJSON
Inject php generated html into elements using $.(element).load
Call php functions directly from your web pages using $.jqpie
Call jQuery from php in respond to $.jqpie calls
Advanced autocomplete using jqpie_complete
QueryPath (http://querypath.org) is a full implementation of the jQuery DOM/XML/HTML part of jQuery. QueryPath has full CSS 3 selector support (including the stuff jQuery doesn't have, like XML namespace support). It also comes with DB tools, where you can run queries and have the results inserted into the query object. And it has a template engine, too. Like jQuery, you can write custom extensions very easily.
But it definitely takes advantage of its server-side status.
The main project page is at https://fedorahosted.org/querypath. You can download it there (and see lots of examples, including RSS and SVG manipulation).
Integrating with jQuery, then, can be done easily by sending XML data of many sorts down to jQuery. (You could probably send JSON, too... never tried.) And since the server side code and the client side code both look the same, there's less of a need to learn two totally different toolkits.