I need a simple ajax tutorial or case study for a simple input form, where I want to post a username through an input form, which sends it to the database and replies with the results.
Any recommendation for such tutorial is welcome, because I've only got one using Mootool but I'm searching for one using jQuery!
Asked
Active
Viewed 5.6e+01k times
110

Gust van de Wal
- 5,211
- 1
- 24
- 48

Reham Fahmy
- 4,937
- 15
- 50
- 71
-
http://blog.teamtreehouse.com/beginners-guide-to-ajax-development-with-php – rassa45 Apr 08 '16 at 13:07
2 Answers
187
You can try this:
$.ajax({
url: "test.html",
cache: false,
success: function(html){
$("#results").append(html);
}
});
This code will append the content of test.html
file to #results
element
You can find more information at jQuery website.
Update:
Use this code to send POST data and output result.
var menuId = $("ul.nav").first().attr("id");
var request = $.ajax({
url: "script.php",
type: "POST",
data: {id : menuId},
dataType: "html"
});
request.done(function(msg) {
$("#log").html( msg );
});
request.fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
43
Assuming you have some html like:
<input type="text" name="username" id="username">
<div id="resultarea"></div>
You would use a <script>
like:
var myusername = $("#username").val();
$.ajax({
type: "GET",
url: "serverscript.xxx",
data: myusername,
cache: false,
success: function(data){
$("#resultarea").text(data);
}
});
-
6You may need to set the parameter name, like data : "username=" + myusername – GrandMasterFlush Jun 27 '16 at 11:43
-
you can also do ajax for somes case with this library https://github.com/Guseyn/EHTML, using only HTML – Guseyn Ismayylov Apr 23 '20 at 21:23
-
You can use above for post also and also can add dataType like json eg :- dataType: 'json', – T.A.Chathura Priyahsad Sep 14 '20 at 06:55
-
Note that you must pass data parameters either in json, object or array, like this https://codetopology.com/scripts/jquery/jquery-ajax-method-with-example/#Example_2_-_Ajax_POST_Request – Ankit21ks Aug 07 '21 at 13:56