2

我正在尝试从组合框的上一页中获取值。

[删除.php]

 <form name="delete" action="deleted.php" method="post">
<?php
$connect = mysql_connect("a","b","") or die("Error connecting");
mysql_select_db("c") or die("Error connecting to database");
$result = mysql_query("SELECT * FROM d ORDER BY e ASC");
echo "<select name='forward'>";
while ($row = mysql_fetch_array($result))
  {
 echo "<option class='class' name=" .$row['t'] . ">".$row['t'] ."</option>";
}
echo "</select><br/><br/><br/><br/>";
?>
<input type="submit" id="thisSbmit" value="Delete Contact" onClick="chck()">&nbsp;&nbsp;<input type="button" id="cls" class="cls" value="Clear">
</form>

[已删除.php]

<?php
$selected = $_POST['forward'];

if ($selected== 'kryptix') 
{
    alert('No one was seleclted');
}else{
alert('Success');
}
?>

但我收到以下错误:

注意:未定义的索引:在第 2 行的 C:\xampp\htdocs\folder\deleted.php 中转发

  • 第 2 行 = $selected = $_POST['forward']; *

我在这里做错了什么?

4

7 回答 7

-1

当您尝试获取 $_POST['forward'] 的值时,您会得到未定义的索引,此时代码如下:

<form name="delete" action="deleted.php" method="post">
<?php
$connect = mysql_connect("a","b","") or die("Error connecting");
mysql_select_db("c") or die("Error connecting to database");
$result = mysql_query("SELECT * FROM d ORDER BY e ASC");
echo "<select name='forward'>";
while ($row = mysql_fetch_array($result))
  {
 echo "<option class='class' name=" .$row['t'] . ">".$row['t'] ."</option>";
}
echo "</select><br/><br/><br/><br/>";
?>
<input type="submit" id="thisSbmit" value="Delete Contact" onClick="chck()">&nbsp;&nbsp;<input type="button" id="cls" class="cls" value="Clear">
</form>

当做这样的代码时:(我已经注释掉了数据库部分)

 <form name="delete" action="deleted.php" method="post">
<?php
//$connect = mysql_connect("a","b","") or die("Error connecting");
//mysql_select_db("c") or die("Error connecting to database");
//$result = mysql_query("SELECT * FROM d ORDER BY e ASC");
$row['t'] = 1; //Dummy
echo "<select name='forward'>";
echo "<option class='class' name=" .$row['t'] . ">".$row['t'] ."</option>";
echo "</select><br/><br/><br/><br/>";
?>
<input type="submit" id="thisSbmit" value="Delete Contact" onClick="chck()">&nbsp;&nbsp;<input type="button" id="cls" class="cls" value="Clear">
</form>

您不会在您的 deleted.php 中收到未定义的索引警告。

使用以下代码,您也不会收到索引警告。

 <form name="delete" action="deleted.php" method="post">
<?php
//$connect = mysql_connect("a","b","") or die("Error connecting");
//mysql_select_db("c") or die("Error connecting to database");
//$result = mysql_query("SELECT * FROM d ORDER BY e ASC");
$row['t'] = 1; //Dummy
$row['u'] = 2; //Dummy
echo "<select name='forward'>";
echo "<option class='class' name=" .$row['t'] . ">".$row['t'] ."</option>";
echo "<option class='class' name=" .$row['u'] . ">".$row['u'] ."</option>";
echo "</select><br/><br/><br/><br/>";
?>
<input type="submit" id="thisSbmit" value="Delete Contact" onClick="chck()">&nbsp;&nbsp;<input type="button" id="cls" class="cls" value="Clear">
</form>

AND $_POST['forward']实际上会在未设置值时返回选项的名称。(1 或 2)。但是你应该像这样使用 value="" :

 <form name="delete" action="deleted.php" method="post">
<?php
//$connect = mysql_connect("a","b","") or die("Error connecting");
//mysql_select_db("c") or die("Error connecting to database");
//$result = mysql_query("SELECT * FROM d ORDER BY e ASC");
$row['t'] = 1; //Dummy
$row['u'] = 2; //Dummy
echo "<select name='forward'>";
echo "<option class='class' value=" .$row['t'] . ">".$row['t'] ."</option>";
echo "<option class='class' value=" .$row['u'] . ">".$row['u'] ."</option>";
echo "</select><br/><br/><br/><br/>";
?>
<input type="submit" id="thisSbmit" value="Delete Contact" onClick="chck()">&nbsp;&nbsp;<input type="button" id="cls" class="cls" value="Clear">
</form>

但是,当您在代码中没有任何选项时(在下面注释掉),您将收到未定义的索引警告。

 <form name="delete" action="deleted.php" method="post">
<?php
//$connect = mysql_connect("a","b","") or die("Error connecting");
//mysql_select_db("c") or die("Error connecting to database");
//$result = mysql_query("SELECT * FROM d ORDER BY e ASC");
$row['t'] = 1; //Dummy
$row['u'] = 2; //Dummy
echo "<select name='forward'>";
//echo "<option class='class' value=" .$row['t'] . ">".$row['t'] ."</option>";
//echo "<option class='class' value=" .$row['u'] . ">".$row['u'] ."</option>";
echo "</select><br/><br/><br/><br/>";
?>
<input type="submit" id="thisSbmit" value="Delete Contact" onClick="chck()">&nbsp;&nbsp;<input type="button" id="cls" class="cls" value="Clear">
</form>

这是因为选择列表不包含任何内容(没有选项)。

因此我的问题是:您的选择列表中实际上有任何选项吗?

试试这段代码,看看echo print_r($row,true);会给你带来什么:

 <form name="delete" action="deleted.php" method="post">
<?php
$connect = mysql_connect("a","b","") or die("Error connecting");
mysql_select_db("c") or die("Error connecting to database");
$result = mysql_query("SELECT * FROM d ORDER BY e ASC");

/*debugging start
$row = mysql_fetch_array($result)
echo print_r($row,true);
debugging end */

echo "<select name='forward'>";
while ($row = mysql_fetch_array($result))
  {
 echo "<option class='class' value=\"" .$row['t'] . "\">".$row['t'] ."</option>";
}
echo "</select><br/><br/><br/><br/>";
?>
<input type="submit" id="thisSbmit" value="Delete Contact" onClick="chck()">&nbsp;&nbsp;<input type="button" id="cls" class="cls" value="Clear">
</form>
于 2013-04-03T06:36:50.600 回答
-1

你没有回应你的选择。因此,永远不会设置 $_POST['forward']:

"<select name='forward'>";

应该

echo "<select name='forward'>";

你最好还是跳出 php 来写 html。它更易于阅读,并允许更好的标记编写而无需转义等。

<select name='forward'>
<?php while ($row = mysql_fetch_array($result)): ?>
    <option class='class' value="<?php echo $row['t']; ?>"><?php echo $row['t']; ?></option>
<?php endwhile; ?>
</select>

此外,正如 IOIO MAD 在他的标记示例中显示的那样,除非您使用 ajax,否则您肯定需要在表单中发布任何内容

于 2013-04-03T01:44:28.690 回答
-1
echo "<select name='forward'>"; //cant find a echo there in your code

添加到您的 php 接收器代码以进行额外验证

if(isset($_POST['forward']))
    {

    //your code here  {put [query] here} code
    }

更新: 您在标签中缺少value属性 <option>

echo "<option class='class' name=" .$row['t'] . ">".$row['t'] ."</option>";

您的代码中的上述行没有值字段(即转发的值是 Undefined )?添加value field

echo "<option class='class' name='" .$row['t'] ."' value='".$row['t'] ."'>".$row['t'] ."</option>";
于 2013-04-03T01:44:43.820 回答
-1

没有错,只是对 PHP 的警告。如果您想避免该警告,您可以使用该isset()功能或​​关闭 php.ini 中的“通知”错误报告。看到这个答案

于 2013-04-03T01:46:26.650 回答
-1

there is no problem in code.You are using Alert in the PHP.that is why you are getting error. if you want you can isset($_POST(forward)) for checking.

于 2013-07-24T10:15:27.487 回答
-1

尝试以下操作:

你的错误是你提到name而不是value

所以更改以下内容并测试它,让我知道。

 echo "<option class='class' name=" .$row['t'] . ">".$row['t'] ."</option>";

进入

echo "<option class='class' value=" .$row['t'] . ">".$row['t'] ."</option>";

谢谢。

于 2016-07-14T10:15:37.613 回答
-1
<form name="delete" action="delete.php" method="post">
<?php
$connect = mysql_connect("localhost","root","menu32") or die("Error connecting");
mysql_select_db("MyProject") or die("Error connecting to database");
$result = mysql_query("SELECT name FROM teacher_detail ORDER BY name ASC limit 5");?>
<select name='forward'>
<option name='' value=''>--select--</option>
<?php 
while ($row = mysql_fetch_array($result))
  {?>
 <option class='class' name="<?= $row['name'] ?>"><?=$row['name'] ?></option>";
<?php } ?>
</select><br/><br/><br/><br/>

<input type="submit" id="thisSbmit" value="Delete Contact" onClick="chck()">&nbsp;&nbsp;
<input type="button" id="cls" class="cls" value="Clear">
</form>

删除.php

<?php

$selected= $_POST['forward'];
if($selected=='Babita')
    echo "matched string";
else
    echo "not found";
于 2016-06-13T10:36:34.990 回答