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

时间:2021-01-14 15:58:36

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.

如何将数据从JavaScript文件发送到PHP文件,以便它在PHP服务器端执行它需要做的事情。我想发送一条短信,我的JavaScript正在读取即将发布的所有数据,所以剩下的就是我的PHP激活并通过我的PHP文件已完成的短信发送数据。我只需要让它们连接就可以发送。

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?

PHP可以处理函数吗?这就是我在这里尝试通过从.js文件将数据发送到PHP中的函数。如果没有,我如何通过邮寄发送?

.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文件中使用的函数。

.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();
    }
}

1 个解决方案

#1


0  

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:

如果你将jQuery引入前端,你将能够发送一个AJAX请求来为你执行该PHP函数。它看起来像这样(直接插入.js代码中的sendSMS部分:

$.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 .

现在你需要做的是为要发送的AJAX请求创建文件,他们称之为“终点”。在我的示例中,我将此文件的路径设置为/ send / sms,因此您可能有一个名为“send”的目录,您可以在其中发送电子邮件或短信等。对于每种方法,您都有一个PHP文件包含它的逻辑。因此,对于此示例,在YOUR_ROOT_DIRECTORY / send中创建一个sms.php文件。

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.

将AJAX请求发送到该文件后,将执行PHP函数。要获取给定数据,请使用$ _POST ['author'],$ _POST ['text']等。

#1


0  

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:

如果你将jQuery引入前端,你将能够发送一个AJAX请求来为你执行该PHP函数。它看起来像这样(直接插入.js代码中的sendSMS部分:

$.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 .

现在你需要做的是为要发送的AJAX请求创建文件,他们称之为“终点”。在我的示例中,我将此文件的路径设置为/ send / sms,因此您可能有一个名为“send”的目录,您可以在其中发送电子邮件或短信等。对于每种方法,您都有一个PHP文件包含它的逻辑。因此,对于此示例,在YOUR_ROOT_DIRECTORY / send中创建一个sms.php文件。

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.

将AJAX请求发送到该文件后,将执行PHP函数。要获取给定数据,请使用$ _POST ['author'],$ _POST ['text']等。