0

I am using an AJAX call with Jquery to a PHP form:

$.ajax({
    type : 'POST',
    url : 'submit_form.php',
    dataType : 'json',
    data:  dataString ,

which encodes the results from the database using:

echo json_encode($results);

However it prints the data back to at the top of the page that made the AJAX call, this completely breaks functionality in IE because it returns the text string before <html> declaration.

{"status":false,"msg":"Sorry The Queue is full"}<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

Is there anyway I can suppress this echo function on the page but still return the data array back to my page so I can use the results like ( data.msg )

0

3 Answers 3

1

Try to put it : header("Content-type: application/json"); before the code:echo json_encode($results);.

Best regards...

Sign up to request clarification or add additional context in comments.

10 Comments

dataType : 'json' on the AJAX call specifies the content header for the PHP file already, re-declaring it breaks the code.
@Edward: dataType is side client while header("Content-type: application/json") is used to define an http header to tells the browser what type of data/document to expect in the body of the document
when I add that header to the php file, it displays my entire document in the browser window kind of if I clicked view source on my page.
@Edward Is the $results variable an array?
@evolquez yes it's an associate array.
|
0

Use a callback function with your response data. The callback will be some function which handles the data received. It must be on the global scope (AFAIK). When the response from the server is ready, the function will be called and the data handled.

// from the server:
someFunction({"status":false,"msg":"Sorry The Queue is full"});

// in your page, in a script tag, etc.
window["someFunction"] = function(result){
    // do something
}

2 Comments

I'm not exactly sure what you're saying, I am using the $(document).ready(function () { and my ajax success function already takes the result as the parameter
The problem you're having is because you are trying to echo the result out onto a page that has data on it already. The typical AJAX workflow usually goes like: some javascript on a web page, like index.html, makes a request to a separate server page, like doWork.php?args=FruitSalad -> server echoes result on the same doWork.php -> callback function on original page (index.html) listens for the callback function to be called and updates the page based on the data received.
0
$.ajax({
  type : 'POST',
    url : 'submit_form.php',
    dataType : 'json',
  success: function(data) {
   console.log(data);
});

Try this.On successful calling you will get the result as js object. Check the result within console.

2 Comments

log comes back as null if i don't echo the json_encode($result)
echo the result then it will be visible

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.