从PHP中的组合框获取价值

时间:2013-04-03 01:41:16

标签: php

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

[delete.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>

[deleted.php]

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

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

但是我收到以下错误:

  

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

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

我在这里做错了什么?

7 个答案:

答案 0 :(得分:-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,否则你肯定需要在表单中发布任何内容

答案 1 :(得分:-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>";

您的代码中的上一行没有值字段(即转发的值是未定义的)? 添加 value field

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

答案 2 :(得分:-1)

没有错,只是对PHP的警告。 如果您想避免该警告,可以使用isset()功能或关闭php中的“通知”错误报告。 See this answer

答案 3 :(得分:-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>

答案 4 :(得分:-1)

代码中没有问题。您在PHP中使用警告。这就是您收到错误的原因。 如果你想要,可以设置($ _ POST(转发))进行检查。

答案 5 :(得分:-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>

<强> delete.php

<?php

$selected= $_POST['forward'];
if($selected=='Babita')
    echo "matched string";
else
    echo "not found";

答案 6 :(得分:-1)

尝试以下方法:

您的错误是提及名称而非

所以改变以下内容并进行测试,请告诉我。

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

进入

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

感谢。