2015-12-02 128 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