2015-12-02 165 views
0

我想要得到一個isset,因此如果我的表單中的sumbit按鈕沒有被點擊,代碼就會停止。然而,我所做的每件事都會讓它認爲按鈕從不按下。我可以使用一些幫助。對不起,巨大的代碼轉儲。PHP提交按鈕檢查不工作

if(!$ gn === null)是我試圖讓它工作的可悲嘗試。

謝謝

<!--*** Form Start ***-->  
<form method="post" name="Lab4Form" id="Lab4Form" 
     action="genre_aries0653.php"> 
    <fieldset> 
     <legend>Select by Genre</legend> 

     <!-- Genre --> 
     <label for="genre">Genre: </label> 
     <input type="text" name="genre" id="genre" size="50" 
          maxlength="35" placeholder= "Freedom" 
          required="required"><br><br> 

     <!--Submit/Reset Buttons -->  
     <input type="submit" value="Submit" id="submit"> 
     <input type="reset"> 
    </fieldset> 
</form> 



<?php 
//require_once functions 
require_once ("inc_functions_aries0653.php"); 
//require_once connect to db, selects db 
require_once("conn_aries0653.php"); 



$gn = (filter_input(INPUT_POST, 'genre')); 
$genre  = (ucwords(strtolower(trim($gn)))); 



if (!$gn===null) { 

} else { 
    die; 
} 



MultiCheck ($genre); 



//Checks if connected to database successfully 
$dbConnection = new mysqli($sn, $un, $pw, $dbName); 

if ($dbConnection->connect_error) 
{ 
die ("Connection Failed: ".$dbConnection->connect_error); 
} 


//Declares the sort and SQL string 
$sort = "author_lastName"; 
//SQL: Select all from table, where genre = user input genre, sort 
$sqlQuery = 
"SELECT * FROM $tableName WHERE genre= '$genre' ORDER BY $sort"; 

//run query 
$result = $dbConnection->query($sqlQuery); 


//check if there is any data 
if ($result-> num_rows > 0) 
{ 
//Prints the table head which are the fields of the table 
$totalRecords = $result-> num_rows; 
print("<table border=\"1\"<tr><header><h1>Book Inventory</h1></header></tr>". 
     "<tr><th>Book Title</th>". 
     "<th>Author's First Name</th>". 
     "<th>Author's Last Name</th>". 
     "<th>Genre</th>". 
     "<th>ISBN13</th>". 
     "<th>Publisher</th>". 
     "<th>Copyright Year</th>". 
     "<th>Price</th></tr>"); 

while($record = $result->fetch_assoc()) 
{ 

//Loops through the table to retrieve all the records of the given fields 
    print("<tr><td>{$record['title']}</td>"); 
    print("<td>{$record['author_firstName']}</td>"); 
    print("<td>{$record['author_lastName']}</td>"); 
    print("<td>{$record['genre']}</td>"); 
    print("<td>{$record['ISBN']}</td>"); 
    print("<td>{$record['publisher']}</td>"); 
    print("<td>{$record['yearPublished']}</td>"); 
    //format price to have 2 decinmal places and a "$" sign 
    print("<td class=\"number\">"."$".number_format("{$record['price']}",2)."</td></tr>\n"); 

} 
    //Tell user how many records are returned 
    print("<tr><td colspan=8 class=message>Your query returned ". 
      $totalRecords." books.</td></tr>"); 
    print("</table>\n"); 
} else { 
     print("No books for you!"); 
     die; 
    } 


//Close db connection 
$dbConnection -> close();  

?>` 
+0

Multicheck是我用來過濾輸入的正則表達式更多 – thelaughingman

回答

1

可能需要此

<input type="submit" name="btnSubmit" value="Submit" id="submit"> 

然後檢查按鈕被點擊

if(isset($_POST['btnSubmit'])){ 
//Your code here if the button is clicked 
}else{ 
//The button is not clicked 
} 
+0

謝謝!我不知道我必須在按鈕上添加一個名稱才能起作用。 – thelaughingman

+0

我很高興它可以幫助你:D –

0

當你使用任何形式提交用於顯示下一階段,你有兩個選擇 -

1)您需要重新載入此頁面,查看提交的數據&顯示下一個階段。 2)或者您需要使用AJAX進行相同的工作,它不會重新加載您的頁面,但會滿足您的要求。

這是更好地使用AJAX來檢查是否提交按鈕按下與否,只要按下然後設置任何變量(假設$var1)&使用isset($var1)用於顯示網頁的下一部分檢查它。

+0

謝謝你!我剛開始編寫幾個月前,我沒有學到與AJAX有關的任何東西,但只是PHP。未來的病態筆記 – thelaughingman