如何从.js文件运行.php函数文件

How do I send data from a JavaScript file to a PHP file so that it does what it needs to do in the PHP server side. I want to send a SMS and my JavaScript is reading all the data that is coming though so all that is left is that my PHP activates and sends the data in a SMS which is already done with my PHP file. I just need to make them connect to be able to send.

Can PHP handle functions? That is what I am trying to do here by sending the data to a function in PHP from my .js file. If not, how do I send them via post?

.js file:

render : function(template,params){

    var arr = [];
    switch(template){

        case 'smsLine':
            arr = [
                '<div class=" sms-',params.id,' rounded"><span class="gravatar"><img src="',params.gravatar,
                '" width="23" height="23" onload="this.style.visibility=\'visible\'" />',
                '</span><span class="author">',params.author,
                ':</span><span class="text">',params.text,
                ':</span><span class="text">',params.to,
                '</span><span class="time">',params.time,'</span></div>'];

                ///////////////////////////////HERE/////////////////////////////////
                //this is where I want to use a function that is in a php file
                sendSMS(params.author, params.text, params.time, params.to);

                ///////////////////////////////HERE////////////////////////////////
        break;

    }

    return arr.join('');
}

This is the function that I want to use in my PHP file.

.php file:

function sendSMS($from, $message, $time, $to){
    $objGsm = new COM("AxSms.Gsm", NULL, CP_UTF8 );
    $objGsm->LogFile = sys_get_temp_dir()."Gsm.log"; 
    //Windows default: 'C:\Windows\Temp\Gsm.log'

    //Form submitted

    $obj;
    $strMessageReference;
    $objSmsMessage   = new COM("AxSms.Message",    NULL, CP_UTF8 );
    $objSmsConstants = new COM("AxSms.Constants" , NULL, CP_UTF8 );

    $strName = 'Modem';
    $strPincode = '';
    $strRecipient = '$number';
    $iDeviceSpeed = '0';

    $objGsm->Clear();
    $objGsm->LogFile = '';
    $objGsm->Open($strName, $strPincode, $iDeviceSpeed);

    if ($objGsm->LastError != 0){
        $strResult = $objGsm->LastError . ": " . $objGsm->GetErrorDescription($objGsm->LastError);
        $objGsm->Close();
    }
    else{
        //Message Settings
        $objSmsMessage->Clear();
        $objSmsMessage->ToAddress = $to;
        $objSmsMessage->Body = $message;

        $objSmsMessage->DataCoding = $objSmsConstants->DATACODING_UNICODE;

        //Send the message !
        $obj = $objSmsMessage;
        $objGsm->SendSms($obj, $objSmsConstants->MULTIPART_ACCEPT, 0);
        $objSmsMessage = $obj;

        $strResult = $objGsm->LastError . ": " . $objGsm->GetErrorDescription($objGsm->LastError);

        $objGsm->Close();
    }
}

If you pull in jQuery to your front-end, you'll be able to send an AJAX request to execute that PHP function for you. it would look something like this (inserted straight into that sendSMS section in the .js code:

$.ajax() {
    url: "/send/sms",
    type: "POST",
    data: {
        author: params.author, 
        text: params.text,
        time: params.time,
        to: params.to,
    }
}

Now what you will have to do is create the file for the AJAX request to be sent to, they call this an "end point". In my example I set the path of this file to being /send/sms, so perhaps you have a directory called "send" where you could send off an email or SMS, etc. For each of those methods you would have a PHP file containing the logic for it. So for this example, create an sms.php file inside YOUR_ROOT_DIRECTORY/send .

Once you send an AJAX request to that file, the PHP function will be executed. To fetch the given data, use $_POST['author'], $_POST['text'], etc.