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

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/

Related

Call a php script from javascript

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 :)

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 give a post variable to php without reloading the page

I want to give php a simple variable from a text-box on a different page, without reloading it. I have heard that you need ajax to do it, but i am not sure on how to do that. I don't know how to use ajax so I just need an example.
thanks
Jquery ( http://jquery.com/ ) is a nice javascript library that can handle this. Look at the .post function ( http://api.jquery.com/jQuery.post/ ) this is commonly used for ajax requests
I've tried to do with javascript, but upto now jQuery is the solution.
$.ajax ({
type:'post',
url:'yourpage.php',
success: function(response.trim()) {
}
data: $("#bidform").serialize();
});

Post form to more URLs

is it there any way to post a form to more URLs?
I need it in javascript..
For example: I've got 2 documents: 1.php and 2.php.. I want the form to post data to the both of these files, but go to the 1.php..
Do you know any way?
You need a form with the action "1.php" and jQuery.
$('#form_id').submit(function(event){
event.preventDefault();
var $th = $(this);
$.ajax({
type: 'post',
url: '2.php', // note: edited by Ryan
data: $th.serialize(),
success: function(){
$th.submit();
}
});
});
Using JavaScript, yes
You'll want to make one call using the XMLHttpRequest function available in JavaScript to make an AJAX call to the php.2 script. The second call can then be done using document.forms["FORMNAME"].submit().
Libraries like jQuery's Ajax functions make it easier to use the XMLHttpRequest function. See the jquery.get(), jquery.post(), and jquery.ajax() (a.k.a $.get(), $.post() and $.ajax()) function documentation
Here are a few guides to doing AJAX calls using jQuery's $.post() and $.ajax() functions.
(http://www.sitepoint.com/ajax-jquery-3/)
(http://www.devirtuoso.com/2009/07/beginners-guide-to-using-ajax-with-jquery/)
(http://www.phpfreaks.com/forums/index.php?topic=256503.0)
(http://www.ryancoughlin.com/2008/11/04/use-jquery-to-submit-form/)
Also here is an example framework of how this can be coded up:
<form id="exampleform" name="exampleform" action="1.php">
...
</form>
<script type="text/javascript">
function submitForm()
{
... // set up vars for $.post() call
// Make the post call for the 2.php
$.post( ... );
// Submit the form for 1.php
document.forms["exampleform"].submit();
}
</script>
All above solution should work, but remember ajax is only alowed in same domain posting. This mean you are on http://example.com, you can only post to example.com/etc or sub.example.com
If you really want to cross domain posting, you should create an empty ifram as target (id="iframeid") then change the target of the form to it:
<form target="_iframeid" action=""/>
After that you can use javascript to post to one page, and then change action url and repost again
the quick and dirty way to do this is to post the form to one script which will in turn post it to a second script.

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.

Categories