dragon8837 2016-06-15 20:22
浏览 36

如何从.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();
    }
}
  • 写回答

1条回答 默认 最新

  • douwen5066 2016-06-15 20:38
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c