2020-12-27

(Multi Step Kotlin Form in android) How can I get ID number in extra after submitting the form? once I submit the form go to second step of form

I am creating a drug addict patient registration form. The first part of the form submits but i am facing problem in second part where i need to enter guardians information and that table is related to patient information table. I need help in how do I get "patientID" from the first form after submission as an extra and it will be posted in guardian form patientID field.

My php API File

<?php 
 
 //adding dboperation file 
 require_once '../includes/DbOperation.php';
 
 //response array 
 $response = array(); 
 
 //if a get parameter named op is set we will consider it as an api call 
 if(isset($_GET['op'])){
 
 //switching the get op value 
 switch($_GET['op']){
 
 //if it is add artist 
 //that means we will add an artist 
 case 'addpatient':
 if(isset($_POST['date']) && isset($_POST['patientname']) && isset($_POST['fathername']) && isset($_POST['gender']) && isset($_POST['age']) && isset($_POST['agegroup']) && isset($_POST['cnicno']) && isset($_POST['religion']) && isset($_POST['address']) && isset($_POST['profession'])){
 $db = new DbOperation(); 
 if($db->createPatient($_POST['date'], $_POST['patientname'], $_POST['fathername'], $_POST['gender'], $_POST['age'], $_POST['agegroup'], $_POST['cnicno'], $_POST['religion'], $_POST['address'], $_POST['profession'])){
 $response['error'] = false;
 $response['message'] = 'Data added successfully';
 }else{
 $response['error'] = true;
 $response['message'] = 'Could not add record';
 }
 }else{
 $response['error'] = true; 
 $response['message'] = 'Required Parameters are missing';
 }
 break;
 
 case 'addguardian':
 if(isset($_POST['patientid']) && isset($_POST['guardianname']) && isset($_POST['relation']) && isset($_POST['address']) && isset($_POST['cnic']) && isset($_POST['contactno']) && isset($_POST['profession'])){
 $db = new DbOperation(); 
 if($db->createGuardian($_POST['patientid'], $_POST['guardianname'], $_POST['relation'], $_POST['address'], $_POST['cnic'], $_POST['contactno'], $_POST['profession'])){
 $response['error'] = false;
 $response['message'] = 'Data added successfully';
 }else{
 $response['error'] = true;
 $response['message'] = 'Could not add record';
 }
 }else{
 $response['error'] = true; 
 $response['message'] = 'Required Parameters are missing';
 }
 break;
 
 //if it is getartist that means we are fetching the records
 case 'getpatients':
 $db = new DbOperation();
 $patient = $db->getPatient();
 if(count($patient)<=0){
 $response['error'] = true; 
 $response['message'] = 'Nothing found in the database';
 }else{
 $response['error'] = false; 
 $response['patient'] = $patient;
 }
 break; 
 
 default:
 $response['error'] = true;
 $response['message'] = 'No operation to perform';
 
 }
 
 }else{
 $response['error'] = false; 
 $response['message'] = 'Invalid Request';
 }
 
 //displaying the data in json 
 echo json_encode($response);

ADD Patient is where I add patient record and add guardian where it adds guardian record

Here is my DBOperation File

<?php
 
class DbOperation
{
    private $con;
 
    function __construct()
    {
        require_once dirname(__FILE__) . '/DbConnect.php';
        $db = new DbConnect();
        $this->con = $db->connect();
    }
 
 //adding a record to database 
 public function createPatient($date, $patientname, $fathername, $gender, $age, $agegroup, $cnicno, $religion, $address, $profession){
 $stmt = $this->con->prepare("INSERT INTO patientrecord (date, patientname, fathername, gender, age, agegroup, cnicno, religion, address, profession) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
 $stmt->bind_param("ssssssssss", $date, $patientname, $fathername, $gender, $age, $agegroup, $cnicno, $religion, $address, $profession);
 if($stmt->execute())
 return true; 
 return false; 
 }
 
 public function createGuardian($patientid, $guardianname, $relation, $address, $cnic, $contactno, $profession){
 $stmt = $this->con->prepare("INSERT INTO guardianrecord (patientid, guardianname, relation, address, cnic, contactno, profession) VALUES (?, ?, ?, ?, ?, ?, ?)");
 $stmt->bind_param("sssssss", $patientid, $guardianname, $relation, $address, $cnic, $contactno, $profession);
 if($stmt->execute())
 return true; 
 return false; 
 }
 
 //fetching all records from the database 
 public function getPatient(){
 $stmt = $this->con->prepare("SELECT id, date, patientname,, fathername, gender, age, agegroup, cnicno, religion, address, profession FROM patientrecord");
 $stmt->execute();
 $stmt->bind_result($id, $date, $patientname, $fathername, $gender, $age, $agegroup, $cnicno, $religion, $address, $profession);
 $artists = array();
 
 while($stmt->fetch()){
 $temp = array(); 
 $temp['id'] = $id; 
 $temp['date'] = $date; 
 $temp['patientname'] = $patientname;
 $temp['fathername'] = $fathername;
 $temp['gender'] = $gender;
 $temp['age'] = $age;
 $temp['agegroup'] = $agegroup;
 $temp['cnicno'] = $cnicno;
 $temp['religion'] = $religion;
 $temp['address'] = $address;
 $temp['profession'] = $profession;
 array_push($patient, $temp);
 }
 return $patient; 
 }
}

Patient Information Form Activity

class ProjectActivity : AppCompatActivity() {

    //edittext and spinner
    private var editTextID: EditText? = null
    private var editTextDate: EditText? = null
    private var editTextPatientName: EditText? = null
    private var editTextFatherName: EditText? = null
    private var spinnerGender: Spinner? = null
    private var editTextAge: EditText? = null
    private var spinnerAgeGroup: Spinner? = null
    private var editTextCNIC: EditText? = null
    private var spinnerReligion: Spinner? = null
    private var editTextAddress: EditText? = null
    private var editTextProfession: EditText? = null

   override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_project)

        val c: Calendar = Calendar.getInstance()
        System.out.println("Current time => " + c.time)

        val df = SimpleDateFormat("dd-MM-yyyy HH:mm:ss")
        val formattedDate: String = df.format(c.time)
        // formattedDate have current date/time
        // formattedDate have current date/time
        Toast.makeText(this, formattedDate, Toast.LENGTH_SHORT).show()



        //getting it from xml
        editTextID = findViewById<EditText>(R.id.editTextID)
        editTextDate = findViewById<EditText>(R.id.editTextDate)
        editTextPatientName = findViewById<EditText>(R.id.editTextPatientName)
        editTextFatherName = findViewById<EditText>(R.id.editTextFatherName)
        spinnerGender = findViewById<Spinner>(R.id.spinnerGender)
        editTextAge = findViewById<EditText>(R.id.editTextAge)
        spinnerAgeGroup = findViewById<Spinner>(R.id.spinnerAgeGroup)
        editTextCNIC = findViewById<EditText>(R.id.editTextCNIC)
        spinnerReligion = findViewById<Spinner>(R.id.spinnerReligion)
        editTextAddress = findViewById<EditText>(R.id.editTextAddress)
        editTextProfession = findViewById<EditText>(R.id.editTextProfession)

        editTextDate!!.setText(formattedDate)
        //adding a click listener to button
        this.findViewById<Button>(R.id.buttonAddPatient).setOnClickListener { addPatient() }

        //in the second button click
        //opening the activity to display all the artist
        //it will give error as we dont have this activity so remove this part for now to run the app

    }

     //adding a new record to database
    private fun addPatient() {
        //getting the record values
        val date = editTextDate?.text.toString()
        val patientname = editTextPatientName?.text.toString()
        val fathername = editTextFatherName?.text.toString()
        val gender = spinnerGender?.selectedItem.toString()
        val age = editTextAge?.text.toString()
        val agegroup = spinnerAgeGroup?.selectedItem.toString()
        val cnic = editTextCNIC?.text.toString()
        val religion = spinnerReligion?.selectedItem.toString()
        val address = editTextAddress?.text.toString()
        val profession = editTextProfession?.text.toString()
        //creating volley string request
        val stringRequest = object : StringRequest(
            Request.Method.POST, EndPoints.URL_ADD_PATIENT,
            Response.Listener<String> { response ->
                try {
                    val obj = JSONObject(response)
                    Toast.makeText(applicationContext, obj.getString("message"), Toast.LENGTH_LONG)
                        .show()
                    val intent = Intent(applicationContext, GuardianActivity::class.java)
                    startActivity(intent)
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
            },
            object : Response.ErrorListener {
                override fun onErrorResponse(volleyError: VolleyError) {
                    Toast.makeText(applicationContext, volleyError.message, Toast.LENGTH_LONG)
                        .show()
                }
            }) {
            @Throws(AuthFailureError::class)
            override fun getParams(): Map<String, String> {
                val params = HashMap<String, String>()
                params.put("date", date)
                params.put("patientname", patientname)
                params.put("fathername", fathername)
                params.put("gender", gender)
                params.put("age", age)
                params.put("agegroup", agegroup)
                params.put("cnicno", cnic)
                params.put("religion", religion)
                params.put("address", address)
                params.put("profession", profession)
                return params
            }
        }

        //adding request to queue
        VolleySingleton.instance?.addToRequestQueue(stringRequest)
    }
}

Here is Guardian Activity Kotlin

class GuardianActivity : AppCompatActivity() {

    //edittext and spinner
    private var editTextID: EditText? = null
    private var editTextPatientID: EditText? = null
    private var editTextGuardianName: EditText? = null
    private var editTextRelation: EditText? = null
    private var editTextAddress: EditText? = null
    private var editTextCNIC: EditText? = null
    private var editTextContactNo: EditText? = null
    private var editTextProfession: EditText? = null

   override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_guardian)

        //getting it from xml
        editTextID = findViewById<EditText>(R.id.editTextID)
       editTextPatientID = findViewById<EditText>(R.id.editTextPatientID)
       editTextGuardianName = findViewById<EditText>(R.id.editTextGuardianName)
       editTextRelation = findViewById<EditText>(R.id.editTextRelation)
       editTextAddress = findViewById<EditText>(R.id.editTextAddress)
       editTextCNIC = findViewById<EditText>(R.id.editTextCNIC)
       editTextContactNo = findViewById<EditText>(R.id.editTextContactNo)
       editTextProfession = findViewById<EditText>(R.id.editTextProfession)


        //adding a click listener to button
        this.findViewById<Button>(R.id.buttonAddGuardian).setOnClickListener { addGuardian() }

        //in the second button click
        //opening the activity to display all the artist
        //it will give error as we dont have this activity so remove this part for now to run the app

    }

     //adding a new record to database
    private fun addGuardian() {
        //getting the record values
        val patientID = editTextPatientID?.text.toString()
        val guardianname = editTextGuardianName?.text.toString()
        val relation = editTextRelation?.text.toString()
        val address = editTextAddress?.text.toString()
        val cnic = editTextCNIC?.text.toString()
        val contactno = editTextContactNo?.text.toString()
        val profession = editTextProfession?.text.toString()
        //creating volley string request
        val stringRequest = object : StringRequest(
            Request.Method.POST, EndPoints.URL_ADD_GUARDIAN,
            Response.Listener<String> { response ->
                try {
                    val obj = JSONObject(response)
                    Toast.makeText(applicationContext, obj.getString("message"), Toast.LENGTH_LONG)
                        .show()
                    val intent = Intent(applicationContext, MainActivity::class.java)
                    startActivity(intent)
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
            },
            object : Response.ErrorListener {
                override fun onErrorResponse(volleyError: VolleyError) {
                    Toast.makeText(applicationContext, volleyError.message, Toast.LENGTH_LONG)
                        .show()
                }
            }) {
            @Throws(AuthFailureError::class)
            override fun getParams(): Map<String, String> {
                val params = HashMap<String, String>()
                params.put("patientid", patientID)
                params.put("guardianname", guardianname)
                params.put("relation", relation)
                params.put("address", address)
                params.put("cnic", cnic)
                params.put("contactno", contactno)
                params.put("profession", profession)
                return params
            }
        }

        //adding request to queue
        VolleySingleton.instance?.addToRequestQueue(stringRequest)
    }
}

kindly advice me how I get ID from patient form and make it available in guardian form after submission. Thanks



from Recent Questions - Stack Overflow https://ift.tt/37Nb25P
https://ift.tt/eA8V8J

No comments:

Post a Comment