Call a php script on button click

Hi,

I want to execute a php code on custom button click event without using javascript, is it possible and how to ?

Thanks.

Mustafa

  • For executing any PHP script, you need call a server side URL or link. Because PHP is a server-side language.

    Either you can use AJAX/CURL or can put a direct <a href> link.

  • You can achieve by following below

    1. Add custom button in record.php

    2. In record.js on click call a function

    this.context.on('button:your_button:click', this.your_button_function, this);

    3. In function you should use ajax to call custom php file should be located under custom folder i-e /custom/abc.php

    your_button_function: function (fields, errors, callback) {

       var a_url=window.location.origin;
       var b_url=window.location.pathname;
       var f_url=a_url.concat(b_url);
       var myArray=f_url.split('/');
       var site_url=a_url+"/"+myArray[3]+"/";
       site_url = site_url.replace("index.php", "");
       var ResulData = $.ajax({
       url: site_url+'custom/abc.php?method=Test&id='+id,
       data: jQuery(this).serialize(),
       dataType: 'json',
          success: function(data){
             console.log(data);

             // any logic if required

          },
       });

    }

    4. In abc.php you can call db connection (only if required db) and use accordingly to execute your custom php code

    function connect1() {
       require_once '../config.php';
       $dbconfig = $sugar_config['dbconfig'];
       define('DB_HOST', $dbconfig['db_host_name']);
       define('DB_USER', $dbconfig['db_user_name']);
       define('DB_PASSWORD', $dbconfig['db_password']);
       define('DB_DATABASE', $dbconfig['db_name']);
       $link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD); // Connect to mysql server
       if(!$link) {
       return "Failed to connect to crm server:";
       return "";
    }

       $db = mysqli_select_db($link, DB_DATABASE); // Select database
       if(!$db) {
       return "Failed to connect to crm server database:";
       return "";
       }
       else { return TRUE; }
       }