Android - 登入系統:忘記密碼查詢
簡單實做忘記密碼的查詢
假設需要名字、信箱、跟電話三種資訊才能完成查詢
Android端
public class forgotpw extends AppCompatActivity { private EditText editTextName; private EditText editTextEmail; private EditText editTextTel; private Button buttonget; private static final String REGISTER_URL = "http://網址/forgot.php"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_forgotpw); editTextName = (EditText) findViewById(R.id.name); editTextEmail = (EditText) findViewById(R.id.email); editTextTel= (EditText) findViewById(R.id.tel); buttonget = (Button) findViewById(R.id.get); buttonget.setOnClickListener(buttonListener); } private Button.OnClickListener buttonListener = new Button.OnClickListener() {/**監聽查詢鈕是否被按下**/ @Override public void onClick(View v) { if(v == buttonget){ getpw();/**呼叫這函式進行使用者資料獲取**/ } } }; private void getpw() {/**讀取使用者輸入數據**/ String name = editTextName.getText().toString().trim().toLowerCase(); String email = editTextEmail.getText().toString().trim().toLowerCase(); String tel = editTextTel.getText().toString().trim().toLowerCase(); get(name,email,tel);/**獲取資料成功後,開始進行傳送**/ } private void get(String name,String email, String tel) { class RegisterUser extends AsyncTask<String, Void, String> { Createmem ruc = new Createmem();/**使用Creatmem.class的功能(傳送用的httpclass)**/ @Override protected void onPreExecute() { super.onPreExecute();/**當按下查詢鈕,出現提式窗**/ } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show(); } @Override protected String doInBackground(String... params)/**將資料放入hashmap**/ { HashMap<String, String> data = new HashMap<String,String>(); data.put("name",params[0]); data.put("email",params[1]); data.put("tel",params[2]); String result = ruc.sendPostRequest(REGISTER_URL,data); return result; } } RegisterUser ru = new RegisterUser();/**傳送資料**/ ru.execute(name,email, tel); } }Android介面布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:background="@drawable/login_bg" tools:context="project.rmotex.achat.forgotpw"> <ImageView android:id="@+id/image" android:background="@drawable/forgot" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="60dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> <LinearLayout android:orientation="vertical" android:id="@+id/input" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="28.0dip" android:layout_marginRight="28.0dip" android:paddingTop="12.0dip" android:layout_below="@+id/image"> <EditText android:textSize="16.0sp" android:textColor="#E0E0E0" android:textColorHint="#E0E0E0" android:id="@+id/name" android:background="#40000000" android:paddingLeft="12.0dip" android:layout_width="fill_parent" android:layout_height="44dp" android:maxLines="1" android:maxLength="20" android:hint="Account" android:inputType="text"/> <View android:background="#ffc0c3c4" android:layout_width="fill_parent" android:layout_height="1.0px" android:layout_marginLeft="1.0px" android:layout_marginRight="1.0px" /> <EditText android:textSize="16.0sp" android:textColor="#E0E0E0" android:textColorHint="#E0E0E0" android:hint="Email" android:gravity="center_vertical" android:id="@+id/email" android:background="#40000000" android:paddingLeft="12.0dip" android:layout_width="fill_parent" android:layout_height="44dp" android:maxLines="1" android:maxLength="30" android:inputType="textEmailAddress" /> <View android:background="#ffc0c3c4" android:layout_width="fill_parent" android:layout_height="1.0px" android:layout_marginLeft="1.0px" android:layout_marginRight="1.0px" /> <EditText android:textSize="16.0sp" android:textColor="#E0E0E0" android:textColorHint="#E0E0E0" android:hint="Telephone" android:gravity="center_vertical" android:id="@+id/tel" android:background="#40000000" android:paddingLeft="12.0dip" android:layout_width="fill_parent" android:layout_height="44dp" android:maxLines="1" android:maxLength="10" android:inputType="phone" /> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:background="#40000000" android:text="Get Back" android:textStyle="bold" android:id="@+id/get" android:paddingTop="5.0dip" android:layout_marginLeft="12.0dip" android:layout_marginTop="12.0dip" android:layout_marginRight="12.0dip" android:textSize="20.0sp" /> </LinearLayout> </RelativeLayout>Server端忘記密碼PHP
<?php /**用於使用者查詢忘記的密碼**/ if($_SERVER['REQUEST_METHOD']=='POST'){//限制條件為POST $name = $_POST['name'];//將使用者傳的資料存進變數 $email = $_POST['email']; $tel = $_POST['tel']; if($name == '' || $email == '' || $tel == ''){ echo '請填滿所有選項!'; } else { require_once('dbConnect.php'); $sql = "SELECT * FROM userinfo WHERE name='$name' AND email='$email' AND tel='$tel'"; $check = mysqli_fetch_array(mysqli_query($con,$sql)); if(isset($check)) { $sqll ="SELECT password FROM userinfo WHERE name='$name' AND email='$email' AND tel='$tel'"; $result = $con->query($sqll); if($result->num_rows >0) { while($row = $result->fetch_assoc()) { echo $row['password']; } } } else { echo '請再次檢查輸入資訊!'; } mysqli_close($con); } } else { echo 'Error'; } ?>
留言
張貼留言