Sairam Duggi
Sairam Duggi

Reputation: 165

How use PHP function for action in form submission?

I am using this form and function to sens sms,I need help in the usage of function in the action of the form, How would I move further should directly add function in action part of the form or should I use any ajax for calling the function

Please suggest me the simple way of handling this situation, Is this the right way of handling the process

    <?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>

Upvotes: 3

Views: 110

Answers (2)

Rotimi
Rotimi

Reputation: 4825

You also need to add an action URL to your form so as to receive all the posted data

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

Where pathToController is the file where the sendSMS function exists.

Also in the sendSMS function, you forgot to pass the sender id as the third parameter.

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

Upvotes: 2

Pritamkumar
Pritamkumar

Reputation: 686

you must first assign name to button submit

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

and then call function on form submit

 <?php 

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

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

Upvotes: 2

Related Questions