I have a registration script for a simple registration form in Android. Firstly I am using the Volley library for the front end part.
private void registerUser(final String firstName, final String lastName, final String email, String password) {
String tag_json_obj = "json_obj_req";
final ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("posting...");
pDialog.show();
final HashMap<String, String> postParams = new HashMap<String, String>
();
postParams.put("firstName", firstName);
postParams.put("lastName", lastName);
postParams.put("email", email);
postParams.put("password", password);
Response.Listener<JSONObject> listener;
Response.ErrorListener errorListener;
final JSONObject jsonObject = new JSONObject(postParams);
JsonObjectRequest jsonObjReq = new JsonObjectRequest(AppConfig.URL_REGISTER, jsonObject,
new com.android.volley.Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("TAG", response.toString());
try {
//Toast.makeText(getApplicationContext(), response.getString("message"), Toast.LENGTH_LONG).show();
// Toast.makeText(getApplicationContext(), "Thank you for your post", Toast.LENGTH_LONG).show();
if (response.getString("status").equals("fail")) {
//session.setLogin(true);
pDialog.dismiss();
Toast.makeText(getApplicationContext(),response.getString("message"), Toast.LENGTH_SHORT).show();
//startActivity(intent);
//finish();
}else{
Toast.makeText(getApplicationContext(), response.getString("message"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
Log.e("TAG", e.toString());
}
pDialog.dismiss();
}
}, new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//VolleyLog.d("TAG", "Error: " + error.getMessage());
pDialog.dismiss();
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
}
That part works fine as I can get JSON back.
{"status":"fail","message":"Missing POST parameter(s)"}
The PHP script is.
<?php
require 'connect.php';
require 'functions.php';
if(logged_in())
{
header("location:profile.php");
exit();
}
if (isset($_POST['firstName']) && isset($_POST['lastName']) &&
isset($_POST['email']) && isset($_POST['password'])) {
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$email = $_POST['email'];
$password = $_POST['password'];
$stmt = $con->prepare('
INSERT INTO
users
(firstName,lastName,email,password)
VALUES
(?,?,?,?)
');
if ( !$stmt ) {
$result = array('status'=>'fail', 'message'=>'prepare failed');
}
else if ( !$stmt->bind_param('ssss', $firstName, $lastName, $email, $password) ) {
$result = array('status'=>'fail', 'message'=>'bind failed');
}
else if ( !$stmt->execute() ) {
$result = array('status'=>'fail', 'message'=>'execute/insert failed');
}
else {
$result = array('status'=>'success', 'message'=>'Successfully
registered');
}
}
else {
$result = array('status'=>'fail', 'message'=>'Missing POST
parameter(s)');
}
echo json_encode($result);
?>
The problem is that the $_POST variables are not set,and that's why I get that JSON response. I know for html forms for you set post variables like $_POST['name of the input field'],but in my case I don't want that.