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

uinbv5nw  于 2023-08-02  发布在  PHP
关注(0)|答案(9)|浏览(130)

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

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

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

zsohkypk

zsohkypk1#

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

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

字符串

guicsvcw

guicsvcw2#

你在检查什么

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

字符串
但是没有一个叫做“submit”的变量名。我想让你明白为什么它不起作用。让我们想象一下,如果你给予你的提交按钮命名delete <input type="submit" value="Submit" name="delete" />,然后检查if(isset($_POST['delete'])),那么它在这段代码中工作,你没有给提交按钮任何名称,并检查它是否存在与isset();函数,所以php没有找到任何变量,如“submit”,所以它现在不工作了试试这个:

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

vnjpjtjt

vnjpjtjt3#

你从来没有命名你的提交按钮,所以就表单而言,它只是一个动作。
要么:
1.将提交按钮命名为(<input type="submit" name="submit" ... />
1.改为测试if (!empty($_POST)),以检测何时发布数据。
请记住,$_POST超全局中的键只出现在 named input元素中。因此,除非元素具有name属性,否则它不会进入$_POST(或$_GET/$_REQUEST

ssgvzors

ssgvzors4#

这有什么不对吗?

<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';
}

3htmauhk

3htmauhk5#

您必须为提交按钮命名

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

字符串
然后您可以调用带有$_POST['login']的按钮

pxq42qpu

pxq42qpu6#

$_post函数需要如下的name值:

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

字符串
呼叫

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

mzaanser

mzaanser7#

另一种选择是使用

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

字符串

vxf3dgd4

vxf3dgd48#

如果(isset($_POST[submit])),任何时候尝试此函数都会显示错误代码(不要直接访问超级全局$_POST数组)

qyzbxkaa

qyzbxkaa9#

您可以用途:
第一个月
或者如果你愿意给我们纽扣
<button type="submit" name="submit">Submit</button>
在你的php脚本if(isset($_POST['submit']))
最重要的是“name”属性,用于执行表单的提交

相关问题