PHP提交的提交表单没有返回任何值

时间:2017-03-09 07:02:54

标签: php html forms post isset

通过在互联网上的一些例子,我设法制作一个表格来插入用户。

我使用了isset作为提交函数。当表单完全填写并提交时,结果将回显以显示状态消息。遗憾的是,该表单根本不起作用,单击“提交”按钮时,表单将被重置。

由于我使用存储过程,因此将根据表单中的输入值自动调用ReturnStatusReturnMessage

这是代码。

<?php
include "config.php";
if(isset($_POST['sub']))
{

ini_set('error_reporting', E_ALL);

error_reporting(-1);

$RegType="D";
$UserId=$_POST["UserId"];
$UserPwd=$_POST["UserPwd"];
$UserNm=$_POST["UserNm"];

$stmt=odbc_exec($conn,"CALL UserInsert ('".$RegType."','UserId','UserPwd','UserNm')");

if (!$stmt) {
"Error : " . odbc_errormsg();
}

if (odbc_fetch_row($stmt)) {
$ReturnStatus=odbc_result($stmt,'ReturnStatus');
$ReturnMessage=odbc_result($stmt,'ReturnMessage');
}

if($ReturnStatus==1) {
echo $ReturnMessage=odbc_result($stmt,'ReturnMessage');
}        
}  
?>

<table>
<form class="form"  method="post">

<tr>
<td class = "nama">Nama Pengguna<span class="required">&nbsp; * &nbsp;</span></td>
<td><input type="text" name="UserNm" value=""></td>
</tr>

<tr>
<td class = "userid">Id Pengguna<span class="required">&nbsp; * &nbsp;</span></td>
<td><input type="text" name="UserId" value=""></td>
</tr>

<tr>
<td class = "password">Kata Laluan<span class="required">&nbsp; * &nbsp;</span></td>
<td><input type="password" name="UserPwd" value=""></td>
</tr>

<tr><td>
<input type="submit" value="Save">
</td></tr>
</table> 

我确定我错过了什么。请指导我。谢谢。

已更新

修正了以下问题:

- Fix the name of the button
- PHP error reporting
- Isset

6 个答案:

答案 0 :(得分:3)

几条重要建议:

首先 代码中的所有启用PHP error reporting,这有助于您找到实际错误:

// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);

// Report all PHP errors
error_reporting(-1);

第二次 ,不知道为什么在<form></form>检查中使用isset(),这将在页面上不返回任何内容刷新。

第三 ,非常重要,正如答案中提到的每个人一样,您在按钮输入中缺少名称属性。

<input type="submit" value="Simpan" name="submit">  

第四 ,假设您修复此错误,则会在$ReturnStatus的失败案例中获得未定义的变量。您必须在顶级声明中定义为0,因为如果odbc_fetch_row($stmt)失败,则会返回Undefined Variable Warnings

答案 1 :(得分:1)

检查以下一行:

 public class MainActivity extends AppCompatActivity {
SimpleCursorAdapter mAdapter;
MatrixCursor mMatrixCursor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // The contacts from the contacts content provider is stored in this cursor
    mMatrixCursor = new MatrixCursor(new String[] { "_id","name","photo","details"} );

    // Adapter to set data in the listview
    mAdapter = new SimpleCursorAdapter(getBaseContext(),
            R.layout.lv_layout,
            null,
            new String[] { "name","photo","details"},
            new int[] { R.id.tv_name,R.id.iv_photo,R.id.tv_details}, 0);

    // Getting reference to listview
    ListView lstContacts = (ListView) findViewById(R.id.lst_contacts);

    // Setting the adapter to listview
    lstContacts.setAdapter(mAdapter);

    // Creating an AsyncTask object to retrieve and load listview with contacts
    ListViewContactsLoader listViewContactsLoader = new ListViewContactsLoader();

    // Starting the AsyncTask process to retrieve and load listview with contacts
    listViewContactsLoader.execute();
}

/** An AsyncTask class to retrieve and load listview with contacts */
private class ListViewContactsLoader extends AsyncTask<Void, Void, Cursor> {

    @Override
    protected Cursor doInBackground(Void...  params) {
        Uri contactsUri = ContactsContract.Contacts.CONTENT_URI;

        // Querying the table ContactsContract.Contacts to retrieve all the contacts
        Cursor contactsCursor = getContentResolver().query(contactsUri, null, null, null,ContactsContract.Contacts.DISPLAY_NAME + " ASC ");

        if(contactsCursor.moveToFirst()){
            do{
                long contactId = contactsCursor.getLong(contactsCursor.getColumnIndex("_ID"));

                Uri dataUri = ContactsContract.Data.CONTENT_URI;

                // Querying the table ContactsContract.Data to retrieve individual items like
                // home phone, mobile phone, work email etc corresponding to each contact
                Cursor dataCursor = getContentResolver().query(dataUri, null,
                        ContactsContract.Data.CONTACT_ID + "=" + contactId,
                        null, null);

                String displayName="";
                String nickName="";
                String homePhone="";
                String mobilePhone="";
                String workPhone="";
                //String photoPath="" + R.drawable.blank;
                byte[] photoByte=null;
                String homeEmail="";
                String workEmail="";
                String companyName="";
                String title="";

                if(dataCursor.moveToFirst()){
                    // Getting Display Name
                    displayName = dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME ));
                    do{

                        // Getting NickName
                        if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE))
                            nickName = dataCursor.getString(dataCursor.getColumnIndex("data1"));

                        // Getting Phone numbers
                        if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)){
                            switch(dataCursor.getInt(dataCursor.getColumnIndex("data2"))){
                                case ContactsContract.CommonDataKinds.Phone.TYPE_HOME :
                                    homePhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
                                    break;
                                case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE :
                                    mobilePhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
                                    break;
                                case ContactsContract.CommonDataKinds.Phone.TYPE_WORK :
                                    workPhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
                                    break;
                            }
                        }

                        // Getting EMails
                        if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE ) ) {
                            switch(dataCursor.getInt(dataCursor.getColumnIndex("data2"))){
                                case ContactsContract.CommonDataKinds.Email.TYPE_HOME :
                                    homeEmail = dataCursor.getString(dataCursor.getColumnIndex("data1"));
                                    break;
                                case ContactsContract.CommonDataKinds.Email.TYPE_WORK :
                                    workEmail = dataCursor.getString(dataCursor.getColumnIndex("data1"));
                                    break;
                            }
                        }

                        // Getting Organization details
                        if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)){
                            companyName = dataCursor.getString(dataCursor.getColumnIndex("data1"));
                            title = dataCursor.getString(dataCursor.getColumnIndex("data4"));
                        }

                        // Getting Photo
                        if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)){
                            photoByte = dataCursor.getBlob(dataCursor.getColumnIndex("data15"));

                            if(photoByte != null) {
                                Bitmap bitmap = BitmapFactory.decodeByteArray(photoByte, 0, photoByte.length);

                                // Getting Caching directory
                                File cacheDirectory = getBaseContext().getCacheDir();

                                // Temporary file to store the contact image
                                File tmpFile = new File(cacheDirectory.getPath() + "/wpta_"+contactId+".png");

                                // The FileOutputStream to the temporary file
                                try {
                                    FileOutputStream fOutStream = new FileOutputStream(tmpFile);

                                    // Writing the bitmap to the temporary file as png file
                                    bitmap.compress(Bitmap.CompressFormat.PNG,100, fOutStream);

                                    // Flush the FileOutputStream
                                    fOutStream.flush();

                                    //Close the FileOutputStream
                                    fOutStream.close();

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                               // photoPath = tmpFile.getPath();
                            }
                        }
                    }while(dataCursor.moveToNext());
                    String details = "";

                    // Concatenating various information to single string
                    if(homePhone != null && !homePhone.equals("") )
                        details = "HomePhone : " + homePhone + "\n";
                    if(mobilePhone != null && !mobilePhone.equals("") )
                        details += "MobilePhone : " + mobilePhone + "\n";
                    if(workPhone != null && !workPhone.equals("") )
                        details += "WorkPhone : " + workPhone + "\n";
                    if(nickName != null && !nickName.equals("") )
                        details += "NickName : " + nickName + "\n";
                    if(homeEmail != null && !homeEmail.equals("") )
                        details += "HomeEmail : " + homeEmail + "\n";
                    if(workEmail != null && !workEmail.equals("") )
                        details += "WorkEmail : " + workEmail + "\n";
                    if(companyName != null && !companyName.equals("") )
                        details += "CompanyName : " + companyName + "\n";
                    if(title != null && !title.equals("") )
                        details += "Title : " + title + "\n";

                    // Adding id, display name, path to photo and other details to cursor
                    mMatrixCursor.addRow(new Object[]{ Long.toString(contactId),displayName,null,details});
                }
            }while(contactsCursor.moveToNext());
        }
        return mMatrixCursor;
    }

    @Override
    protected void onPostExecute(Cursor result) {
        // Setting the cursor containing contacts to listview
        mAdapter.swapCursor(result);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

并且您正尝试通过以下方式访问它:

<input type="submit" value="Simpan">

在这里你忘了在输入中添加if(!isset($_POST['submit'])) ,所以请按照:

name="submit"

解释&#34; <input type="submit" value="Simpan" name="submit"> 此处$_POST['index']是代码的名称

答案 2 :(得分:0)

请更改

<input type="submit" value="Simpan"> // you are not added name attribute here

<input type="submit" value="Simpan" name="submit">  

答案 3 :(得分:0)

如果您在name类型input中添加submit属性,那么我希望您的代码能够正常运行。 所以在html编辑:

<input type="submit" value="Simpan">

作为

<input type="submit" name='submit' value="Simpan">

答案 4 :(得分:0)

我修正了一些错误并且工作正常

<table>
<form class="form"  method="post">

<tr><td class = "nama">Name : <span class="required">&nbsp; * &nbsp;</span></td>
<td><input type="text" name="UserNm" value=""></td></tr>

<tr><td class = "userid">Id : <span class="required">&nbsp; * &nbsp;</span></td>
<td><input type="text" name="UserID" value=""></td></tr>

<tr><td>
<input type="submit" value="Simpan" name="submit">
</td></tr>

</form>
</table>
<?php 
if(isset($_POST['submit'])){
$RegType="D";
$UserId=$_POST["UserID"];
$UserNm=$_POST["UserNm"];
echo $UserId;
echo $UserNm;
$stmt=odbc_exec($conn,"CALL UserInsert ('".$RegType."','UserId','UserNm')");

if (!$stmt) {
"Error : " . odbc_errormsg();
}

if (odbc_fetch_row($stmt)) {
$ReturnStatus=odbc_result($stmt,'ReturnStatus');
$ReturnMessage=odbc_result($stmt,'ReturnMessage');
}

if($ReturnStatus==1) {
echo $ReturnMessage=odbc_result($stmt,'ReturnMessage');
} 

}   
?>

答案 5 :(得分:0)

在提交按钮上使用name =“submit”

然后可以访问邮政车