I have a php script that gets user input (some text and a link), puts it in the database and then using the link the user provided, it searches for an image.
The problem is, sometimes it takes a while to search for the image, and this makes the website 'stuck', so the view that says 'Your post has been submitted!' sometimes takes between 4 to 6 seconds to finally load.
Is there a way to make PHP search for the image asynchronously?
You may be wondering why don't I just load the view and then do the image search, or just use AJAX. Well, I need the 'last insert ID' from the database to rename the image according to the item ID, and this can only be done once the post has been inserted in the database.
EDIT 1: I have already read this Asynchronous PHP calls? but I am not sure I understand.
EDIT 2: Here is an overview of the process as requested:
- Step 1: User opens form and fills 2 fields (some text and a link)
- Step 2: User submits form
- Step 3: PHP inserts post in MySQL database
- Step 4: PHP searchs for an image of that link (crawls the web, takes a few seconds) and saves the image with the name of the last ID from database insert. If last ID was 75 image will be saved as 'img_75'.
- Step 5: PHP loads 'You have succesfully posted a link'
What I want is step 5 to happen just after step 3, while step 4 is running. I don't actually need the response from step 4. As long as it runs on the background I am happy :)
SOLUTION: I have done what @shadyyx suggests. A simple AJAX call. I really hoped for an alternative but apparently not php nor any other server side language can handle asynchronous calls.