Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am new here and so here goes my first question :): I was writing a PHP/HTML/JQuery application for a website and it was going smoothly until I decided to have an "require_once" command along with a header specifying json content.

I am using an AJAX JQuery call to send data to the php file and then using the success callback function to get data returned by the php file and (trying) to alert it on the screen.

PHP code (#1):

<?php
include_once "/path/swiftmailer.php";   
header('Content-type: application/json');
$name = 'james';
$email = 'bob';
$test = array($name, $email);
echo json_encode($test);

PHP Code (#2):

<?php
include_once "/path/swiftmailer.php";   
header('Content-type: application/json');
$name = 'james';
$email = 'bob';
$test = array($name, $email);
echo json_encode($test);
?>

PHP Code (#3):

<?php
header('Content-type: application/json');
include_once "/path/swiftmailer.php";   
$name = 'james';
$email = 'bob';
$test = array($name, $email);
echo json_encode($test);

PHP Code (#4):

<?php
header('Content-type: application/json');
include_once "/path/swiftmailer.php";   
$name = 'james';
$email = 'bob';
$test = array($name, $email);
echo json_encode($test);
?>

AJAX Code:

$.ajax({
    url : 'process.php',
    type: 'POST',
    data : form.serialize(),
    success: function(results) {
        alert(results[0]);
    },
});
return false;   

Here are the results when I try the above 4 PHP code samples along with the above AJAX code sample: No. 1: alerts "<" to my screen
No. 2: same as No. 1
No. 3: never actually makes it to the alert command
No. 4: same as No. 3

I have been struggling with this for hours now and I could not find any questions that were identical. However, I tried to follow the advice of all questions I could locate that were similar (such as this question) to the best of my ability. I have also tried every permutation and combination of include_once and the like and all of them have produced the same result as require_once.

I would be immensely grateful to anyone who could help me with this issue, as it has really begun to irritate me :).

Thank you for your time and assistance.

Please note: the form variable seen in the ajax call is really my form (I believe), not a mistake. I believe it is my form because (var form = $('#form');) was also in my jquery code. Additionally, if I attempt to echo something without the "include_once" line, everything works perfectly and it displays the correct result (james). Lastly, there is no whitespace above or below the php tags.

share|improve this question
1  
Do you have URL we can view an example? Have you tried POSTing to your pages without AJAX just to see what is generated? –  Andy Jones Dec 8 '13 at 4:48
2  
Swiftmailer is probably throwing notices or warnings, and you probably have PHP error reporting enabled. Stop struggling, and look at the real output you're getting from PHP. Use your browser's tools or something like Fiddler. Show us the actual output... if it isn't immediately obvious to you what the problem is. –  Brad Dec 8 '13 at 4:48
    
1. Try setting the data type of json in your ajax call, as dataType:json, 2. Try the url directly and see whether there is any warnings or errors ? –  allupaku Dec 8 '13 at 4:49
1  
It's not clear to me exactly what is happening. In php you cannot echo any characters before the header. If you do an error is thrown because in HTTP the header must come first. Can you try your code and require an empty php file. Does this cause a weird error? Can you post the included file? Look for any echo or spaces after or before php in the included file –  portforwardpodcast Dec 8 '13 at 4:50
1  
@portforwardpodcast, I was not aware that I was echoing any characters before the header. Requiring an empty file causes the same errors as swift mailer. –  cryptopi Dec 8 '13 at 4:58

1 Answer 1

up vote 3 down vote accepted

Most likely, your PHP code is causing errors/warnings. The include/require triggers an error/warning, which gets sent, which prevents the header from being set, which causes further issues.

Use Firebug or other debugging tools (as included in current browsers) to look at what actually gets sent by the server. It will be an error message or warning, take care of it.

Most likely, the following happened: In the first case (#1/#2), the header doesn't get sent, jQuery interprets the response as a string, so response[0] gives you the first character, which is the opening of the HTML tag wrapping the warning/error. In the second case (#3/#4), jQuery tries to parse the JSON but can't because the warning/error made the JSON invalid.

share|improve this answer
1  
You got it :). Thanks so much for your help (sorry I don't have enough rep to vote your answer up). Marking this answer as accepted. –  cryptopi Dec 8 '13 at 14:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.