AJAX processing in action buttons in Scriptcase Grid

Editor Mestre Scriptcase
Scriptcase Master
Published in
4 min readAug 2, 2022

--

Master,

In this article we will show you how to perform an Ajax Processing running through a grid Action Button.

In this example, we chose something quite common in conventional systems:

Delete a customer.

To create the button that will be used in the processing, we will use the sc_field_action() macro that we teach how to implement in this article here:

Using Action Buttons in Scriptcase Grid

Also, as a bonus, we will use the Sweet Alert library to ask if the user really wants to delete the customer.

Let’s get to the code!

Button definition

In the Grid’s onRecordevent, we use the following code to set the delete button:

Remember to remove the <?php and ?> tags if you copy and paste this code snippet

Notice that we are informing that the button will execute the JS method called ms_delete_customer .

So the next step is to create this method.

JS method creation

This method is an essential part of this article.

It is responsible for performing AJAX processing and also performing validation with Sweet Alert.

Then, go to the onScriptInitevent and paste the code below:

Note that most of it corresponds to how validation works with Sweet Alert.

However, we want you to focus only on the code between lines 17 and 34.

Note that here it is declared that a POST type request will be made to the application we are editing.

Also, we are passing the parameters:

  • ajax
  • option
  • id

Which will be used later on.

To adapt to your needs, you must edit line 19, changing the name of our application (grid_customers_action) to the name of your app.

The same goes for the parameters that we are passing on line 20. You must edit them to make it suitable for your needs.

Configuring the AJAX request receiver

Now, we need to define a receiver for our request.

So, still in the onScriptInitevent, copy and paste the code below:

Remember to remove the <?php and ?> tags if you copy and paste this code snippet

Note that we are validating if this execution is the AJAX request that we configured in the method above.

For this, we test if the ajax and option parameters are present.

Soon after, we verify the nature of this request and forward it to the function responsible for the execution.

In this case, as we want to delete a customer, we create the following function deleteCustomer($customer_id).

To create it, go to the application’s side menu “Programming -> PHPMethods” and choose “New Method”.

Define the method name and create the $customer_id parameter.

In the method code, we are performing the DELETEof the record through the sc_exec_sql() macro.

Remember to remove the <?php and ?> tags if you copy and paste this code snippet

That’ s all.

This is enough for us to have implemented client deletion via AJAX Processing and Action Buttons.

Running the application, the result would be this:

Say it here in the comments, this helps a lot in everyday life, doesn’t it?

Enjoy.

Greetings!

If you found this article helpful, go to the 👏 button and click how many times you enjoyed reading this post. Also don’t forget to comment and suggest the content you want to see here. This will certainly go a long way. We are still on Facebook, Instagram and Youtube. Together for a better Scriptcase!

--

--