如何在表单提交中使用PHP函数进行操作?

时间:2017-09-22 06:17:46

标签: php forms

我正在使用这个表单和函数来感知短信,我需要帮助在表单的动作中使用函数,如何进一步移动应该直接在表单的动作部分添加函数或者我应该使用任何ajax用于调用函数

请建议我处理这种情况的简单方法,这是处理过程的正确方法吗

    <?php
    function sendSMS($mobile,$message,$senderid){
    $user = "APIKEY";
    $url = 'http://login.smsgatewayhub.com/api/mt/SendSMS?APIKey='.$user.'&senderid='.$senderid.'&channel=INT&DCS=0&flashsms=0&number='.$mobile.'&text='.$message.'&route=16;';

    $ch=curl_init($url);    
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch,CURLOPT_POST,1);
    curl_setopt($ch,CURLOPT_POSTFIELDS,"");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,2);
    $data = curl_exec($ch);
    print($data); /* result of API call*/

    }
    $senderid =$_POST['senderid'];
    $message = rawurlencode($_POST['message']);
    $mobile="0123456789";
sendSMS($mobile,$message);
?>
 <form role="form" method="POST" action="" >

        <div class="form-group row">
            <label class="col-sm-4">MobileNumbers<span class="text-danger">*</span></label>
            <div class="col-sm-7">
                <textarea class="form-control" rows="3" id="view-rows" name="mobilenumber" readonly></textarea>
            </div>
        </div>
        <div class="form-group row">
            <label class="col-sm-4">Select Sender ID<span class="text-danger">*</span></label>
            <div class="col-sm-7">
                 <select class="form-control" name="senderid" id="senderid" required>
                    <option value="">Select Sender ID</option>
                    <option value="FORITO">FORITO</option>
                    <option value="KWKMNT">KWKMNT</option>
                    <option value="KWKVEG">KWKVEG</option>
                </select>
            </div>
        </div>
        <div class="form-group row">
            <label class="col-sm-4">Enter Message<span class="text-danger">*</span></label>
            <div class="col-sm-7">
                <textarea class="form-control" rows="3" name="message"></textarea>
            </div>
        </div>
        <div class="form-group row">

            <input type="submit" class="btn btn-primary btn-sm" value="Send SMS">
        </div>
    </form>

2 个答案:

答案 0 :(得分:2)

您必须先为按钮提交指定名称

  <input type="submit" name="submit" class="btn btn-primary btn-sm" value="Send SMS">

然后在表单提交上调用函数

 <?php 

    if(isset($_POST['submit'])){
       $senderid =$_POST['senderid'];

       $message = rawurlencode($_POST['message']);
       $mobile = isset($_POST['mobilenumber']) ? $_POST['mobilenumber'] : '';
       sendSMS($mobile,$message);
    }
?>

答案 1 :(得分:2)

您还需要向表单添加操作URL,以便接收所有发布的数据

<form method='post' action='pathToController'>

其中pathToController是sendSMS函数所在的文件。

同样在sendSMS函数中,您忘记将发件人ID作为第三个参数传递。

  $senderid =$_POST['senderid'];
    $message = rawurlencode($_POST['message']);
    $mobile="0123456789";
sendSMS($mobile, $message, $senderid);//pass senderid as well