Reputation: 21
I created this ajax file. I just want to know if there is a way to load the ajax file before i type in something. This file is only to test the ajax. When u run it the html display only the search bar. if i type something it print the typed stuff of the search bar and the var second. But i want that the html file displays it from beginning and the var will change later in the final file. is there a way to run the ajax once when I open up the page and not when i start typing?
I want to print the saved var(in this case second) when i open up the page or press f5. So when i open up the page it should print out: abcd
because the variable already exist(again it is var second)
I want that the function 'keyup(function()' run on beginning to show the results of the hardcoded string
<!DOCTYPE html>
<html>
<head>
<script src="js/jquery-3.5.1.min.js"></script>
<title></title>
</head>
<body>
<input type="text" id="search_text">
<div id="result"></div>
<div style="clear:both"></div>
</body>
</html>
<script type="text/javascript">
$(document).ready(function(){
$("#search_text").keyup(function(){
var search = $(this).val();
var second = "abcd";
$.ajax({
url:'checkvalues.php',
method:'post',
data:{query:search, second:second},
success: function(resonse){
$("#result").html(resonse);
}
})
})
})
</script>
<?php
if(isset($_POST['query'])){
echo $_POST['query'];
}
if(isset($_POST['second'])){
echo $_POST['second'];
}
?>
Upvotes: 1
Views: 96
Reputation: 458
If you want to run something "immediately" in your case you can just run it in a function passed as an argument of ready
function.
$(document).ready(function(){
//everything here is ran just after the event fired
//so you can just call AJAX
$.ajax({});
//or create separated function doing it and then call it
myFuncCallingAJAX();
})
Upvotes: 2