Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax call facing an awkward situation

Tags:

ajax

php

mysql

Please bare me for long question...

I have to add one database record to one master table let say test table. After adding record to this table I want its last inserted id and then want to add approximate 1000 entries into transaction table (also sending EMAILS to these 1000 records) let say test_transaction table.

But this second operation, adding record into transaction table, takes more second to add records to database. I am using customer MVC 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's entries into another action like below

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

So main question, I am calling addtotest action from an AJAX and when it will give id in response I want to call another AJAX which will add approximate 1000 records to database.

BUT I do not want to disturb users' experience by letting them to wait for another few minutes for 1000 records.

  1. So I am reloading page after record has been added by addtotest `AJAX' call.
  2. Calling another AJAX call in success of addtotest.
  3. And after calling testtransaction AJAX call, I am reloading 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 page get reloaded my AJAX call is also get vanished without completing AJAX request.

So could be done in this case? I you want more details then please let me know.

I do not want users to wait for another 1000 entries (plus sending EMAILS to these 1000 records) being made into database.

like image 996
NullPointer Avatar asked Sep 11 '16 23:09

NullPointer


1 Answers

Just Create a Database Trigger then. So When Record Gets inserted in First Table it inserts into transaction table or SO.

Exampl -

    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
like image 148
Steve Avatar answered Sep 20 '22 16:09

Steve