Ajax call facing awkward situation

Please tell me a long task ...

I need to add one database record to one main table, say test table. After adding a record to this table, I want its last inserted id , and then I want to add about 1000 records to the transaction table (also sending EMAILS to these 1000 records), say test_transaction table.

But this second operation, adding a record to the transaction table, takes more than a second to add records to the database. I am using MVC client and my code looks like ...

 public function addtotest() { $strSql = "INSERT INTO test ..."; $intId = $this->db->execute($strSql); foreach($arrTransaction $transact) { $strSql = "INSERT INTO test_transaction ..."; $this->db->execute($strSql); } echo $intId; } 

So, I want to put test_transaction table test_transaction in another action , as shown below

 public function testtrancation() { $id = $_REQUEST['id']; //It understood that I am doing proper validation here foreach($arrTransaction $transact) { $strSql = "INSERT INTO test_transaction ..."; $this->db->execute($strSql); } } 

So, the main question: I call the addtotest action from AJAX , and when it gives an id in response, I want to call another AJAX , which will add the approximate 1000 entries to the database.

BUT, I don’t want to upset the user experience by letting them wait a few more minutes for 1000 entries.

  • So, I reload the page after the record is added by calling addtotest `AJAX '.
  • Call another AJAX call in success from addtotest .
  • And after calling testtransaction AJAX I reload my page.

     $.ajax( { url: SERVER_PATH + 'addtotest', type: 'post', async: true, data: { required_data: required_date }, success:function(data) { alert('Record added successfully.'); //I DO NOT WANT USERS TO WAIT FOR THIS AJAX CALL $.ajax( { url: SERVER_PATH + 'testtransaction', type: 'post', async: true, data: { intId: data.id }, success:function(data) { //DO NOTHING }, }); //I DO NOT WANT USERS TO WAIT FOR THIS AJAX CALL location.reload(); }, }); 

But as the page reloads, my AJAX call will also disappear without an AJAX request.

So what can be done in this case? I want more information, please let me know.

I do not want users to wait for other 1000 records (plus sending EMAILS to these 1000 records) that were entered into the database.

+7
ajax php mysql
source share
4 answers

Just create a database trigger. So, when the record is returned to the first table, it inserts into the transaction table or SO.

Exam -

  CREATE TRIGGER trg_Table1_INSERT ON dbo.Table1 AFTER INSERT AS BEGIN INSERT INTO dbo.Table2(SerialNo, Name) SELECT SerialNo, Name FROM Inserted INSERT INTO dbo.Table3(SomeOtherCol) SELECT SomeOtherCol FROM Inserted END 
+3
source share

It's simple!

After a single insert, you can invoke a separate background process:

 public function addtotest() { $strSql = "INSERT INTO test ..."; $intId = $this->db->execute($strSql); exec('php /your/php/cli/script.php param1 param2 paramN 1>> /dev/null 2>> /dev/null &'); echo $intId; } 

With "&", the process starts in the background, so you can continue and not wait until the script ends.

Or you can add this task to “pending tasks” and run these tasks with cron.

+1
source share

Do not use AJAX for this. You need a CRONJOB with basic pagination.

In one php file extraction file and after this sequence a list of 500 requests for each sequence. And with header () redirecting to the next 500 until you reach the end. This will not affect the user experience, and ypu will have good features.

+1
source share

You can insert all rows with one sql query.

 INSERT INTO test_transaction (col1, col2) VALUES (value11, value12), (value21, value22), (value31, value32), ... (value10001, value10002); 
+1
source share

All Articles