2

I'm trying to get the value from a previous page from a combobox.

[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');
}
?>

but i get the following error:

Notice: Undefined index: forward in C:\xampp\htdocs\folder\deleted.php on line 2

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

What am i doing wrong here?

miken32
  • 42,008
  • 16
  • 111
  • 154
Xtacy Xtatic
  • 41
  • 1
  • 2
  • 7

7 Answers7

-1

You're not echoing your select. Therefore, $_POST['forward'] will never be set:

"<select name='forward'>";

should be

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

You may be better off breaking out of php to write html. It's easier to read and allows better markup writing without escaping and so on.

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

Also, as IOIO MAD shows in his markup example, you definitely need this in a form to post anything unless you're using ajax

Kai Qing
  • 18,793
  • 5
  • 39
  • 57
  • is alert() a php function you wrote? also, post your form code so we can verify it is constructed correctly – Kai Qing Apr 03 '13 at 01:51
  • see my updated code. specifically the while loop option markup – Kai Qing Apr 03 '13 at 01:52
  • right, and now the only thing that looks wrong immediately is that you're using name instead of value in your option tag. That and that I don't know what alert() does. – Kai Qing Apr 03 '13 at 02:04
  • alert() is javascript? I guess it should be echo ? – bestprogrammerintheworld Apr 03 '13 at 02:08
  • it could also be a php function if he defined it somewhere. Since we don't see that it is left to assumption. Either way it would spit out an error at least. – Kai Qing Apr 03 '13 at 02:20
  • okay but what should my GET be? $_GET['forward'] or $_GET[what the value is]? – Xtacy Xtatic Apr 03 '13 at 02:42
  • it should not be $_GET. it should be $_POST because that's the method your form uses. and it should be $_POST['forward'] because your select box has a name of forward: – Kai Qing Apr 03 '13 at 03:00
-1
echo "<select name='forward'>"; //cant find a echo there in your code

Add to your php receiver code for extra validation

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

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

updated: you are missing value attribute in <option> Tag

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

the above line in your code doesn't have a value field (ie forward's value is Undefined )? add the value field

echo "<option class='class' name='" .$row['t'] ."' value='".$row['t'] ."'>".$row['t'] ."</option>";
internals-in
  • 4,798
  • 2
  • 21
  • 38
-1

There is nothing wrong, is just a warning on PHP. If you want to avoid that warning you can use the isset() function or turn off "notice" error reporting in php. See this answer

Community
  • 1
  • 1
Jorge Zapata
  • 2,316
  • 1
  • 30
  • 57
-1

You get the undefined index when trying to get value of $_POST['forward'] when having this code:

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

When doing a code like this: (I've commented out the database-part)

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

You will not recieve an undefined index-warning in your deleted.php.

With following code, you will not recieve an index-warning either.

 <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'] would actually return the name of option when no value is set. (1 or 2). BUT you SHOULD use value="" like this:

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

However, when you don't have any options in the code (commented out below), you will get an undefined Index warning.

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

This is because the select list contains nothing (no options).

Therefore my question: Do you actually have any options in your select-list?

Try this code, and see what echo print_r($row,true); brings you:

 <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>
bestprogrammerintheworld
  • 5,417
  • 7
  • 43
  • 72
-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.

NEO
  • 1
-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";
ArK
  • 20,698
  • 67
  • 109
  • 136
Rohit Sharma
  • 1
  • 1
  • 3
-1

Try the following:

Your mistake is you are mention name instead of value.

So change the following and test it, let me know.

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

Into

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

Thanks.

Maths RkBala
  • 2,207
  • 3
  • 18
  • 21