Using if(isset($_POST['submit'])) to not display echo when script is open is not working

帅比萌擦擦* 提交于 2019-11-26 09:55:17

问题


I have a little problem with my if(isset($_POST[\'submit\'])) code. What I want is some echos and a table to not appear when the script is open but I do want it to show when the submit button for the form has been clicked. The problem is though that when I include the if(isset($_POST[\'submit\'])) function, when I click on the submit button it does not display the echos and the table at all. Why is this and can you help me out with this issue please.

Below is the code:

    <!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>

<title>Exam Interface</title>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
</head>
<body>

<p><strong>NOTE: </strong>If a search box is left blank, then the form will search for all data under that specific field</p>

<form action=\"exam_interface.php\" method=\"post\" name=\"sessionform\">        <!-- This will post the form to its own page\"-->
<p>Session ID: <input type=\"text\" name=\"sessionid\" /></p>      <!-- Enter Session Id here-->
<p>Module Number: <input type=\"text\" name=\"moduleid\" /></p>      <!-- Enter Module Id here-->
<p>Teacher Username: <input type=\"text\" name=\"teacherid\" /></p>      <!-- Enter Teacher here-->
<p>Student Username: <input type=\"text\" name=\"studentid\" /></p>      <!-- Enter User Id here-->
<p>Grade: <input type=\"text\" name=\"grade\" /></p>      <!-- Enter Grade here-->
<p>Order Results By: <select name=\"order\">
<option value=\"ordersessionid\">Session ID</option>
<option value=\"ordermoduleid\">Module Number</option>
<option value=\"orderteacherid\">Teacher Username</option>
<option value=\"orderstudentid\">Student Username</option>
<option value=\"ordergrade\">Grade</option>
</select>
<p><input type=\"submit\" value=\"Submit\" /></p>
</form>

<?php

$username=\"xxx\";
$password=\"xxx\";
$database=\"mobile_app\";

mysql_connect(\'localhost\',$username,$password);

@mysql_select_db($database) or die(\"Unable to select database\");

$sessionid = isset ($_POST[\'sessionid\']) ? $_POST[\'sessionid\'] : \"\";
$moduleid = isset ($_POST[\'moduleid\']) ? $_POST[\'moduleid\'] : \"\";
$teacherid = isset ($_POST[\'teacherid\']) ? $_POST[\'teacherid\'] : \"\";
$studentid = isset ($_POST[\'studentid\']) ? $_POST[\'studentid\'] : \"\";
$grade = isset ($_POST[\'grade\']) ? $_POST[\'grade\'] : \"\";
$orderfield = isset ($_POST[\'order\']) ? $_POST[\'order\'] : \"\";

$sessionid = mysql_real_escape_string($sessionid);
$moduleid = mysql_real_escape_string($moduleid);
$teacherid = mysql_real_escape_string($teacherid);
$studentid = mysql_real_escape_string($studentid);
$grade = mysql_real_escape_string($grade);

switch ($orderfield) {
    case \'ordersessionid\': $orderfield = \'gr.SessionId\';
    break;
    case \'ordermoduleid\': $orderfield = \'m.ModuleId\'; 
    break;
    case \'orderteacherid\': $orderfield = \'s.TeacherId\';
    break;
    case \'orderstudentid\': $orderfield = \'gr.StudentId\'; 
    break;
    case \'ordergrade\': $orderfield = \'gr.Grade\';
    break;
}

$ordertable = $orderfield;

$result = mysql_query(\"SELECT * FROM Module m INNER JOIN Session s ON m.ModuleId = s.ModuleId JOIN Grade_Report gr ON s.SessionId = gr.SessionId JOIN Student st ON gr.StudentId = st.StudentId WHERE (\'$sessionid\' = \'\' OR gr.SessionId = \'$sessionid\') AND (\'$moduleid\' = \'\' OR m.ModuleId = \'$moduleid\') AND (\'$teacherid\' = \'\' OR s.TeacherId = \'$teacherid\') AND (\'$studentid\' = \'\' OR gr.StudentId = \'$studentid\') AND (\'$grade\' = \'\' OR gr.Grade = \'$grade\') ORDER BY $ordertable ASC\");

$num=mysql_numrows($result);

if(isset($_POST[\'submit\'])){

echo \"<p>Your Search: <strong>Session ID:</strong> \"; if (empty($sessionid))echo \"\'All Sessions\'\"; else echo \"\'$sessionid\'\";echo \", <strong>Module ID:</strong> \"; if (empty($moduleid))echo \"\'All Modules\'\"; else echo \"\'$moduleid\'\";echo \", <strong>Teacher Username:</strong> \"; if (empty($teacherid))echo \"\'All Teachers\'\"; else echo \"\'$teacherid\'\";echo \", <strong>Student Username:</strong> \"; if (empty($studentid))echo \"\'All Students\'\"; else echo \"\'$studentid\'\";echo \", <strong>Grade:</strong> \"; if (empty($grade))echo \"\'All Grades\'\"; else echo \"\'$grade\'\"; \"</p>\";

echo \"<p>Number of Records Shown in Result of the Search: <strong>$num</strong></p>\";

echo \"<table border=\'1\'>
<tr>
<th>Student Id</th>
<th>Forename</th>
<th>Session Id</th>
<th>Grade</th>
<th>Mark</th>
<th>Module</th>
<th>Teacher</th>
</tr>\";

while ($row = mysql_fetch_array($result)){

 echo \"<tr>\";
  echo \"<td>\" . $row[\'StudentId\'] . \"</td>\";
  echo \"<td>\" . $row[\'Forename\'] . \"</td>\";
  echo \"<td>\" . $row[\'SessionId\'] . \"</td>\";
  echo \"<td>\" . $row[\'Grade\'] . \"</td>\";
  echo \"<td>\" . $row[\'Mark\'] . \"</td>\";
  echo \"<td>\" . $row[\'ModuleName\'] . \"</td>\";
  echo \"<td>\" . $row[\'TeacherId\'] . \"</td>\";
  echo \"</tr>\";
}

echo \"</table>\";

}

mysql_close();


 ?>

</body>
</html>

Any help will be much appreciated, Thank You.


回答1:


You need to give your submit <input> a name or it won't be available using $_POST['submit']:

<p><input type="submit" value="Submit" name="submit" /></p>



回答2:


You never named your submit button, so as far as the form is concerned it's just an action.

Either:

  1. Name the submit button (<input type="submit" name="submit" ... />)
  2. Test if (!empty($_POST)) instead to detect when data has been posted.

Remember that keys in the $_POST superglobal only appear for named input elements. So, unless the element has the name attribute, it won't come through to $_POST (or $_GET/$_REQUEST)




回答3:


What you're checking

if(isset($_POST['submit']))

but there's no variable name called "submit". well i want you to understand why it doesn't works. lets imagine if you give your submit button name delete <input type="submit" value="Submit" name="delete" /> and check if(isset($_POST['delete'])) then it works in this code you didn't give any name to submit button and checking its exist or not with isset(); function so php didn't find any variable like "submit" so its not working now try this :

<input type="submit" name="submit" value="Submit" />



回答4:


You must give a name to your submit button

<input type="submit" value"Submit" name="login">

Then you can call the button with $_POST['login']




回答5:


The $_post function need the name value like:

<input type="submit" value"Submit" name="example">

Call

$var = strip_tags($_POST['example']);
if (isset($var)){
    // your code here
}



回答6:


Whats wrong in this?

<form class="navbar-form navbar-right" method="post" action="login.php">
  <div class="form-group">
    <input type="email" name="email" class="form-control" placeholder="email">
    <input type="password" name="password" class="form-control" placeholder="password">
  </div>
  <input type="submit" name="submit" value="submit" class="btn btn-success">
</form>

login.php

if(isset($_POST['submit']) && !empty($_POST['submit'])) {
  // if (!logged_in()) 
  echo 'asodj';
}



回答7:


Another option is to use

$_SERVER['REQUEST_METHOD'] == 'POST'


来源:https://stackoverflow.com/questions/7775512/using-ifisset-postsubmit-to-not-display-echo-when-script-is-open-is-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!