Call a php script from javascript - php

I need to call my REST API that is written in PHP from a javascript script. A similar situation I can point to is the jquery ui autocomplete plugin. When you specify an external php script for the source attribute, it reads the output from the php script, and uses that as the autocomplete options.

you could use the jQuery ajax function:
http://api.jquery.com/jQuery.ajax/

AJAX. That is all.
If you have any more specific issues, feel free to ask.

You can use an AJAX request (if I understood the question)
If your API is written in myapi.php, you can:
var word = "The Word";
$.ajax({
url: "myapi.php?action=searchfor&word="+word,
success: function(data){
alert(data);
}
});
...assuming you're using jQuery, of course :)

Related

PHP - Execute a specific php code when clicking a link

I'm currently working on a website and I would like to be able to do the following:
when clicking one of the links from the sideMenu the only thing I would like to change would be the content of my contentMain div and nothing else(page layout/design/etc)
Could anybody give me some general pointers on how I could achieve this in php?
Thank You in advance :D
This is a client-side change that cannot be accomplished using PHP. PHP is evaluated on the server-side, so once the page is loaded for the user, it has no control over what the user sees (unless you use client-side code to call PHP).
To accomplish this, you will need to use Javascript and/or jQuery.
Reference: https://developer.mozilla.org/en/JavaScript/
jQuery: http://jquery.org/
iFrame, frameset or AJAX all work for your case depending on what you are actually trying to achieve.
For AJAX calls (the most modern way out of the three that relies on Javascript) you can use a library such as jQuery.
http://api.jquery.com/category/ajax/
You can use ajax for this one. Using jQuery to detect the click on the link or use normal JavaScript onClick function. Then do the things like you want.
<a href="" id="my_link">My link<a>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('#my_link').click(function(){
jQuery.ajax({
url: 'ajax page need to be called',
success: function(data) {
//do your operations on success
}
});
});
});
</script>
You can get more details on :
jQuery
jQuery Ajax
Hope this helps you

how to call a web service written in php in javascript?

Hi I have web service that is written in php that outputs my data in xml format. I need that data to be send to an html page where I use javascript to display it. I want to know to how call that php service from my html page using javascript?
You can use JSON and PECL for data transfer with php
JSON
http://www.json.org
Scroll down to the page to see some options that you have for PHP.
If you wanted to do something fast and simple, you can use the jquery get or post call to acheive the disired result:
jQuery GET:
http://api.jquery.com/jQuery.get/
The get and the post both have a call-back function which can be used to accept the result of loading your php page and then you can determine how to display it.
You should have a way or create a way to access that by going to a specific URL. So just create some JS that will call that URL. Best to use JQuery to make things easier. Then just manipulate the data all you want.
Try that .ajax function from JQuery. There are some great examples at http://visualjquery.com/. You can also get more details on it from http://api.jquery.com/jQuery.ajax/
Example:
var html = $.ajax({
url: "some.php",
async: false
}).responseText;
You could also use JQuery's load function:
$('#result').load('ajax/test.html');
How about something like this using jQuery's load()?
$('#result').load('http://myWebService/serviceName.php');
<div id="result"/>

Making a href link call upon a php script and run it

How do i make a href link call upon a PHP script without making the page refresh? Just in general? If someone could point me in the right direction it would be great. :)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.js">
<script>
function trigger() {
$.ajax({
type: 'POST',
url: myfile.php,
data: data,
success: success
dataType: dataType
});
}
</script>
<body>
some link
</body>
For more examples and references go to http://api.jquery.com/category/ajax/ and http://api.jquery.com/jQuery.post/
Embed jQuery and use one of the AJAX commands:
If you want to load data, use $.load()
If you want to send a GET/POST request use $.get() or $.post() or the generic $.ajax() method.
jQuery is your friend! http://www.jquery.com
Specifically, what you're looking for is called AJAX, and it's going to be a fun journey for you. Good luck!
Reference:
http://api.jquery.com/category/ajax/

Execute PHP without leaving page

I have a form - textarea (named su) and submit button.
When the form is submitted, I need to
run a PHP script without refreshing/leaving page
"echo" or somehow print a return on the screen
I'm pretty sure this works via some kind of ajax request thing. but I have no idea how.
Like I said I'm uneducated with ajax or java. A quick example would be wonderful.
Simple , that is what is called AJAX. The solution is more of Javascript, than PHP.
Using Jquery, you can do it with a simple function call:
<script src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#ID_Of_Your_Button').change(function(){
$.ajax({
type: "GET",
url: "send.php",
data: "query="+document.form.textarea.value,
success: function(msg){
document.getElementById("Div_Where_you_want_the_response").innerHTML = msg }
})
});
});
</script>
Does the trick.
If your just now getting into ajax, PHP, and JQuery I would highly suggest using firefox and installing Firebug. Learn to use it and it will tell you all sorts of great things. There is not enough space to tell you everything it does, but it is ,at this point, one of my best debugging tools. Learn it, Love it and good luck.
It is usually best to use a Javascript library for doing AJAX.
See jQuery.get() for one way to send a request to a PHP web page using the jQuery library. You can have your PHP page output Javascript to be executed, or output plain text or data.

Send data to database when click on a link without page refresh

Is there a way to send data to database when click on a link without page refresh?
I use php/mysql...
I will give you an example using jQuery.
Let's say that we have a link with an attribute id="button_id" (you have to learn the jQuery selectors ).
$("#button_id").click(function(){
var var_data = 5;
$.ajax({
url: "my_script.php",
data: { var_PHP_data: var_data };
success: function(data) {
// do something;
alert(data);
},
});
});
Explanation: you will send the variable var_data with the name var_PHP_data to a my_script.php without page refresh using an ajax call (using GET method).
This is very simple example of what you have to write on your PHP script.
<?php
$var_name = $_GET['var_PHP_data'];
echo 'This is what you have send'.$var_name;
?>
Because the default method to send variables in the ajax function in jQuery is GET.
We have to use the $_GET function in PHP.
This php script will print a message and this message will be handled in the success: function in the Ajax call and just for example we will alert this message returned from PHP.
You'd have to use JavaScript. When a user clicks a link, if you don't use JavaScript, then you need to go user -> server -> user and they get a whole new page.
HTTP is stateless.
It's not possible without a page refresh but this is the classic use-case for AJAX requests. If you're not familiar with AJAX then there are various methods of doing this using all the popular JavaScript frameworks such as Prototype and jQuery
You can't send data directly to a database, but you can use AJAX to send data to a php page that will save them to the database, without reloading the page or following the link in the browser..
Have a look at http://api.jquery.com/jQuery.post/
Not using PHP because it is server side - you need JavaScript / AJAX for this.
Check out frameworks like dojo (http://www.dojotoolkit.org/) , mootools (http://mootools.net/) or jQuery ( http://jquery.com/ ).
Yes, you can use AJAX.
This is a very big topic, but I'd recommend you do some research on AJAX and jquery (javascript).
Here are some tutorials:
http://www.ajaxf1.com/tutorial/ajax-php.html
http://www.talkphp.com/vbarticles.php?do=article&articleid=58&title=simple-ajax-with-jquery
Do a search in google for more info.

Categories