0

This has been a difficult problem to search for, as all search results describe AJAX requests from javascript. Technically AJAX from javascript COULD solve my issue, but I'd prefer this to be solved purely server side using PHP.

Here's a breakdown of what's going on so far. I've simplified the code a lot.

CustomerOrderForm.php:

<form action="CreateOrder.php" method="post" onsubmit="this.submitButton.disabled = true;">
  <input name="first">
  <input name="second">
  <button type="submit">Place Order</button>
</form>

CreateOrder.php

<?php
basicOrderProcesses($_POST['first'],$_POST['second']);
header("Location: PaymentMethod.php?first=$_POST['first']&second=$_POST['second']");
slowOrderProcesses($_POST['first'],$_POST['second']);
die();

function basicOrderProcesses(){
  //Code that is absolutely essential to processing payment
}
function slowOrderProcesses(){
  //Very time consuming code that I'd rather the customer doesn't have to wait for
}

PaymentMethod.php

<h1>Please Select Your Payment Method</h1>
...
//You get the idea

I thought it was working fine, but as slowOrderProcesses() got bigger, the code ran slower. I see now that it never was running in the background, and the page isn't redirecting until slowOrderProcesses() is completed.

If CreateOrder.php were running in javascript, I could easily write an AJAX solution like this one. But it's running in pure php. What's the equivalent php solution? Remember, I need the slow processes to run while the customer is choosing a payment method. (And maybe even continue to run after the customer has paid, if the customer is fast enough.)

I'm hoping the solution won't require too much rewriting, but I gotta do what I gotta do. (I try to avoid installing libraries if I can help it.)

12
  • You need multi-threading. "Off-the-shelf PHP builds from package managers do not support multi-threading. " (medium.com/@rossbulat/…) Commented Jun 28, 2020 at 2:34
  • I'm pretty sure it's not possible in a webserver, but you could check the php.net/manual/en/intro.pthreads.php. I would use Ajax, I don't see the problem, every browser supports javascript... Commented Jun 28, 2020 at 2:35
  • 1
    Can you split the call from the javascript into two - make one AJAX call for the essential stuff and another for the slow stuff. I do that successfully in another similar context. Because AJAX is async you can fire them off one after the other and they run in parallel. Commented Jun 28, 2020 at 2:37
  • @Nikkorian if you wrote that as an answer, I'd definitely give it an up vote. It's probably what I'll end up doing. I just want to hear out my other options first. (And if I end up doing it, I'll give it the green tick.) Commented Jun 28, 2020 at 2:44
  • @JonathonPhilipChambers thanks - I am usually reluctant to fire off answers rather than comments. But seeing you asked... Commented Jun 28, 2020 at 2:58

1 Answer 1

2

Split the call from the javascript into two - make one AJAX call for the essential stuff and another for the slow stuff. I do that successfully in another similar context. Because AJAX is async you can fire them off one after the other and they run in parallel.

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

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.