I am working on implementing a get link button that will send you a link via text once you type your phone number in the textfield. I am using Twilio to handle my SMS stuff, and I am following an app tutorial that teaches exactly what I am trying to implement. This is the tutorial and the ideal implementation of what I'd like to do, respectively:
http://www.apptamin.com/blog/app-download-button/
So far I have made an easy html file that just displays the button, a message, and the text field. This is the code for that file:
<html>
<head><title>Text Me The Link!</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
//Javascript
$(function(){
$("#frm").submit(function(e){
e.preventDefault();
$.post("textMeLink.php", $("#frm").serialize(),
function(data){
if(data.sms_sent == 'OK'){
alert("Message Sent");
} else {
alert("Message Not Sent");
}
}, "json");
});
});
</script>
</head>
<body>
<form id="frm" name="frm">
<input type="hidden" name="ajax" value="1"/>
<h4>Text your phone a download link for our iPhone and Android apps</h4>
<input type="phone" name="phone" placeholder="Enter Your Mobile Number"/>
<button type="submit">Get Link</button>
<div style="display: none; " class="error"/>
</form>
</body>
</html>
the PHP file works like a charm. I've tested it independently and I get SMS messages from Twilio without any issue. The problem is when I try it from the html file. Whenever I enter a phone number I get no alert, and the only thing that changes is the link it goes
from:
file:///C:/Users/Kevin/Marco%20Polo/twilio-php-master/sendLinkV0.html
to:
file:///C:/Users/Kevin/Marco%20Polo/twilio-php-master/sendLinkV0.html?ajax=1&phone=1234567890
After that no matter what other numbers I try, nothing works, the only thing that changes is the number at the end of the link and no SMS are ever sent by Twilio. I might just be missing a small part or probably am unaware that I have to have the file in the server instead of my C: drive, maybe I don't know. So if anyone is able to see where I went wrong, and explain why it's failing, that would be awesome!
Thanks,
Everyone
P.S. the html, php files as well as the Twilio library are all in the same directory which was changed in the WAMP httpd.conf file to be the new Documentroot
EDIT
I ran my php code and it seems that there error was with the code on the php file and it may not have been an issue with the html file. Here is my php file:
<?php
require "Services\Twilio.php";
$AccountSid = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$AuthToken = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy";
$client = new Services_Twilio($AccountSid, $AuthToken);
$phone=$_POST["phone"];
$sms = $client->account->sms_messages->create(
"+17327047999",
$phone,
"Get our app now: http://play.google.com/store/apps/details?id=some.app"
);
$sms_check="OK";
$return_json = '{"sms_sent":"' .$sms_check . '"}';
echo $return_json;
?>
It seems like the error stems from the fact way I've defined my $phone variable. The errors I get read:
Notice: Undefined index: phone in C:\Users\Kevin\Marco Polo\twilio-php-master\textMeLink.php on line 9
Warning: file_get_contents: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in C:\Users\Kevin\Marco Polo\twilio-php-master\Services\Twilio\HttpStream.php on line 61
Fatal error: Uncaught exception 'Services_Twilio_HttpStreamException' with message 'Unable to connect to service' in C:\Users\Kevin\Marco Polo\twilio-php-master\Services\Twilio\HttpStream.php on line 64
Services_Twilio_HttpStreamException: Unable to connect to service in C:\Users\Kevin\Marco Polo\twilio-php-master\Services\Twilio\HttpStream.php on line 64