简体   繁体   English

PHP无法在HTML标签内工作

[英]php not working inside html tags

When I am using php code its working fine but when I am using inside html tags its not working, and i have tried this solution but didn't worked for me. 当我使用php代码时,它可以正常工作,但是当我在html标签中使用时,它不起作用,我已经尝试过解决方案,但对我却没有用。

Additional summary (I am creating android app which connect android with php mysql, using utf8 thats why I have to use html tags which supports (meta http-equiv="Content-Type" content="text/html; charset=utf-8") 其他摘要(我正在使用utf8创建将android与php mysql连接的android应用,这就是为什么我必须使用支持(meta http-equiv =“ Content-Type” content =“ text / html; charset = utf-8 ”)

My Class 我的课

package com.example.androidhive;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.androidhive.R;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

public class NewProductActivity extends Activity implements OnTouchListener, OnClickListener,
OnFocusChangeListener {
    private Button mBSpace, mBdone, mBack, mBChange, mNum, xQ1, xW1, xE1, xQ2, xW2, xE2;
    private RelativeLayout mLayout, mKLayout;
    private boolean isEdit = false, isEdit1 = false, isEdit2 = false;
    private String mUpper = "upper", mLower = "lower";
    private int w, mWindowWidth;
    private String sL[] = { "ا", "ب", "پ", "ت", "ٹ", "ث", "ج", "چ", "ح", "خ",
            "د", "ڈ", "ذ", "ر", "ڑ", "ز", "ژ", "س", "ش", "ص", "ض", "ط", "ظ",
            "ع", "غ", "ف", "ق", "ک", "گ", "ل", "م", "ن" };
    private String cL[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
            "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
            "X", "Y", "Z", "ç", "à", "é", "è", "û", "î" };
    private String nS[] = { "!", ")", "'", "#", "3", "$", "%", "&", "8", "*",
            "?", "/", "+", "-", "9", "0", "1", "4", "@", "5", "7", "(", "2",
            "\"", "6", "_", "=", "]", "[", "<", ">", "|" };
    private Button mB[] = new Button[32];
    // Progress Dialog
    private ProgressDialog pDialog;

    JSONParser jsonParser = new JSONParser();
    EditText inputName;
    EditText inputPrice;
    EditText inputDesc;

    // url to create new product
    private static String url_create_product = "http://192.168.1.4:81/android_connect/create_product.php";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    try {
        setContentView(R.layout.add_product);

        setKeys();
        setFrow();
        setSrow();
        setTrow();
        setForow();

        // Edit Text
        inputName = (EditText) findViewById(R.id.inputName);
        inputPrice = (EditText) findViewById(R.id.inputPrice);
        inputDesc = (EditText) findViewById(R.id.inputDesc);

        inputName.setOnTouchListener(this);
        inputPrice.setOnTouchListener(this);
        inputDesc.setOnTouchListener(this);
        inputName.setOnFocusChangeListener(this);
        inputPrice.setOnFocusChangeListener(this);
        inputDesc.setOnFocusChangeListener(this);

        inputName.setOnClickListener(this);
        inputPrice.setOnClickListener(this);
        inputDesc.setOnClickListener(this);

        mLayout = (RelativeLayout) findViewById(R.id.xK1);
        mKLayout = (RelativeLayout) findViewById(R.id.xKeyBoard);

    } catch (Exception e) {
        Log.w(getClass().getName(), e.toString());
    }
        // Create button
        Button btnCreateProduct = (Button) findViewById(R.id.btnCreateProduct);



        // button click event
        btnCreateProduct.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                // creating new product in background thread
                new CreateNewProduct().execute();
            }
        });

    }

    /**
     * Background Async Task to Create new product
     * */
    class CreateNewProduct extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(NewProductActivity.this);
            pDialog.setMessage("Creating Product..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        /**
         * Creating product
         * */
        protected String doInBackground(String... args) {
            String name = inputName.getText().toString();
            String price = inputPrice.getText().toString();
            String description = inputDesc.getText().toString();

            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("name", name));
            params.add(new BasicNameValuePair("price", price));
            params.add(new BasicNameValuePair("description", description));

            // getting JSON Object
            // Note that create product url accepts POST method
            JSONObject json = jsonParser.makeHttpRequest(url_create_product,
                    "POST", params);

            // check log cat fro response
            Log.d("Create Response", json.toString());

            // check for success tag
            try {
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // successfully created product
                    Intent i = new Intent(getApplicationContext(), AllProductsActivity.class);
                    startActivity(i);

                    // closing this screen
                    finish();
                } else {
                    // failed to create product
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once done
            pDialog.dismiss();
        }

    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        // TODO Auto-generated method stub
        if (v == inputName && hasFocus == true) {
            isEdit = true;
            isEdit1 = false;
            isEdit2 = false;

        } else if (v == inputPrice && hasFocus == true) {
            isEdit = false;
            isEdit1 = true;
            isEdit2 = false;


    } else if (v == inputDesc && hasFocus == true) {
        isEdit = false;
        isEdit1 = false;
        isEdit2 = true;

    }

    }
    private void addText(View v) {
        if (isEdit == true) {
            String b = "";
            b = (String) v.getTag();
            if (b != null) {
                // adding text in Edittext
                inputName.append(b);

            }
        }
        if (isEdit1 == true) {
            String b = "";
            b = (String) v.getTag();
            if (b != null) {
                // adding text in Edittext
                inputPrice.append(b);
            }
            }
            if (isEdit1 == true) {
                String b = "";
                b = (String) v.getTag();
                if (b != null) {
                    // adding text in Edittext
                    inputDesc.append(b);

        }


    }}
    private void isBack(View v) {
        if (isEdit == true) {
            CharSequence cc = inputName.getText();
            if (cc != null && cc.length() > 0) {
                {
                    inputName.setText("");
                    inputName.append(cc.subSequence(0, cc.length() - 1));
                }

            }
        }
        if (isEdit1 == true) {
            CharSequence cc = inputPrice.getText();
            if (cc != null && cc.length() > 0) {
                {
                    inputPrice.setText("");
                    inputDesc.append(cc.subSequence(0, cc.length() - 1));
                }
            }
        } 
    }

    private void changeSmallLetters() {
        mBChange.setVisibility(Button.VISIBLE);
        for (int i = 0; i < sL.length; i++)
            mB[i].setText(sL[i]);
        mNum.setTag("12#");
    }
    private void changeSmallTags() {
        for (int i = 0; i < sL.length; i++)
            mB[i].setTag(sL[i]);
        mBChange.setTag("lower");
        mNum.setTag("num");
    }
    private void changeCapitalLetters() {
        mBChange.setVisibility(Button.VISIBLE);
        for (int i = 0; i < cL.length; i++)
            mB[i].setText(cL[i]);
        mBChange.setTag("upper");
        mNum.setText("12#");

    }

    private void changeCapitalTags() {
        for (int i = 0; i < cL.length; i++)
            mB[i].setTag(cL[i]);
        mNum.setTag("num");

    }

    private void changeSyNuLetters() {

        for (int i = 0; i < nS.length; i++)
            mB[i].setText(nS[i]);
        mNum.setText("ABC");
    }

    private void changeSyNuTags() {
        for (int i = 0; i < nS.length; i++)
            mB[i].setTag(nS[i]);
        mNum.setTag("ABC");
    }
    // enabling customized keyboard
        private void enableKeyboard() {

            mLayout.setVisibility(RelativeLayout.VISIBLE);
            mKLayout.setVisibility(RelativeLayout.VISIBLE);

        }

        // Disable customized keyboard
        private void disableKeyboard() {
            mLayout.setVisibility(RelativeLayout.INVISIBLE);
            mKLayout.setVisibility(RelativeLayout.INVISIBLE);

        }

        private void hideDefaultKeyboard() {
            getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        }

        private void setFrow() {
            w = (mWindowWidth / 13);
            w = w - 15;
            mB[16].setWidth(w);
            mB[22].setWidth(w + 3);
            mB[4].setWidth(w);
            mB[17].setWidth(w);
            mB[19].setWidth(w);
            mB[24].setWidth(w);
            mB[20].setWidth(w);
            mB[8].setWidth(w);
            mB[14].setWidth(w);
            mB[15].setWidth(w);
            mB[16].setHeight(50);
            mB[22].setHeight(50);
            mB[4].setHeight(50);
            mB[17].setHeight(50);
            mB[19].setHeight(50);
            mB[24].setHeight(50);
            mB[20].setHeight(50);
            mB[8].setHeight(50);
            mB[14].setHeight(50);
            mB[15].setHeight(50);

        }

        private void setSrow() {
            w = (mWindowWidth / 10);
            mB[0].setWidth(w);
            mB[18].setWidth(w);
            mB[3].setWidth(w);
            mB[5].setWidth(w);
            mB[6].setWidth(w);
            mB[7].setWidth(w);
            mB[26].setWidth(w);
            mB[9].setWidth(w);
            mB[10].setWidth(w);
            mB[11].setWidth(w);
            mB[26].setWidth(w);

            mB[0].setHeight(50);
            mB[18].setHeight(50);
            mB[3].setHeight(50);
            mB[5].setHeight(50);
            mB[6].setHeight(50);
            mB[7].setHeight(50);
            mB[9].setHeight(50);
            mB[10].setHeight(50);
            mB[11].setHeight(50);
            mB[26].setHeight(50);
        }

        private void setTrow() {
            w = (mWindowWidth / 12);
            mB[25].setWidth(w);
            mB[23].setWidth(w);
            mB[2].setWidth(w);
            mB[21].setWidth(w);
            mB[1].setWidth(w);
            mB[13].setWidth(w);
            mB[12].setWidth(w);
            mB[27].setWidth(w);
            mB[28].setWidth(w);
            mBack.setWidth(w);

            mB[25].setHeight(50);
            mB[23].setHeight(50);
            mB[2].setHeight(50);
            mB[21].setHeight(50);
            mB[1].setHeight(50);
            mB[13].setHeight(50);
            mB[12].setHeight(50);
            mB[27].setHeight(50);
            mB[28].setHeight(50);
            mBack.setHeight(50);

        }

        private void setForow() {
            w = (mWindowWidth / 10);
            mBSpace.setWidth(w * 4);
            mBSpace.setHeight(50);
            mB[29].setWidth(w);
            mB[29].setHeight(50);

            mB[30].setWidth(w);
            mB[30].setHeight(50);

            mB[31].setHeight(50);
            mB[31].setWidth(w);
            mBdone.setWidth(w + (w / 1));
            mBdone.setHeight(50);

        }

        private void setKeys() {
            mWindowWidth = getWindowManager().getDefaultDisplay().getWidth(); // getting
            // window
            // height
            // getting ids from xml files
            mB[0] = (Button) findViewById(R.id.xA);
            mB[1] = (Button) findViewById(R.id.xB);
            mB[2] = (Button) findViewById(R.id.xC);
            mB[3] = (Button) findViewById(R.id.xD);
            mB[4] = (Button) findViewById(R.id.xE);
            mB[5] = (Button) findViewById(R.id.xF);
            mB[6] = (Button) findViewById(R.id.xG);
            mB[7] = (Button) findViewById(R.id.xH);
            mB[8] = (Button) findViewById(R.id.xI);
            mB[9] = (Button) findViewById(R.id.xJ);
            mB[10] = (Button) findViewById(R.id.xK);
            mB[11] = (Button) findViewById(R.id.xL);
            mB[12] = (Button) findViewById(R.id.xM);
            mB[13] = (Button) findViewById(R.id.xN);
            mB[14] = (Button) findViewById(R.id.xO);
            mB[15] = (Button) findViewById(R.id.xP);
            mB[16] = (Button) findViewById(R.id.xQ);
            mB[17] = (Button) findViewById(R.id.xR);
            mB[18] = (Button) findViewById(R.id.xS);
            mB[19] = (Button) findViewById(R.id.xT);
            mB[20] = (Button) findViewById(R.id.xU);
            mB[21] = (Button) findViewById(R.id.xV);
            mB[22] = (Button) findViewById(R.id.xW);
            mB[23] = (Button) findViewById(R.id.xX);
            mB[24] = (Button) findViewById(R.id.xY);
            mB[25] = (Button) findViewById(R.id.xZ);
            mB[26] = (Button) findViewById(R.id.xS1);
            mB[27] = (Button) findViewById(R.id.xS2);
            mB[28] = (Button) findViewById(R.id.xS3);
            mB[29] = (Button) findViewById(R.id.xS4);
            mB[30] = (Button) findViewById(R.id.xS5);
            mB[31] = (Button) findViewById(R.id.xS6);
            xQ1 = (Button) findViewById(R.id.xQ1);
            xQ2 = (Button) findViewById(R.id.xQ2);
            xE1 = (Button) findViewById(R.id.xE1);
            xE2 = (Button) findViewById(R.id.xE2);
            xW1 = (Button) findViewById(R.id.xW1);
            xW2 = (Button) findViewById(R.id.xW2);
            mBSpace = (Button) findViewById(R.id.xSpace);
            mBdone = (Button) findViewById(R.id.xDone);
            mBChange = (Button) findViewById(R.id.xChange);
            mBack = (Button) findViewById(R.id.xBack);
            mNum = (Button) findViewById(R.id.xNum);
            for (int i = 0; i < mB.length; i++)
                mB[i].setOnClickListener(this);
            xQ1.setOnClickListener(this);
            xQ2.setOnClickListener(this);
            xE1.setOnClickListener(this);
            xE2.setOnClickListener(this);
            xW1.setOnClickListener(this);
            xW2.setOnClickListener(this);
            mBSpace.setOnClickListener(this);
            mBdone.setOnClickListener(this);
            mBack.setOnClickListener(this);
            mBChange.setOnClickListener(this);
            mNum.setOnClickListener(this);


        }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        if (v == mBChange) {

            if (mBChange.getTag().equals(mUpper)) {
                changeSmallLetters();
                changeSmallTags();
                xQ1.setVisibility(View.VISIBLE);
                xQ2.setVisibility(View.VISIBLE);
                xW1.setVisibility(View.VISIBLE);
                xW2.setVisibility(View.VISIBLE);
                xE1.setVisibility(View.VISIBLE);
                xE2.setVisibility(View.VISIBLE);

            } else if (mBChange.getTag().equals(mLower)) {
                changeCapitalLetters();
                changeCapitalTags();
                xQ1.setVisibility(View.GONE);
                xQ2.setVisibility(View.GONE);
                xW1.setVisibility(View.GONE);
                xW2.setVisibility(View.GONE);
                xE1.setVisibility(View.GONE);
                xE2.setVisibility(View.GONE);

            }

        } else if (v != mBdone && v != mBack && v != mBChange && v != mNum) {
            addText(v);

        } else if (v == mBdone) {
            disableKeyboard();


        } else if (v == mBack) {
            isBack(v);
        } else if (v == mNum) {
            String nTag = (String) mNum.getTag();
            if (nTag.equals("num")) {
                changeSyNuLetters();
                changeSyNuTags();
                mBChange.setVisibility(Button.INVISIBLE);

                xQ1.setVisibility(View.GONE);
                xQ2.setVisibility(View.GONE);
                xW1.setVisibility(View.GONE);
                xW2.setVisibility(View.GONE);
                xE1.setVisibility(View.GONE);
                xE2.setVisibility(View.GONE);

            }
            if (nTag.equals("ABC")) {
                changeCapitalLetters();
                changeCapitalTags();

                xQ1.setVisibility(View.GONE);
                xQ2.setVisibility(View.GONE);
                xW1.setVisibility(View.GONE);
                xW2.setVisibility(View.GONE);
                xE1.setVisibility(View.GONE);
                xE2.setVisibility(View.GONE);
            }

        }

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub

        if (v == inputName) {
            hideDefaultKeyboard();
            enableKeyboard();

        }

        if (v == inputPrice) {
            hideDefaultKeyboard();
            enableKeyboard();

        }

        if (v == inputDesc) {
            hideDefaultKeyboard();
            enableKeyboard();

        }

        return true;
    }
}

<-----------------------------> <----------------------------->

without html tags working fine with android 没有HTML标签可以在Android上正常工作

 <?php

    /*
     * Following code will create a new product row
     * All product details are read from HTTP Post Request
     */

    // array for JSON response
    $response = array();

    // check for required fields
    if (isset($_POST['name']) && isset($_POST['price']) && isset($_POST['description'])) {

        $name = $_POST['name'];
        $price = $_POST['price'];
        $description = $_POST['description'];

        // include db connect class
        require_once __DIR__ . '/db_connect.php';

        // connecting to db
        $db = new DB_CONNECT();

        // mysql inserting a new row
        $result = mysql_query("INSERT INTO products(name, price, description) VALUES('$name', '$price', '$description')");

        // check if row inserted or not
        if ($result) {
            // successfully inserted into database
            $response["success"] = 1;
            $response["message"] = "Product successfully created.";

            // echoing JSON response
            echo json_encode($response);
        } else {
            // failed to insert row
            $response["success"] = 0;
            $response["message"] = "Oops! An error occurred.";

            // echoing JSON response
            echo json_encode($response);
        }
    } else {
        // required field is missing
        $response["success"] = 0;
        $response["message"] = "Required field(s) is missing";

        // echoing JSON response
        echo json_encode($response);
    }
    ?>

inside Html tags HTML标签内

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> Create Product </title>
</head>
<body>
<?php

/*
 * Following code will create a new product row
 * All product details are read from HTTP Post Request
 */

// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['name']) && isset($_POST['price']) && isset($_POST['description'])) {

    $name = $_POST['name'];
    $price = $_POST['price'];
    $description = $_POST['description'];

    // include db connect class
    require_once __DIR__ . '/db_connect.php';

    // connecting to db
    $db = new DB_CONNECT();

    // mysql inserting a new row
    $result = mysql_query("INSERT INTO products(name, price, description) VALUES('$name', '$price', '$description')");

    // check if row inserted or not
    if ($result) {
        // successfully inserted into database
        $response["success"] = 1;
        $response["message"] = "Product successfully created.";

        // echoing JSON response
        echo json_encode($response);
    } else {
        // failed to insert row
        $response["success"] = 0;
        $response["message"] = "Oops! An error occurred.";

        // echoing JSON response
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>
</body>
</html>

My Log Cat https://www.dropbox.com/s/b3p8kifm0kpzjsg/Untitled2.png?dl=0 我的日志猫 https://www.dropbox.com/s/b3p8kifm0kpzjsg/Untitled2.png?dl=0

04-13 17:00:46.512: E/WindowManager(24821): Activity com.example.androidhive.NewProductActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41782898 that was originally added here

04-13 17:00:46.512: E/WindowManager(24821): android.view.WindowLeaked: Activity com.example.androidhive.NewProductActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41782898 that was originally added here

04-13 17:00:46.512: E/WindowManager(24821):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:344)

04-13 17:00:46.512: E/WindowManager(24821):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267)

04-13 17:00:46.512: E/WindowManager(24821):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)

04-13 17:00:46.512: E/WindowManager(24821):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)

04-13 17:00:46.512: E/WindowManager(24821):     at android.view.Window$LocalWindowManager.addView(Window.java:537)

use this at the top of your HTML 在HTML顶部使用它

<!doctype html>
<html>
<head>
<meta charset="utf-8">

Sunil表示文件必须具有扩展名.php

We cannot see your code but i think the following happens: The php only code delivers a json text. 我们看不到您的代码,但我认为会发生以下情况:仅php代码可提供json文本。 That is what the json parser expects as reply. 这就是json解析器期望的答复。 But now you send a html page. 但是现在您发送了一个html页面。 So the json parser will deliver null. 因此,json解析器将传递null。 You are not checking for null so your app will crash with a NullPointerException . 您没有检查null因此您的应用程序将崩溃,并null NullPointerException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM