Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Having require files messing up JSON encoding/returning
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>PHP code (#1):</p> <pre><code>&lt;?php include_once "/path/swiftmailer.php"; header('Content-type: application/json'); $name = 'james'; $email = 'bob'; $test = array($name, $email); echo json_encode($test); </code></pre> <p>PHP Code (#2):</p> <pre><code>&lt;?php include_once "/path/swiftmailer.php"; header('Content-type: application/json'); $name = 'james'; $email = 'bob'; $test = array($name, $email); echo json_encode($test); ?&gt; </code></pre> <p>PHP Code (#3):</p> <pre><code>&lt;?php header('Content-type: application/json'); include_once "/path/swiftmailer.php"; $name = 'james'; $email = 'bob'; $test = array($name, $email); echo json_encode($test); </code></pre> <p>PHP Code (#4):</p> <pre><code>&lt;?php header('Content-type: application/json'); include_once "/path/swiftmailer.php"; $name = 'james'; $email = 'bob'; $test = array($name, $email); echo json_encode($test); ?&gt; </code></pre> <p>AJAX Code:</p> <pre><code>$.ajax({ url : 'process.php', type: 'POST', data : form.serialize(), success: function(results) { alert(results[0]); }, }); return false; </code></pre> <p>Here are the results when I try the above 4 PHP code samples along with the above AJAX code sample: No. 1: alerts "&lt;" to my screen <br> No. 2: same as No. 1 <br> No. 3: never actually makes it to the alert command <br> No. 4: same as No. 3 <br></p> <p>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 <a href="https://stackoverflow.com/questions/11261489/php-include-files-are-making-json-data-invalid">this question</a>) 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. </p> <p>I would be immensely grateful to anyone who could help me with this issue, as it has really begun to irritate me :).</p> <p>Thank you for your time and assistance.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload