php 使用 if(isset($_POST['submit'])) 在脚本打开时不显示 echo 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7775512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:21:45  来源:igfitidea点击:

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

phpmysqlpostif-statementisset

提问by BruceyBandit

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.

我的if(isset($_POST['submit']))代码有点问题。我想要的是一些回声和一个在脚本打开时不出现的表格,但我确实希望它在表单的提交按钮被点击时显示。问题是,当我包含该if(isset($_POST['submit']))函数时,当我单击提交按钮时,它根本不显示回声和表格。这是为什么,你能帮我解决这个问题吗?

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.

任何帮助将不胜感激,谢谢。

回答by Clive

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

您需要为您的提交<input>命名,否则将无法使用$_POST['submit']

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

回答by Mohit Bumb

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" value="Submit" name="delete" />并检查if(isset($_POST['delete']))然后它在这段代码中工作你没有给提交按钮提供任何名称并检查它是否存在与isset();函数所以php没有找到任何像“提交”这样的变量所以它现在不工作试试这个:

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

回答by Brad Christie

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.
  1. 命名提交按钮 ( <input type="submit" name="submit" ... />)
  2. if (!empty($_POST))改为测试以检测何时发布数据。

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

请记住,$_POST超全局中的键只出现在命名的输入元素中。因此,除非元素具有 name 属性,否则它不会通过$_POST(或$_GET/ $_REQUEST)

回答by Cristina Nogués

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']

然后你可以调用按钮 $_POST['login']

回答by saied lakhdar

The $_postfunction need the name value like:

$_post函数需要名称值,如:

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

Call

称呼

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

回答by Himanshu Kriplani

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

登录.php

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

回答by Tansel Yuzer

Another option is to use

另一种选择是使用

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