0

I'm writing a PHP application which uses AJAX to submit forms via POST when required. When javascript is not able to be used, I submit the form via HTML/PHP as per normal.

What I really want to do is return JSON or XML to the AJAX call and I don't want to write all of the form processing logic twice or repeat myself at all really.

I'm trying to determine the best way to write the form processing logic as a single interface which can then be used by both an AJAX call and the PHP script.

I have come up with two options, which both seem like hacks. Hoping I can be given some cleaner/better/more correct solutions or have my two solutions evaluated to determine which one is preferred.

Form: form.php Processor: process.php

AJAX: JS intercepts submit click in form.php, POSTs to process.php which returns JSON result back to JS. JS updates HTML accordingly using JSON result.

PHP:

Option 1 form.php posts to process.php which outputs HTML if a certain variable is passed with the POST data. e.g.

if ($_POST['output'] == 'html') {
    //output as html
} else {
    //output JSON
}

Option 2 form.php posts to intermediate.php which will then include('process.php'), catch the JSON output and use the JSON output to display the HTML as required. e.g.

ob_start();
include('process.php');
$json = json_decode(ob_get_contents());
ob_end_clean();
//use json to create HTML to display to waiting user here

Both of these options seem a little hacky. The second seems cleaner (although I never feel like I'm doing something cleanly when I use ob_start) because it doesn't require me to write process.php any differently - which emulates an external web service better - although this is not a concern since process.php is fully under my control.

Thoughts?

Thanks as always, Aaron

3 Answers 3

1

I would choose option 1. Just like in popular frameworks like Yii. The typical workflow is like this one:

if (isAjaxRequest()) {
    // Ouput JSON and finish script
    die(json_encode($output));
}
else {
    // This is not AJAX request, proceed
}

And isAjaxRequest is:

// Taken from Yii framework method
function isAjaxRequest() {
    return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']==='XMLHttpRequest';
}

You don't need to set certain variable to indicate that the request is via AJAX because browser sends HTTP_X_REQUESTED_WITH header and you just need to check if it's been set.

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

1 Comment

Thank you. I've accepted this as the answer and it is the option that I went for.
0

Maybe something along the lines of option 1?

Just before the ajax script fires, use javascript to change the form variable from HTML to JSON and then the PHP script can capture it right?

If the ajax script fails, it will never change the variable.

You could even use javascript to change the form action to JSON_process.php or something like that

Comments

0

I'd recommend using an MVC framework. In the controller, set all the data you'll need. Then, set up two view templates, one for JSON (which could just do a simple json_encode of the data), and one for HTML. You can then just set the view based on an output parameter as in your Option 1.

Comments

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.