I am trying to design a message system for my website, but i can't get my ajax running. So I make a simpler version of the interaction between the files.
Here is my file both test.php and load.php is under the root folder ( public_html).
i have the ajax function in test.php. load.php is just simply echoing "wow".
$("#test").click(function(){
alert("Clicked.");
$.ajax({
url:"/load.php",
type:"POST",
beforeSend: function(){
alert("testing");
},
success:function(result){
$("#result").html(result);
alert(" sss "+result);
}
}).error(function(){alert("wrong");});
});
Now It works perfectly.
...........how to set relative path ...................
Here is the more complicated design
3 files, all in different folder :
so when user load in messages.php, it will load control.php then run the ajax call control.php.
I am so confused about how to set up these paths for ajax
( including control.php in messages.php works fine)
Thanks
AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page. Classic web pages, (which do not use AJAX) must reload the entire page if the content should change.
Yeah, but ../ means 'the parent folder'. You'd want /folder/folder/file.php to get directly to a specific file. k i see now. its not like require and include in php. Ajax url is using the absolute path.
See jQuery.ajax ( settings ) below for a complete list of all settings. A set of key/value pairs that configure the Ajax request. All settings are optional. A default can be set for any option with $.ajaxSetup (). A set of key/value pairs that map a given dataType to its MIME type, which gets sent in the Accept request header.
1. If we put slash at the beginning of ajax url, ajax url pattern will be `hostname/yourFile` example: 2. If we don't use slash at the beginning, ajax url will add to the current url in the browser. example:
statusText: If the Ajax request fails, then this property will contain a textual representation of the error that just occurred. If the server encounters an error, then this will contain the text “Internal Server Error”.
To understand ajax url, I have two things that should always be remembered.
1. If we put slash at the beginning of ajax url, ajax url pattern will be `hostname/yourFile` example:
// current url: http://sample.com/users
// ajax code load from users page
$.ajax({
url: '/yourFile.php',
...
});
// ajax url will be: http://sample.com/yourFile.php
2. If we don't use slash at the beginning, ajax url will add to the current url in the browser. example:
// current url: http://sample.com/users
// ajax code load from users page
$.ajax({
url: 'yourFile.php',
...
});
//...ajax url will be http://simple.com/users/yourFile.php
I hope this things can help people who want to use ajax. Happy coding, thanks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With