Thursday, 10 October 2013

how to take input in PHP

HOW TO TAKE INPUT IN PHP:


Its a very difficult task to take input in PHP, so I have developed a function get_input($num_of_inputs) which takes num of input variable and returns those inputs in an array.

For Example: if i wanted to take 3 inputs

<?php
//DO NOT MODIFY THE CODE
// processing started
function get_input($num_of_inputs){
        global $_NUM_OF_INPUTS;
 $_NUM_OF_INPUTS = $num_of_inputs;
 $inputs = array();
 if (isset($_POST['submit'])) {
  $value = isset($_POST['input0'])? $_POST['input0'] : null;
  for($count = 1; $value != null; $count++){
   array_push($inputs, $value);
   $value = isset($_POST['input'.$count])? $_POST['input'.$count] : null;
  }
 }
 return $inputs;
}
// processing ends

//change code below
$inputs = get_input(3);
//var dump is used to check the inputs
var_dump($inputs);
// to access specific index use echo $inputs[0];
/*
 *
 *
 *code here
 *
 *
 *
 */
?>

<!-- copy this form and paste it where you wanted that your text boxes will appear -->
<!-- DO NOT MODIFY THE CODE -->
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    <?php
        for($count = 0; $count < $_NUM_OF_INPUTS; $count++){
            printf("Enter input %'02d:", $count+1);
            echo '&nbsp;<input type="text" name="input' . $count .'" value="" /><br />';
        }
        echo '<input type="submit" name="submit" value="Submit" />';
    ?>
</form>
Preview LOOKED LIKE BELOW:



Monday, 5 August 2013

Client-Server Application for Android




Android client file: Main.java
 package com.nab.clientserver;  
 import java.io.IOException;  
 import java.io.PrintWriter;  
 import java.net.Socket;  
 import java.net.UnknownHostException;  
 import android.os.AsyncTask;  
 import android.os.Bundle;  
 import android.app.Activity;  
 import android.util.Log;  
 import android.view.Menu;  
 import android.view.View;  
 import android.widget.Button;  
 import android.widget.EditText;  
 public class Main extends Activity {  
      static Socket clientSocket;  
      static PrintWriter pr;  
      static EditText et;  
      static EditText ipText;  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.main);  
           et = (EditText) findViewById(R.id.messageField);  
           ipText = (EditText) findViewById(R.id.ipText);  
           Button b = (Button) findViewById(R.id.button1);  
           b.setOnClickListener(new View.OnClickListener() {  
                @Override  
                public void onClick(View v) {  
                     myAsyncTask at = new myAsyncTask();  
                     at.execute();  
                }  
           });  
      }  
      @Override  
      public boolean onCreateOptionsMenu(Menu menu) {  
           // Inflate the menu; this adds items to the action bar if it is present.  
           getMenuInflater().inflate(R.menu.main, menu);  
           return true;  
      }  
      class myAsyncTask extends AsyncTask<Void, Void, Void>  {  
     myAsyncTask()  {  
       //constructor;        
     }  
     @Override  
     protected void onPostExecute(Void result) {  
       super.onPostExecute(result);  
     }  
     @Override  
     protected Void doInBackground(Void...arg0) {  
          try {  
                     clientSocket = new Socket(ipText.getText().toString(), 4444);  
                     pr = new PrintWriter(clientSocket.getOutputStream(), true);  
                     pr.write(et.getText().toString());  
                     pr.flush();  
                     pr.close();  
                     clientSocket.close();  
                } catch (UnknownHostException e) {  
                     // TODO Auto-generated catch block  
                     e.printStackTrace();  
                     Log.d("NAB", "unknown host");  
                } catch (IOException e) {  
                     // TODO Auto-generated catch block  
                     e.printStackTrace();  
                     Log.d("NAB", "IO exception");  
                }  
          return null;  
     }  
     @Override  
     protected void onPreExecute() {  
       super.onPreExecute();  
     }  
   }    
 }  

Graphical Layout file: main.xml

1:  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
2:    xmlns:tools="http://schemas.android.com/tools"  
3:    android:layout_width="match_parent"  
4:    android:layout_height="match_parent"  
5:    android:paddingBottom="@dimen/activity_vertical_margin"  
6:    android:paddingLeft="@dimen/activity_horizontal_margin"  
7:    android:paddingRight="@dimen/activity_horizontal_margin"  
8:    android:paddingTop="@dimen/activity_vertical_margin"  
9:    tools:context=".Main" >  
10:    <Button  
11:      android:id="@+id/button1"  
12:      android:layout_width="wrap_content"  
13:      android:layout_height="wrap_content"  
14:      android:layout_alignLeft="@+id/messageField"  
15:      android:layout_below="@+id/messageField"  
16:      android:gravity="left"  
17:      android:text="@string/send" />  
18:    <EditText  
19:      android:id="@+id/ipText"  
20:      android:layout_width="match_parent"  
21:      android:layout_height="wrap_content"  
22:      android:layout_alignLeft="@+id/messageField"  
23:      android:layout_alignParentTop="true"  
24:      android:ems="10"  
25:      android:inputType="number"  
26:      android:text="192.168.0.100" >  
27:      <requestFocus />  
28:    </EditText>  
29:    <EditText  
30:      android:id="@+id/messageField"  
31:      android:layout_width="match_parent"  
32:      android:layout_height="wrap_content"  
33:      android:layout_below="@+id/ipText"  
34:      android:layout_centerHorizontal="true"  
35:      android:ems="10"  
36:      android:inputType="text"  
37:      android:text="hey i&apos;m NAB" >  
38:      <requestFocus />  
39:    </EditText>  
40:  </RelativeLayout>  

AndroidManifest.xml
 <?xml version="1.0" encoding="utf-8"?>  
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
   package="com.nab.clientserver"  
   android:versionCode="1"  
   android:versionName="1.0" >  
   <uses-sdk  
     android:minSdkVersion="8"  
     android:targetSdkVersion="17" />  
   <uses-permission android:name="android.permission.INTERNET"/>  
   <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>  
   <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>  
   <application  
     android:allowBackup="true"  
     android:icon="@drawable/ic_launcher"  
     android:label="@string/app_name"  
     android:theme="@style/AppTheme" >  
     <activity  
       android:name="com.nab.clientserver.Main"  
       android:label="@string/app_name" >  
       <intent-filter>  
         <action android:name="android.intent.action.MAIN" />  
         <category android:name="android.intent.category.LAUNCHER" />  
       </intent-filter>  
     </activity>  
   </application>  
 </manifest>  

In the last create Server.java file


 import java.io.BufferedReader;  
 import java.io.IOException;  
 import java.io.InputStreamReader;  
 import java.net.ServerSocket;  
 import java.net.Socket;  
 public class Server {  
      private static Socket clientSocket;  
      private static ServerSocket serverSocket;  
      public static void main(String[] args) {  
           try {  
                serverSocket = new ServerSocket(4444);  
           } catch (IOException e) {  
                print("Exception agai" + e.toString());  
                e.printStackTrace();  
           }  
           print(serverSocket.getInetAddress().toString());  
           print("Server is listening ....");  
           while (true) {  
                try {  
                     clientSocket = serverSocket.accept();  
                     InputStreamReader isr = new InputStreamReader(  
                               clientSocket.getInputStream());  
                     BufferedReader br = new BufferedReader(isr);  
                     print(br.readLine());  
                     isr.close();  
                     clientSocket.close();  
                } catch (IOException e) {  
                     print("Exception agai" + e.toString());  
                     e.printStackTrace();  
                }  
           }  
      }  
      public static void print(String mesg) {  
           System.out.println(mesg);  
      }  
 }  
Note:

  1. Run the Server first and then the client!
  2. AsyncTask class is mandatory to run client server architecture on device.
  3. to get ip address of your computer ... goto run->write "cmd" ->write "ipconfig" and it will show your IP configuration .... see IPv4 Address there.