PHP提交按钮检查不起作用

时间:2015-12-02 03:57:11

标签: php sql forms post form-submit

我正在尝试获取一个isset,因此如果我的表单中的sumbit按钮尚未单击,代码将停止。然而,我所做的一切都让它只是认为按钮从未被按下。我可以使用一些帮助。对不起巨大的代码转储。

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();       

 ?>`

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:0)

当您使用任何表单提交来显示下一阶段时,您有两个选项 -

1)您需要重新加载此页面,检查提交的数据和显示下一阶段。

2)或者你需要使用AJAX进行同样的工作,它不会重新加载你的页面,而是满足你的要求。

最好使用AJAX检查是否按下提交按钮,如果按下则设置任何变量(假设为$var1)&amp;使用isset($var1)进行检查,以显示页面的下一部分。