Thursday, September 7, 2017

jQuery Submit Form with Ajax

Hello,

In this blog we are going to take a look at how we can submit for with Ajax using jQuery.

First of all add jQuery file in your head section of HTML page.

<script src="jquery.min.js"></script>

Now we will have following form.

<form action="">
      <input id="text" autocomplete="off" placeholder="Type here" />
</form>

If you want to submit this form with Ajax using jQuery, first you have to do is add submit event handler.

$(function () {
$('form').submit(function(){
var formData = JSON.stringify($('form').serializeArray());
$.ajax({
type: "POST",
url: "YOUR_API_URL",
data: formData,
success: function(data){

},
failure: function(errMsg) {
}
});
return false;
    });
});

So first we have added submit event handler and used return false so it does refresh the whole page and just use the Ajax request.  Inside event handler we are using HTML 5 FormData to get form data first and then using Ajax request to send data to server in APIs.

Hope this helps you.

Saturday, September 2, 2017

Quick Tips Before You Start Working on IOT Project

Recently me and my team has completed an IOT project where we done GPRS Thermal printer integration with POS and Mobile App. We faced certain issues in project but later on we completed it successfully. Here I am sharing some quick tips before you start working on IOT project.



Readers who are new to the concept of IOT, please read my blog here on IOT.

IOT and IOT Examples, Read here.

1) Understand your IOT device and configure it

As we know IOT is all about connecting machine to Internet. For that you must understand the device and it's configurations. Most of the devices comes with user manual so it would be easy but still sometimes manuals are difficult to understand. For example in our case printer was manufactured in China so we got manuals with some Chinese languages so we had to figure out how it works and do some manual testings to connect printer to internet and get it working. Also you must understand the protocol on which device will work. For example most of the devices use Http protocol but some also work on socket. As per you web server and web services you have to decide protocol to be used.

2) Strictly follow protocol of Device.

This is again and important thing for IOT project. All the devices have some protocol to receive data and to get data back from device. You must follow that. If you don't, then your device will not work properly.

For example in our case if we have to send some data to printer we have to follow following syntax.

"&!*1Order*Item1*Item2#";

So here 1 stands for printer action

if it's 0 that means printer will just print the order and it will be accepted by default.

1 means printer will print it and user have to take action to accept or reject.

So in this case we thought 1 is the serial number so ignored it and printer stopped working.

So it's very important to strictly follow this syntax of devices because IOT devices are very strict in terms of syntax.

3) Have Patience, It will work for sure...

When you are working with hardwares and software together it's always difficult and time consuming. You have to deal with protocols and manual of devices to make it working and for that you should have patience. Also it's testing and development is not easy. First you write some code and trigger some events and then you check if that's working with device or not. So it really needs patience. Do not get frustrated and bang the device. Have patience and keep on working on it. It will work for sure. 

Monday, August 28, 2017

Using Google Translation API For Indian Language with PHP

Google translation API works good for Indian languages, if we type in proper language. For example, if you pass following in API. 

कैसे हो

Then it will translate properly to 

how are you

But if you pass it in english, like this.

"KAISE HO"

Then it will send the same text back but it only detects language. So it does not work properly. So what shall the possible solution for it. 

Here is how I worked out in one of my old project.

First I used Google input tools API to convert the input to regional language. 

So steps if First

KAISE HO is converted to

कैसे हो

and then pass it in translation API to convert it to English. Here is the code for the same.

$translatedText = "KAISE HO";
$detectedSourceLanguage = "hi";

$url ='https://www.google.com/inputtools/request?text='.urlencode($translatedText).'&ime=transliteration_en_'.urlencode($detectedSourceLanguage);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_PROXYPORT,3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
$response = curl_exec($ch);
$output = json_decode($response);
$resultText = '';

if($output[0] == 'SUCCESS'){
if(isset($output[1])){
if(isset($output[1][0])){
if(isset($output[1][0][1])){
$resultText = $output[1][0][1][0];
}
}
}
}

if($resultText != ''){
$url ='https://translation.googleapis.com/language/translate/v2';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"q=".urlencode($resultText)."&target=en&key=YOUR_API");

curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_PROXYPORT,3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
$response = curl_exec($ch);
$output = json_decode($response);
$resultText = $output->data->translations[0]->translatedText;
}

Hope this helps you in proper translation.