Pages

Wednesday, June 8, 2016

Admission Form Android Example With PHP and MySQL

student_personal Table
DROP TABLE IF EXISTS `student_personal`;
CREATE TABLE IF NOT EXISTS `student_personal` (
 `reg_id` int(11) NOT NULL AUTO_INCREMENT,
 `first_name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `last_name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `DOB` date NOT NULL,
 `gender` varchar(6) COLLATE utf8_unicode_ci NOT NULL,
 `blood_group` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
 `birth_place` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
 `pin` varchar(6) COLLATE utf8_unicode_ci NOT NULL,
 `city` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `state` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `country` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
 `mother_tongue` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
 `Religion` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
 `DOJ` date NOT NULL,
 `Standard` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
 `Batch` varchar(5) COLLATE utf8_unicode_ci NOT NULL,
 PRIMARY KEY (`reg_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=10 ;
student_personal.php
<?php

include "conn.php";

$fname=$_POST["fname"];
$lname=$_POST["lname"];
$dob=$_POST["dob"];
$sex=$_POST["sex"];
$blood=$_POST["blood"];
$bithplace=$_POST["bithplace"];
$pin=$_POST["pin"];
$city=$_POST["city"];
$state=$_POST["state"];
$country=$_POST["country"];
$mother_ton=$_POST["mother_ton"];
$religions=$_POST["religions"];
$doj=$_POST["doj"];
$course=$_POST["course"];
$batch=$_POST["batch"];


$sql="INSERT INTO `u179022344_db`.`student_personal` (`reg_id`, `first_name`, `last_name`, `DOB`, `gender`, `blood_group`, `birth_place`, `pin`, `city`, `state`, `country`, `mother_tongue`, `Religion`, `DOJ`, `Standard`, `Batch`) VALUES (NULL, '$fname', '$lname', '$dob', '$sex', '$blood', '$bithplace', '$pin', '$city', '$state', '$country', '$mother_ton', '$religions', '$doj', '$course', '$batch');";



if(!mysqli_query($conn,$sql))
{
 echo "fail";
}
else
{
 echo "success";
}
?>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context="kantapp.admission.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <EditText
            android:id="@+id/fname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="First Name"
            android:inputType="textPersonName" />


        <EditText
            android:id="@+id/lname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Last Name"
            android:inputType="textPersonName" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/txt_dob"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="5dp"
                android:paddingTop="10dp"
                android:text="YYYY-MM-DD (Date of Birth)"
                android:textColor="#000000"
                android:textSize="15dp" />

            <ImageView
                android:id="@+id/img_dob"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_marginLeft="10dp"
                android:onClick="onImageDOBClick"
                android:src="@drawable/calendar" />
        </LinearLayout>

        <RadioGroup
            android:id="@+id/radioSex"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/radioMale"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Male" />

            <RadioButton
                android:id="@+id/radioFemale"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Female" />

        </RadioGroup>


        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="10dip">


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"

                android:text="Blood Type :"
                android:textColor="#000000" />

            <Spinner
                android:id="@+id/spinner"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1" />


        </LinearLayout>

        <EditText
            android:id="@+id/birthplace"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Birth Place"
            android:inputType="textPersonName" />

        <EditText
            android:id="@+id/pin"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Pin Code"
            android:inputType="number" />

        <EditText
            android:id="@+id/city"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="City"
            android:inputType="textPersonName" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="10dip">


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="State :"

                android:textColor="#000000"
                android:textSize="15dp" />

            <Spinner
                android:id="@+id/state"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1" />


        </LinearLayout>

        <EditText
            android:id="@+id/india"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="India" />

        <EditText
            android:id="@+id/mother_ton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Mother Tongue"
            android:inputType="textPersonName" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="10dip">


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Religion :"

                android:textColor="#000000"
                android:textSize="15dp" />

            <Spinner
                android:id="@+id/religion"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1" />


        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/txt_doj"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="5dp"
                android:paddingTop="10dp"
                android:text="YYYY-MM-DD (Date of Join)"
                android:textColor="#000000"
                android:textSize="15dp" />

            <ImageView
                android:id="@+id/img_doj"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_marginLeft="10dp"
                android:onClick="onImageDOJClick"
                android:src="@drawable/calendar" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="10dip">


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Standard :"

                android:textColor="#000000"
                android:textSize="15dp" />

            <Spinner
                android:id="@+id/standard"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1" />


        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="10dip">


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Batch :"

                android:textColor="#000000"
                android:textSize="15dp" />

            <Spinner
                android:id="@+id/batch"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1" />


        </LinearLayout>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onSaveClick"
            android:text="Save" />
    </LinearLayout>


</ScrollView>
MainActivity.java
package kantapp.admission;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Calendar;

public class MainActivity extends Activity {

    RadioGroup radioSex;
    RadioButton gen;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        onEditText();
        onBloodType();
        onState();
        onReligion();
        onBatch();
        onStandard();

    }

    EditText fname, lname,birth_place,pin,city,country,mother_ton;

    public void onEditText() {
        fname = (EditText) findViewById(R.id.fname);
        lname = (EditText) findViewById(R.id.lname);
        birth_place=(EditText)findViewById(R.id.birthplace);
        pin=(EditText)findViewById(R.id.pin);
        city=(EditText)findViewById(R.id.city);
        country=(EditText)findViewById(R.id.india);
        mother_ton=(EditText)findViewById(R.id.mother_ton);
    }

    TextView dob;

    public void onImageDOBClick(View view) {
        dob = (TextView) findViewById(R.id.txt_dob);
        Calendar calendar = Calendar.getInstance();
        new DatePickerDialog(MainActivity.this, onDateSetListener, calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
    }

    DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            int month = monthOfYear + 1;
            dob.setText(year + "-" + month + "-" + dayOfMonth);
        }
    };




    String[] bloodtype = {"O Positive", "A Positive", "B Positive", "AB Positive",
            "O Negative", "A Negative", "B Negative", "AB Negative"};
    Spinner blood;

    public void onBloodType() {

        blood = (Spinner) findViewById(R.id.spinner);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, bloodtype);
        blood.setAdapter(adapter);

        Toast.makeText(MainActivity.this, blood.getSelectedItem().toString(), Toast.LENGTH_SHORT).show();
    }

    //String[] abc={"A","B"};
    Spinner state;
    public void onState()
    {
        state=(Spinner)findViewById(R.id.state);
        ArrayAdapter<String> adapter=new ArrayAdapter<String>
                (MainActivity.this,android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.india_states));

        state.setAdapter(adapter);
    }

    String[] religions={"Hinduism","Islam","Christianity","Sikhism","Buddhism","Jainism","Zoroastrianism","Others"};
    Spinner religion;
    public void onReligion()
    {
        religion=(Spinner)findViewById(R.id.religion);
        ArrayAdapter<String> adapter=new ArrayAdapter<String>
                (MainActivity.this,android.R.layout.simple_spinner_item,religions);

        religion.setAdapter(adapter);
    }

    TextView doj;

    public void onImageDOJClick(View view) {
        doj = (TextView) findViewById(R.id.txt_doj);
        Calendar calendar = Calendar.getInstance();
        new DatePickerDialog(MainActivity.this, d, calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
    }

    DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            int month = monthOfYear + 1;
            doj.setText(year + "-" + month + "-" + dayOfMonth);
        }
    };

    String[] standards={"Class I","Class II","Class III","Class IV","Class V","Class VI","Class VII","Class VIII","Class IX","Class X","Class XI","Class XII"};
    Spinner standard;
    public void onStandard()
    {
        standard=(Spinner)findViewById(R.id.standard);
        ArrayAdapter<String> adapter=new ArrayAdapter<String>
                (MainActivity.this,android.R.layout.simple_spinner_item,standards);

        standard.setAdapter(adapter);
    }

    String[] batchs={"A","B","C","D","E"};
    Spinner batch;
    public void onBatch()
    {
        batch=(Spinner)findViewById(R.id.batch);
        ArrayAdapter<String> adapter=new ArrayAdapter<String>
                (MainActivity.this,android.R.layout.simple_spinner_item,batchs);

        batch.setAdapter(adapter);
    }

    public void onSaveClick(View view)
    {

        radioSex = (RadioGroup) findViewById(R.id.radioSex);
        int selectID = radioSex.getCheckedRadioButtonId();
        gen = (RadioButton) findViewById(selectID);

        String first_name=fname.getText().toString();
        String last_name=lname.getText().toString();
        String birthplace=birth_place.getText().toString();
        String pins=pin.getText().toString();
        String citys=city.getText().toString();
        String countrys=country.getText().toString();
        String mother_tons=mother_ton.getText().toString();

        String bloodz=blood.getSelectedItem().toString();
        String statez=state.getSelectedItem().toString();
        String religionz=religion.getSelectedItem().toString();
        String standardz=standard.getSelectedItem().toString();
        String batchz=batch.getSelectedItem().toString();

        String dobz=dob.getText().toString();
        String dojz=doj.getText().toString();
      String sex=gen.getText().toString();

        String method="personal";
        BackGroundTask backGroundTask=new BackGroundTask(this);
        backGroundTask.execute(method,first_name,last_name,dobz,sex,bloodz,birthplace,pins,citys,statez,countrys,mother_tons,dojz,standardz,batchz,religionz);


    }
}
BackGroundTask.java
package kantapp.admission;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;

public class BackGroundTask extends AsyncTask<String, Void, String> {
    Context context;
    AlertDialog alertDialog;

    public BackGroundTask(Context context) {
        this.context = context;
    }

    @Override
    protected String doInBackground(String... params) {
        String method = params[0];

        if (method.equals("personal")) {
            try {
                String fname = params[1];
                String lname = params[2];
                String dob = params[3];
                String sex = params[4];
                String blood = params[5];
                String bithplace = params[6];
                String pin = params[7];
                String city = params[8];
                String state = params[9];
                String country = params[10];
                String mother_ton = params[11];

                String doj = params[12];
                String course = params[13];
                String batch = params[14];
                String religions = params[15];


                URL url = new URL("http://varahii.16mb.com/student/student_personal.php");

                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));

                String Data = URLEncoder.encode("fname", "UTF-8") + "=" + URLEncoder.encode(fname, "UTF-8") + "&" +
                        URLEncoder.encode("lname", "UTF-8") + "=" + URLEncoder.encode(lname, "UTF-8") + "&" +
                        URLEncoder.encode("dob", "UTF-8") + "=" + URLEncoder.encode(dob, "UTF-8") + "&" +
                        URLEncoder.encode("sex", "UTF-8") + "=" + URLEncoder.encode(sex, "UTF-8") + "&" +
                        URLEncoder.encode("blood", "UTF-8") + "=" + URLEncoder.encode(blood, "UTF-8") + "&" +
                        URLEncoder.encode("bithplace", "UTF-8") + "=" + URLEncoder.encode(bithplace, "UTF-8") + "&" +
                        URLEncoder.encode("pin", "UTF-8") + "=" + URLEncoder.encode(pin, "UTF-8") + "&" +
                        URLEncoder.encode("city", "UTF-8") + "=" + URLEncoder.encode(city, "UTF-8") + "&" +
                        URLEncoder.encode("state", "UTF-8") + "=" + URLEncoder.encode(state, "UTF-8") + "&" +
                        URLEncoder.encode("country", "UTF-8") + "=" + URLEncoder.encode(country, "UTF-8") + "&" +
                        URLEncoder.encode("mother_ton", "UTF-8") + "=" + URLEncoder.encode(mother_ton, "UTF-8") + "&" +
                        URLEncoder.encode("religions", "UTF-8") + "=" + URLEncoder.encode(religions, "UTF-8") + "&" +
                        URLEncoder.encode("doj", "UTF-8") + "=" + URLEncoder.encode(doj, "UTF-8") + "&" +
                        URLEncoder.encode("course", "UTF-8") + "=" + URLEncoder.encode(course, "UTF-8") + "&" +
                        URLEncoder.encode("batch", "UTF-8") + "=" + URLEncoder.encode(batch, "UTF-8");

                bufferedWriter.write(Data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();

                InputStream inputStream = httpURLConnection.getInputStream();

                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));

                String response = "";
                String line = "";

                while ((line = bufferedReader.readLine()) != null) {
                    response += line;
                }

                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();

                return response;

            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onPreExecute() {
        alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setTitle("Status");
    }

    @Override
    protected void onPostExecute(String s) {
        alertDialog.setMessage(s);
        if (s.equals("success")) {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setPositiveButton("Next", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    context.startActivity(new Intent(context.getApplicationContext(), Student_contact.class));
                }
            });

            alertDialog = builder.create();

        }
        alertDialog.show();
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

}
Student_contact.java
package kantapp.admission;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Student_contact extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.student_contact);
    }
}
student_contact.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="kantapp.admission.Student_contact">


</ScrollView>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="kantapp.admission">
<uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Student_contact"></activity>
    </application>

</manifest>
string.xml
<resources>
    <string name="app_name">Admission</string>
    <string-array name="india_states">
        <item>Select State</item>
        <item>Andaman and Nicobar Islands</item>
        <item>Andhra Pradesh</item>
        <item>Arunachal Pradesh</item>
        <item>Assam</item>
        <item>Bihar</item>
        <item>Chandigarh</item>
        <item>Chhattisgarh</item>
        <item>Dadra and Nagar Haveli</item>
        <item>Daman and Diu</item>
        <item>Delhi</item>
        <item>Goa</item>
        <item>Gujarat</item>
        <item>Haryana</item>
        <item>Himachal Pradesh</item>
        <item>Jammu and Kashmir</item>
        <item>Jharkhand</item>
        <item>Karnataka</item>
        <item>Kerala</item>
        <item>Lakshadweep</item>
        <item>Madhya Pradesh</item>
        <item>Maharashtra</item>
        <item>Manipur</item>
        <item>Meghalaya</item>
        <item>Mizoram</item>
        <item>Nagaland</item>
        <item>Orissa</item>
        <item>Pondicherry</item>
        <item>Punjab</item>
        <item>Rajasthan</item>
        <item>Sikkim</item>
        <item>Tamil Nadu</item>
        <item>Tripura</item>
        <item>Uttaranchal</item>
        <item>Uttar Pradesh</item>
        <item>West Bengal</item>
    </string-array>
</resources>

No comments:

Post a Comment