Swati Sharma Swati Sharma - 5 months ago 61
Ajax Question

To use sms sending api on click of button

I have a php form which makes an ajax call on click of sumbit button to save data in database. I want to call sms api to send these details on click of the same submit button. How do I do that?

I am using it from http://sms.hspsms.com/.

Here is the script which takes data annd save it to database:

<script>
$(function () {
$('#user-form').on('submit', function(e) {

$.ajax({
type: 'post',
url: './user-details.php',
data: $('#user-form').serialize(),
success: function () {
alert('SUCCESS');
}
});
e.preventDefault();
});
});
</script>


Where do I use sms api like:

http://sms.hspsms.com:/sendSMS?username=swati&message=XXXXXXXXXX&sendername=XYZ&smstype=TRANS&numbers=&apikey=XXXXXX

to send the details entered in the form.

Edit: I found the solution and it is posted as an answer.

Answer

I did it using this:

 $url = "http://sms.hspsms.com:/sendSMS";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, 'username=xxx&password=xxx&to=XXXXX&message=' . $phone . '&sendername=xxx&smstype=TRANS&numbers=xxxx&apikey=xxx');
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($ch);
        echo $result;

inside user-details.php

Comments