php does not echo anything from ajax javascript?












0















I cannot get my input to be echo when I input text in my search field.
I do get the alert javascript but not the echo 2 to be display into the srv_search_mini_display field.
Im thinking that the ajax cant access the find_Service.php



search.php



 <input id="service_search_box" class="form-control mr-sm-2" type="text" name="search" placeholder="Search Services..." oninput="searchService()">
<div id="service_search_div" class="srv_search_mini_display d-none"></div>

<script type="application/javascript">

function searchService(){

var srv_search = document.getElementById("service_search_box").value;

var param = "service_search_box="+srv_search;

if(srv_search.length > 0){

$("#search_search_div").removeClass("d-none");

var ajax = new XMLHttpRequest();

ajax.onreadystatechange = function(){

if(ajax.readyState === 4 && ajax.status === 200){

if(ajax.responseText === ""){

document.getElementById("service_search_div").innerHTML = "";
$("#service_search_div").addClass("d-none");

} else {

alert(param); document.getElementById("service_search_div").innerHTML = ajax.responseText;
}

}
};

ajax.open("POST",'find_Service.php',false);
ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded");
ajax.send(param);

} else{

document.getElementById("service_search_div").innerHTML = "";
$("#service_search_div").addClass("d-none");
}

}
</script>


find_Service.php



 <?php

require_once('database.php');
$heidisql = pdo_con();

echo 2; // does not even appear
?>









share|improve this question

























  • "echo 2; // does not even appear" Where should that appear?

    – kerbholz
    21 hours ago













  • inside the <div id="service_search_div" class="srv_search_mini_display d-none"></div>

    – Emanula Sohn
    20 hours ago






  • 1





    document.getElementById("service_search_div").innerHTML = ajax.responseText; why this in the comment section. it should be in the next line to set the HTML.

    – Naveen
    20 hours ago






  • 1





    Enable error_reporting in 'find_Service.php' and check. If you run the find_Service.php separately in browser is it echoing '2' have you checked that

    – Chris shi
    20 hours ago








  • 1





    Have you checked in browser console. Does it display any error related to javascript

    – Chris shi
    20 hours ago
















0















I cannot get my input to be echo when I input text in my search field.
I do get the alert javascript but not the echo 2 to be display into the srv_search_mini_display field.
Im thinking that the ajax cant access the find_Service.php



search.php



 <input id="service_search_box" class="form-control mr-sm-2" type="text" name="search" placeholder="Search Services..." oninput="searchService()">
<div id="service_search_div" class="srv_search_mini_display d-none"></div>

<script type="application/javascript">

function searchService(){

var srv_search = document.getElementById("service_search_box").value;

var param = "service_search_box="+srv_search;

if(srv_search.length > 0){

$("#search_search_div").removeClass("d-none");

var ajax = new XMLHttpRequest();

ajax.onreadystatechange = function(){

if(ajax.readyState === 4 && ajax.status === 200){

if(ajax.responseText === ""){

document.getElementById("service_search_div").innerHTML = "";
$("#service_search_div").addClass("d-none");

} else {

alert(param); document.getElementById("service_search_div").innerHTML = ajax.responseText;
}

}
};

ajax.open("POST",'find_Service.php',false);
ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded");
ajax.send(param);

} else{

document.getElementById("service_search_div").innerHTML = "";
$("#service_search_div").addClass("d-none");
}

}
</script>


find_Service.php



 <?php

require_once('database.php');
$heidisql = pdo_con();

echo 2; // does not even appear
?>









share|improve this question

























  • "echo 2; // does not even appear" Where should that appear?

    – kerbholz
    21 hours ago













  • inside the <div id="service_search_div" class="srv_search_mini_display d-none"></div>

    – Emanula Sohn
    20 hours ago






  • 1





    document.getElementById("service_search_div").innerHTML = ajax.responseText; why this in the comment section. it should be in the next line to set the HTML.

    – Naveen
    20 hours ago






  • 1





    Enable error_reporting in 'find_Service.php' and check. If you run the find_Service.php separately in browser is it echoing '2' have you checked that

    – Chris shi
    20 hours ago








  • 1





    Have you checked in browser console. Does it display any error related to javascript

    – Chris shi
    20 hours ago














0












0








0








I cannot get my input to be echo when I input text in my search field.
I do get the alert javascript but not the echo 2 to be display into the srv_search_mini_display field.
Im thinking that the ajax cant access the find_Service.php



search.php



 <input id="service_search_box" class="form-control mr-sm-2" type="text" name="search" placeholder="Search Services..." oninput="searchService()">
<div id="service_search_div" class="srv_search_mini_display d-none"></div>

<script type="application/javascript">

function searchService(){

var srv_search = document.getElementById("service_search_box").value;

var param = "service_search_box="+srv_search;

if(srv_search.length > 0){

$("#search_search_div").removeClass("d-none");

var ajax = new XMLHttpRequest();

ajax.onreadystatechange = function(){

if(ajax.readyState === 4 && ajax.status === 200){

if(ajax.responseText === ""){

document.getElementById("service_search_div").innerHTML = "";
$("#service_search_div").addClass("d-none");

} else {

alert(param); document.getElementById("service_search_div").innerHTML = ajax.responseText;
}

}
};

ajax.open("POST",'find_Service.php',false);
ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded");
ajax.send(param);

} else{

document.getElementById("service_search_div").innerHTML = "";
$("#service_search_div").addClass("d-none");
}

}
</script>


find_Service.php



 <?php

require_once('database.php');
$heidisql = pdo_con();

echo 2; // does not even appear
?>









share|improve this question
















I cannot get my input to be echo when I input text in my search field.
I do get the alert javascript but not the echo 2 to be display into the srv_search_mini_display field.
Im thinking that the ajax cant access the find_Service.php



search.php



 <input id="service_search_box" class="form-control mr-sm-2" type="text" name="search" placeholder="Search Services..." oninput="searchService()">
<div id="service_search_div" class="srv_search_mini_display d-none"></div>

<script type="application/javascript">

function searchService(){

var srv_search = document.getElementById("service_search_box").value;

var param = "service_search_box="+srv_search;

if(srv_search.length > 0){

$("#search_search_div").removeClass("d-none");

var ajax = new XMLHttpRequest();

ajax.onreadystatechange = function(){

if(ajax.readyState === 4 && ajax.status === 200){

if(ajax.responseText === ""){

document.getElementById("service_search_div").innerHTML = "";
$("#service_search_div").addClass("d-none");

} else {

alert(param); document.getElementById("service_search_div").innerHTML = ajax.responseText;
}

}
};

ajax.open("POST",'find_Service.php',false);
ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded");
ajax.send(param);

} else{

document.getElementById("service_search_div").innerHTML = "";
$("#service_search_div").addClass("d-none");
}

}
</script>


find_Service.php



 <?php

require_once('database.php');
$heidisql = pdo_con();

echo 2; // does not even appear
?>






javascript php ajax






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 20 hours ago







Emanula Sohn

















asked 21 hours ago









Emanula SohnEmanula Sohn

275




275













  • "echo 2; // does not even appear" Where should that appear?

    – kerbholz
    21 hours ago













  • inside the <div id="service_search_div" class="srv_search_mini_display d-none"></div>

    – Emanula Sohn
    20 hours ago






  • 1





    document.getElementById("service_search_div").innerHTML = ajax.responseText; why this in the comment section. it should be in the next line to set the HTML.

    – Naveen
    20 hours ago






  • 1





    Enable error_reporting in 'find_Service.php' and check. If you run the find_Service.php separately in browser is it echoing '2' have you checked that

    – Chris shi
    20 hours ago








  • 1





    Have you checked in browser console. Does it display any error related to javascript

    – Chris shi
    20 hours ago



















  • "echo 2; // does not even appear" Where should that appear?

    – kerbholz
    21 hours ago













  • inside the <div id="service_search_div" class="srv_search_mini_display d-none"></div>

    – Emanula Sohn
    20 hours ago






  • 1





    document.getElementById("service_search_div").innerHTML = ajax.responseText; why this in the comment section. it should be in the next line to set the HTML.

    – Naveen
    20 hours ago






  • 1





    Enable error_reporting in 'find_Service.php' and check. If you run the find_Service.php separately in browser is it echoing '2' have you checked that

    – Chris shi
    20 hours ago








  • 1





    Have you checked in browser console. Does it display any error related to javascript

    – Chris shi
    20 hours ago

















"echo 2; // does not even appear" Where should that appear?

– kerbholz
21 hours ago







"echo 2; // does not even appear" Where should that appear?

– kerbholz
21 hours ago















inside the <div id="service_search_div" class="srv_search_mini_display d-none"></div>

– Emanula Sohn
20 hours ago





inside the <div id="service_search_div" class="srv_search_mini_display d-none"></div>

– Emanula Sohn
20 hours ago




1




1





document.getElementById("service_search_div").innerHTML = ajax.responseText; why this in the comment section. it should be in the next line to set the HTML.

– Naveen
20 hours ago





document.getElementById("service_search_div").innerHTML = ajax.responseText; why this in the comment section. it should be in the next line to set the HTML.

– Naveen
20 hours ago




1




1





Enable error_reporting in 'find_Service.php' and check. If you run the find_Service.php separately in browser is it echoing '2' have you checked that

– Chris shi
20 hours ago







Enable error_reporting in 'find_Service.php' and check. If you run the find_Service.php separately in browser is it echoing '2' have you checked that

– Chris shi
20 hours ago






1




1





Have you checked in browser console. Does it display any error related to javascript

– Chris shi
20 hours ago





Have you checked in browser console. Does it display any error related to javascript

– Chris shi
20 hours ago












1 Answer
1






active

oldest

votes


















1














First you have to make sure that your PHP file work, you can check it by open find_Service.php separately or like @ADyson said check it from inspect console in network tab.



Since I saw you using jQuery, I simplify your code a little bit by using jQuery's ajax and select element with $ sign.



I use fake API server to make it work here.






function searchService(){
var srv_search = $('#service_search_box').val();
var $srv_search_div = $('#service_search_div');

if(srv_search.length > 0){
$("#search_search_div").removeClass("d-none");

$.post("https://jsonplaceholder.typicode.com/posts", //replace with "/find_Service.php"
{
service_search_box: srv_search,
},
function(data, status){
if(status === "success" ){
$srv_search_div.html(data.service_search_box); //replace with data
$srv_search_div.addClass("d-none");
}
console.log(data);
});

}else{
$srv_search_div.html("");
$("#service_search_div").addClass("d-none");
}
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<input id="service_search_box" class="form-control mr-sm-2" type="text" name="search" placeholder="Search Services..." oninput="searchService()">
<div id="service_search_div" class="srv_search_mini_display d-none"></div>








share|improve this answer
























  • ok i manage to get the 2 appear in console, but still nothing appear in the div

    – Emanula Sohn
    19 hours ago






  • 1





    if you use my code change $srv_search_div.html(data.service_search_box); to $srv_search_div.html(data);

    – Kyaw Kyaw Soe
    19 hours ago











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54250619%2fphp-does-not-echo-anything-from-ajax-javascript%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














First you have to make sure that your PHP file work, you can check it by open find_Service.php separately or like @ADyson said check it from inspect console in network tab.



Since I saw you using jQuery, I simplify your code a little bit by using jQuery's ajax and select element with $ sign.



I use fake API server to make it work here.






function searchService(){
var srv_search = $('#service_search_box').val();
var $srv_search_div = $('#service_search_div');

if(srv_search.length > 0){
$("#search_search_div").removeClass("d-none");

$.post("https://jsonplaceholder.typicode.com/posts", //replace with "/find_Service.php"
{
service_search_box: srv_search,
},
function(data, status){
if(status === "success" ){
$srv_search_div.html(data.service_search_box); //replace with data
$srv_search_div.addClass("d-none");
}
console.log(data);
});

}else{
$srv_search_div.html("");
$("#service_search_div").addClass("d-none");
}
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<input id="service_search_box" class="form-control mr-sm-2" type="text" name="search" placeholder="Search Services..." oninput="searchService()">
<div id="service_search_div" class="srv_search_mini_display d-none"></div>








share|improve this answer
























  • ok i manage to get the 2 appear in console, but still nothing appear in the div

    – Emanula Sohn
    19 hours ago






  • 1





    if you use my code change $srv_search_div.html(data.service_search_box); to $srv_search_div.html(data);

    – Kyaw Kyaw Soe
    19 hours ago
















1














First you have to make sure that your PHP file work, you can check it by open find_Service.php separately or like @ADyson said check it from inspect console in network tab.



Since I saw you using jQuery, I simplify your code a little bit by using jQuery's ajax and select element with $ sign.



I use fake API server to make it work here.






function searchService(){
var srv_search = $('#service_search_box').val();
var $srv_search_div = $('#service_search_div');

if(srv_search.length > 0){
$("#search_search_div").removeClass("d-none");

$.post("https://jsonplaceholder.typicode.com/posts", //replace with "/find_Service.php"
{
service_search_box: srv_search,
},
function(data, status){
if(status === "success" ){
$srv_search_div.html(data.service_search_box); //replace with data
$srv_search_div.addClass("d-none");
}
console.log(data);
});

}else{
$srv_search_div.html("");
$("#service_search_div").addClass("d-none");
}
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<input id="service_search_box" class="form-control mr-sm-2" type="text" name="search" placeholder="Search Services..." oninput="searchService()">
<div id="service_search_div" class="srv_search_mini_display d-none"></div>








share|improve this answer
























  • ok i manage to get the 2 appear in console, but still nothing appear in the div

    – Emanula Sohn
    19 hours ago






  • 1





    if you use my code change $srv_search_div.html(data.service_search_box); to $srv_search_div.html(data);

    – Kyaw Kyaw Soe
    19 hours ago














1












1








1







First you have to make sure that your PHP file work, you can check it by open find_Service.php separately or like @ADyson said check it from inspect console in network tab.



Since I saw you using jQuery, I simplify your code a little bit by using jQuery's ajax and select element with $ sign.



I use fake API server to make it work here.






function searchService(){
var srv_search = $('#service_search_box').val();
var $srv_search_div = $('#service_search_div');

if(srv_search.length > 0){
$("#search_search_div").removeClass("d-none");

$.post("https://jsonplaceholder.typicode.com/posts", //replace with "/find_Service.php"
{
service_search_box: srv_search,
},
function(data, status){
if(status === "success" ){
$srv_search_div.html(data.service_search_box); //replace with data
$srv_search_div.addClass("d-none");
}
console.log(data);
});

}else{
$srv_search_div.html("");
$("#service_search_div").addClass("d-none");
}
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<input id="service_search_box" class="form-control mr-sm-2" type="text" name="search" placeholder="Search Services..." oninput="searchService()">
<div id="service_search_div" class="srv_search_mini_display d-none"></div>








share|improve this answer













First you have to make sure that your PHP file work, you can check it by open find_Service.php separately or like @ADyson said check it from inspect console in network tab.



Since I saw you using jQuery, I simplify your code a little bit by using jQuery's ajax and select element with $ sign.



I use fake API server to make it work here.






function searchService(){
var srv_search = $('#service_search_box').val();
var $srv_search_div = $('#service_search_div');

if(srv_search.length > 0){
$("#search_search_div").removeClass("d-none");

$.post("https://jsonplaceholder.typicode.com/posts", //replace with "/find_Service.php"
{
service_search_box: srv_search,
},
function(data, status){
if(status === "success" ){
$srv_search_div.html(data.service_search_box); //replace with data
$srv_search_div.addClass("d-none");
}
console.log(data);
});

}else{
$srv_search_div.html("");
$("#service_search_div").addClass("d-none");
}
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<input id="service_search_box" class="form-control mr-sm-2" type="text" name="search" placeholder="Search Services..." oninput="searchService()">
<div id="service_search_div" class="srv_search_mini_display d-none"></div>








function searchService(){
var srv_search = $('#service_search_box').val();
var $srv_search_div = $('#service_search_div');

if(srv_search.length > 0){
$("#search_search_div").removeClass("d-none");

$.post("https://jsonplaceholder.typicode.com/posts", //replace with "/find_Service.php"
{
service_search_box: srv_search,
},
function(data, status){
if(status === "success" ){
$srv_search_div.html(data.service_search_box); //replace with data
$srv_search_div.addClass("d-none");
}
console.log(data);
});

}else{
$srv_search_div.html("");
$("#service_search_div").addClass("d-none");
}
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<input id="service_search_box" class="form-control mr-sm-2" type="text" name="search" placeholder="Search Services..." oninput="searchService()">
<div id="service_search_div" class="srv_search_mini_display d-none"></div>





function searchService(){
var srv_search = $('#service_search_box').val();
var $srv_search_div = $('#service_search_div');

if(srv_search.length > 0){
$("#search_search_div").removeClass("d-none");

$.post("https://jsonplaceholder.typicode.com/posts", //replace with "/find_Service.php"
{
service_search_box: srv_search,
},
function(data, status){
if(status === "success" ){
$srv_search_div.html(data.service_search_box); //replace with data
$srv_search_div.addClass("d-none");
}
console.log(data);
});

}else{
$srv_search_div.html("");
$("#service_search_div").addClass("d-none");
}
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<input id="service_search_box" class="form-control mr-sm-2" type="text" name="search" placeholder="Search Services..." oninput="searchService()">
<div id="service_search_div" class="srv_search_mini_display d-none"></div>






share|improve this answer












share|improve this answer



share|improve this answer










answered 20 hours ago









Kyaw Kyaw SoeKyaw Kyaw Soe

611211




611211













  • ok i manage to get the 2 appear in console, but still nothing appear in the div

    – Emanula Sohn
    19 hours ago






  • 1





    if you use my code change $srv_search_div.html(data.service_search_box); to $srv_search_div.html(data);

    – Kyaw Kyaw Soe
    19 hours ago



















  • ok i manage to get the 2 appear in console, but still nothing appear in the div

    – Emanula Sohn
    19 hours ago






  • 1





    if you use my code change $srv_search_div.html(data.service_search_box); to $srv_search_div.html(data);

    – Kyaw Kyaw Soe
    19 hours ago

















ok i manage to get the 2 appear in console, but still nothing appear in the div

– Emanula Sohn
19 hours ago





ok i manage to get the 2 appear in console, but still nothing appear in the div

– Emanula Sohn
19 hours ago




1




1





if you use my code change $srv_search_div.html(data.service_search_box); to $srv_search_div.html(data);

– Kyaw Kyaw Soe
19 hours ago





if you use my code change $srv_search_div.html(data.service_search_box); to $srv_search_div.html(data);

– Kyaw Kyaw Soe
19 hours ago


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54250619%2fphp-does-not-echo-anything-from-ajax-javascript%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Liquibase includeAll doesn't find base path

How to use setInterval in EJS file?

Petrus Granier-Deferre