使用 if(isset($_POST['submit'])) 在脚本打开时不显示回显不起作用
我的 if(isset($_POST['submit']))
代码有一点问题。我想要的是一些回声和一个表格,当脚本打开时不会出现,但我确实希望它在单击表单的提交按钮时显示。问题是,当我包含 if(isset($_POST['submit']))
函数时,当我单击提交按钮时,它根本不显示回声和表格。这是为什么?您能帮我解决这个问题吗?
以下是代码:
<!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>
任何帮助将不胜感激,谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您需要为提交的
指定一个名称,否则
$_POST['submit']
将无法使用:You need to give your submit
<input>
a name, otherwise$_POST['submit']
won't be available for use:您正在检查的内容
,但没有名为“submit”的变量名称。
好吧,我想让你明白为什么它不起作用。
让我们想象一下,如果您将提交按钮名称命名为“删除”
并检查
if(isset($_POST['delete']))
然后它在这段代码中工作,你没有给提交按钮提供任何名称,并使用 isset(); 函数检查其是否存在,因此 php 没有找到任何像“submit”这样的变量,所以它不是现在工作试试这个:What you're checking
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 checkif(isset($_POST['delete']))
then it works in this code you didn't give any name to submit button and checking its exist or not withisset();
function so php didn't find any variable like "submit" so its not working now try this :您从未命名过提交按钮,因此就表单而言,它只是一个操作。
要么:
)
if (!empty($_POST))
而是检测数据何时发布。请记住,
$_POST
超全局中的键仅出现在命名 输入元素中。因此,除非元素具有 name 属性,否则它不会到达$_POST
(或$_GET
/$_REQUEST
)You never named your submit button, so as far as the form is concerned it's just an action.
Either:
<input type="submit" name="submit" ... />
)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
)这有什么问题吗?
登录.php
Whats wrong in this?
login.php
您必须为提交按钮命名
然后您可以使用
$_POST['login']
调用该按钮You must give a name to your submit button
Then you can call the button with
$_POST['login']
$_post
函数需要 name 值喜欢:
打电话
The
$_post
function need the name valuelike:
Call
另一种选择是使用
Another option is to use
if (isset($_POST[submit]) ,任何时候尝试此函数都会显示错误代码(不要直接访问超级全局 $_POST 数组)
If (isset($_POST[submit]) ,any time try this function it displays error code (do not access super global $_POST array directly)
您可以使用:
"
或者如果您愿意使用按钮
在你的 php 脚本中
if(isset($_POST['submit']))
最重要的是“name”属性来执行表单的提交
You can use :
<input type="submit" value="submit" name="submit">"
Or if you will to us buttons
<button type="submit" name="submit">Submit</button>
In your php script
if(isset($_POST['submit']))
The most important is the "name" attribute to perform the submission of the form