Jump to content

How to make an ajax request inside a helper form?


Recommended Posts

I'd love to help, but I don't understand.
Do you need two buttons in the form?
Something like the Save button and the Save and stay button?
Please screen what you need.

Link to comment
Share on other sites

I need 2 buttons: 

1) Send message

2) Check the price

I found a way around it but  I still look for a way to not reload the page

$back = $_SERVER['HTTP_REFERER'];

if (!empty($back)) {
    //check for price with server
    
    Tools::redirectAdmin($back . '&price_checked=1');
}

 

Link to comment
Share on other sites

Hi There, 

There is a plenty of way to integrate ajax functionality to an admin module controller. 

First you need to bind an ajax function on click of that  button. You also need to send the module admin controller link to the js file. You can send the file via custom script tag in your helper form.

<?php 
$link = new Link();
$urladmin = $link->getAdminLink( 'YourControllerName' );
?>
<script type="text/javascript">
var priceExpiryDate = "{$urladmin}";
</script>

Then you can use ajax to fire function on the server. 

$.ajax({
            type: 'POST',
            cache: false,
            dataType: 'json',
            url: adminajax_link, 
            data: {
                ajax: true,
                action: 'yourActionName'//lowercase with action name
            },
            success : function (data) {
                console.log(data);
            },
            error : function (data){
                console.log(data);
            }
            
        });

Then do your php on code on the function e.g. that is your actionname in your admin controller class.

 public function ajaxProcessYourActionName()
    {
		echo json_encode('foo');//something you want to return
        exit;
    }

Please mind the camel case of the name.

Boom It is done.

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...