php does not echo anything from ajax javascript?
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
|
show 6 more comments
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
"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
|
show 6 more comments
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
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
javascript php ajax
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
|
show 6 more comments
"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
|
show 6 more comments
1 Answer
1
active
oldest
votes
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>
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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>
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
add a comment |
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>
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
add a comment |
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>
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>
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
"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