3

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>
1
  • First you need to check the values are set or not then call the function Commented Sep 22, 2017 at 6:23

2 Answers 2

2

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);
    }
?>
Sign up to request clarification or add additional context in comments.

1 Comment

@sairam duggi. Also you need to add a URL (that .php file) in action attribute of the html form element.
2

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 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.