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.
Related
I'm looking for a fast and efficient way to detect changes to a page HTML structure. This doesn't include text/strings within the html elements.
From all my research online I haven't been able to find any good method..
Does someone have an idea?
Re your comment:
Yes, but I need to achieve it serverside. In PHP prefably.
You have to use Javascript for this, and it has to run client-side. There is no option about that.
If you want to make PHP aware of it, then you will still have to use Javascript to detect it, and then use a Javascript Ajax call back to your server.
Does any one know how to implement auto complete feature in php using javascript and postgres db. I dont want to use ajax for this purpose.
If you mean that you don't want to use XMLHttpRequest by saying that you don't want to use AJAX, there are another techniques to achieve same results. For example JSONP. You should be more specific in your question.
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.
Can someone kindly put a good & simple example of JQuery with ajax to send json to a php script. I need to send json to a php script when page loads. I mean will be lot easy for me if the link contains example using $(document).ready(function()... I need a simple example as my problem is not big, & I need a simple solution. Just POST json to php.
thanX a lot in advance.
I like this visualJquery reference site a lot, sometimes more than the official api
http://visualjquery.com/
I believe you want to use the function $.post()
simple link to start from example :
http://www.factsandpeople.com/facts-mainmenu-5/26-html-and-javascript/89-jquery-ajax-json-and-php
A good place to start is the jQuery documentation.. They even have an article specifically on doing ajax posts: jQuery.post()
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 ).