Form-data-processing PHP called via AJAX in same file as <form>?
<p>When you use HTML forms, I learned it's 'elegant' to process the data in the same file eg. <code>&lt;?php if isset($_POST['submit'] doSomething(); ?&gt; ... &lt;form action="" method="POST"&gt;</code> </p>

<p>But what if I want to send the data via ajax? Do I put the <code>doSomething()</code> in a separate php-file that I send the data to or should I also put it in the same file and just <code>exit()</code> before the actual site content begins (if it has been called by ajax)? I think that way it would be a lot easier to make the site also work with JS disabled.</p>

<p>I understand that both ways would work, but I'm wondering what's considered cleaner and will lead to less errors.</p>

<p>Here's example code to explain what I mean:</p>

<pre><code>/* example.php */
&lt;?php
if( isset( $_POST['submit'] ) ) {
    doSomething( $_POST['foo'] );
    if( !isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) &amp;&amp; $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
        exit();
    }
}
?&gt;
&lt;html&gt;
    &lt;head&gt;
    ...
    &lt;/head&gt;
    &lt;body&gt;
        &lt;form action="" method="POST"&gt;
            &lt;input type="text" name="foo" id="foo"&gt;
            &lt;input type="submit" name="submit"&gt;
        &lt;/form&gt;
    &lt;script type='text/javascript'&gt;
    $('form').submit( function( event ) {

        event.preventDefault();

        $.post( 'example.php', { submit: '', foo: $('#foo').val() } );

    } );
    &lt;/script&gt;
    &lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>Is that a good and maintainable approach?</p>