ISSUE SOLVED
The issues seemed to be browser related. Tried with a different system and it works fine.
I am coding a live search to show the details of users from database using ajax and php. The input from the form fields are getting passed to the ajax file properly. I checked with console.log() and it is printing properly. After that, the Ajax call takes place without any errors. But when trying to echo the $_POST['search'], it is printing empty space.
searchform.php
<input type="text" placeholder="Search" class="search"/>
<div class="search-result"></div>
search.js
$(function(){
$('.search').keyup(function(){
var search = $(this).val();
//console.log(search);
$.ajax({
url:'search.php',
data:{search:search,},
type:'POST',
success:function(data){
console.log(data);
$('.search-result').html(data);
},
error:function(){
console.log("error");
}
});
});
});
search.php
<?php
include('core/init.php');
echo $_POST['search'];
?>
EDITED
I figured out the issue, but no idea about the cause and no solution found. It is basically that the include('core/init.php');
is not getting included. I tried var_dump(include('core/init.php'));
and it is returning bool(false).The path is correct. Is there something that needs to be done when file with includes
is called using ajax.?
First, you change this
data:{search:search,}
to thisdata:{search:search}
Second, try to change your
echo
in yoursearch.php
become thisecho json_encode($_POST['search']);
For more complex checking, you can
inspect element
(press F12 from your browser), go tonetwork
tab and open in new tab yoursearch.php
file. It is show your expected value?If
search.php
shown the expected value, try to hard refresh your browser in windows (ctrl + F5) to try updated your js
There is no extra key/value pair in this object data:{search:search,}
so do not need to add "," at the end. You should just use data:{search:search}
.
Hope it will work.
Remove comma in data:{search:search,}
I recommend to use jquery $ prefix to vars to don't get confused, following code works.
HTML side
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready( function() {
$('#search-btn').on('click', function() {
$search = $('#search-txt').val();
console.log($search);
$.ajax({
url:'search.php',
data:{search: $search},
type:'POST',
success:function(data){
console.log(data);
$('#what-you-searched').html(data);
},
error:function(){
console.log("Error");
}
});
});
});
</script>
</head>
<body>
<input id="search-txt" type="text">
<input id="search-btn" type="button" value="search">
<p>You searched</p><p id="what-you-searched"></p>
</body>
</html>
PHP side
<?php
echo $_POST['search'];
?>
Here is what my code does:
$(document).ready(fuc) /* code will only run once the page Document Object Model (DOM) is ready for JavaScript code to be executed*/
$().on('click', func) /* listener: triggered when a user click on a DOM object, in this case the search input tag (which is a button)*/
When user click on "Submit" button $search will be filled up with #search-txt value and tha Ajax method is called (passing $search as search to the backend). My PHP backend simply echo the searched content. JS on Success fills #what-you-searched with returned value