I am sending data from android app to PHP server and i have got the errors:
Notice: Undefined index: name in E:\xampp\htdocs\server.php on line 4
Notice: Undefined index: email in E:\xampp\htdocs\server.php on line 5
I have taken two edit text and data enter in the edit text should be sent to the server while the button is clicked. So I have made a method for this purpose and I call it in the button on clicked listener
I don't know why I am not receiving data in server there is no problem in my code Please help me!
My client code is
public class MainActivity extends AppCompatActivity {
EditText ed_name, ed_email;
Button btn;
String url = "http://xxxxxxx/server.php";//hidden
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed_name=(EditText)findViewById(R.id.editText);
ed_email=(EditText)findViewById(R.id.editText2);
btn=(Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
insert();
}
});
}
public void insert() {
StringRequest MyRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(getApplication(), response, Toast.LENGTH_SHORT).show();
//Toast.makeText(MainActivity.this,"There is some error on server side",Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error + "", Toast.LENGTH_LONG).show();
}
}) {
protected Map<String, String> getparams() throws AuthFailureError {
Map<String, String> mydata = new HashMap<String, String>();
String name=ed_name.getText().toString();
String email=ed_email.getText().toString();
mydata.put("name", name);
mydata.put("email", email);
return mydata;
}
};
RequestQueue MyRequestQueue = Volley.newRequestQueue(this);
MyRequestQueue.add(MyRequest);
}
}
While Server Side Code is:
<?php
$name = $_POST['name'];//i am retrieving data where ['name]' is key
$email = $_POST['email'];//Here ['email '] is the key i have given in android code
echo $name;
echo $email;
?>