在php中从按钮到isset post获取id
在我的应用程序中,我有一些按钮,当我点击时,它会将数据保存到mysql数据库中,并且工作正常。 问题是,我有10-15个页面,其中我需要保存一些数据,所以我想使用一个,而不是10-15个php页面,将数据保存在mysql中 这就是成功的原因 我的纽扣在php中从按钮到isset post获取id,php,android,Php,Android,在我的应用程序中,我有一些按钮,当我点击时,它会将数据保存到mysql数据库中,并且工作正常。 问题是,我有10-15个页面,其中我需要保存一些数据,所以我想使用一个,而不是10-15个php页面,将数据保存在mysql中 这就是成功的原因 我的纽扣 mSubmit = (Button)findViewById(R.id.submit); mSubmit.setOnClickListener(this); 还有我的php if (!empty($_POST)) { //init
mSubmit = (Button)findViewById(R.id.submit);
mSubmit.setOnClickListener(this);
还有我的php
if (!empty($_POST)) {
//initial query
$query = "INSERT INTO comments ( username, title, message ) VALUES ( :user, :title, :message ) ";
//Update query
$query_params = array(
':user' => $_POST['username'],
':title' => $_POST['title'],
':message' => $_POST['message']
);
我的java
package overskov.rhkbrand;
@SuppressLint("SimpleDateFormat")
public class AddComment extends Fragment {
private EditText lys, skilt, door, etDate;
private Button mSubmit;
// Progress Dialog
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
//php login script
//localhost :
//testing on your device
//put your local ip instead, on windows, run CMD > ipconfig
//or in mac's terminal type ifconfig and look for the ip under en0 or en1
// private static final String POST_COMMENT_URL = "http://xxx.xxx.x.x:1234/webservice/addcomment.php";
//testing on Emulator:
private static final String POST_COMMENT_URL = "http://overskov-hansen.dk/addcomment.php";
//testing from a real server:
//private static final String POST_COMMENT_URL = "http://www.mybringback.com/webservice/addcomment.php";
//ids
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.opgang_a, container, false);
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//setContentView(R.layout.add_comment);
lys =(EditText) rootView.findViewById(R.id.lys);
skilt = (EditText)rootView.findViewById(R.id.skilt);
door = (EditText)rootView.findViewById(R.id.door);
etDate = (EditText)rootView.findViewById(R.id.etDate);
//Calendar cal = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
try {
etDate.setText(format.format(new Date()));
} catch (ParseException e) {
e.printStackTrace();
}
mSubmit = (Button)rootView.findViewById(R.id.under_a);
mSubmit.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
PostComment mTask = new PostComment(getActivity());
mTask.execute();
//new PostComment().execute();
}
});
return rootView;
}
//////////////////////
class PostComment extends AsyncTask<String, String, String> {
private Context context;
ProgressDialog prog;
public PostComment(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(context);
pDialog.setMessage("Posting Comment...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;
String post_lys = lys.getText().toString();
String post_skilt = skilt.getText().toString();
String post_door = door.getText().toString();
String post_etDate = etDate.getText().toString();
//We need to change this:
String post_username = "admin";
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", post_username));
params.add(new BasicNameValuePair("lys", post_lys));
params.add(new BasicNameValuePair("skilt", post_skilt));
params.add(new BasicNameValuePair("door", post_door));
params.add(new BasicNameValuePair("etDate", post_etDate));
Log.d("request!", "starting");
//Posting user data to script
JSONObject json;
json = jsonParser.makeHttpRequest(
POST_COMMENT_URL, "POST", params);
// full json response
Log.d("Post Comment attempt", json.toString());
// json success element
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Comment Added!", json.toString());
//finish();
return json.getString(TAG_MESSAGE);
}else{
Log.d("Comment Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url != null){
Toast.makeText(getActivity(), file_url, Toast.LENGTH_LONG).show();
}
}
}
02-20 20:51:09.322:W/dalvikvm(19779):threadid=13:线程退出时出现未捕获异常(组=0x41ad2700)
02-20 20:51:09.332:E/AndroidRuntime(19779):致命异常:AsyncTask#1
02-20 20:51:09.332:E/AndroidRuntime(19779):java.lang.RuntimeException:执行doInBackground()时出错
02-20 20:51:09.332:E/AndroidRuntime(19779):在android.os.AsyncTask$3.done(AsyncTask.java:299)
02-20 20:51:09.332:E/AndroidRuntime(19779):在java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)我想你应该确保你的按钮有如下名称参数
<input type="button" id="under_b" name="under_b"/>
否则它将不会传递给POST。请显示更多代码,以便我们可以查看变量的来源。另外,$\u POST['name of item']不是item的id。不介意POST名称在php中有changeya,这是可行的,但在我的应用程序中,我的按钮?在按钮标记的xml中,是否可以使用类似于android:name的想法来设置name属性?只需尝试android:name=“under\u a”但仍然相同
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#000000"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="150dp"
android:layout_height="35dp"
android:background="#000000"
android:paddingLeft="10dp"
android:text="@string/opgang_a"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<EditText
android:id="@+id/etDate"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/edit_text_design"
android:clickable="false"
android:cursorVisible="false"
android:ems="15"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center_vertical|right"
android:inputType="date"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="30dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical" >
<TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:background="#000000"
android:text="@string/lys" />
<EditText
android:id="@+id/lys"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
android:text="@string/ok"
android:textSize="14sp"
android:inputType="text"/>
<TextView
android:id="@+id/textView3"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:layout_marginTop="20dp"
android:background="#000000"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="@string/skilte"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<EditText
android:id="@+id/skilt"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
android:text="@string/ok"
android:textSize="14sp"
android:inputType="text" />
<TextView
android:id="@+id/textView4"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:layout_marginTop="20dp"
android:background="#000000"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="@string/door"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<EditText
android:id="@+id/door"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
android:inputType="text"
android:text="@string/ok"
android:textSize="14sp" >
<requestFocus />
</EditText>
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/under_a"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#000000"
android:text="@string/gem_data"
android:textColor="#FFFFFF" />
</RelativeLayout>
02-20 20:51:09.317: E/JSON Parser(19779): Error parsing data org.json.JSONException: End of input at character 2 of
<input type="button" id="under_b" name="under_b"/>