In this Ajax call video, I'm trying to show how to call Ajax in jQuery using the GET or POST method. And how simple you call Ajax in PHP using jQuery. In this video, I did that with an example. I'm using the GET and POST method, and I called Ajax and got back a return value.
AJAX stands for Asynchronous JavaScript and XML. AJAX is a new technique for creating better, faster, and more interactive web applications with the help of XML, HTML, CSS, and Java Script.:::::CODE:::::
file: ajaxCall/index.php
<form id="formSubmit">
<input type="text" name="hello" value="">
<input type="submit" name="submit" value="search">
</form>
<p id="post"></p>
<p id="get"></p>
<!--- JQuery Library --->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#formSubmit").submit(function( event ){
event.preventDefault(); //stop form submit and stop page load
// ajax post call
let rawData = {
//method: "post",
f_value: $(this).find('input[name="hello"]').val()
};
//$.post(url, data, function)
$.post("ajax.php", rawData, function(data, status){
$("#post").html(data);
});
// ajax get call
let ajaxCallURL = "ajax.php?f_value="+$(this).find('input[name="hello"]').val();
$.get(ajaxCallURL, function(data, status){
$("#get").html(data);
});
});
});
</script>
file: ajaxCall/ajax.php
if(isset($_POST["f_value"])){
echo "post value: ".$_POST["f_value"];
}
if(isset($_GET["f_value"])){
echo "get value: ".$_GET["f_value"];
}
Comments
Post a Comment