Jump to content

Notice: Undefined index: action please help


welard

Recommended Posts

hi i am carl i am quite new to php and i am haveing a problem with my code

this is the error

Notice: Undefined index: action in sale.php line 30

I was on the understanding if it could not find " ?action"  it would go to default witch it does but it thoughs up the error sorry for my spellings

thanks in advance

carl

<style type="text/css">
<!--
body,td,th {
color: #FF0000;
}
body {
background-color: #CCFFFF;
}
a:link {
color: #FF0000;
}
a:visited {
color: #FF0000;
}
-->
</style>
<script type="text/javascript" language="JavaScript1.2" SRC="scripts/stm31.js"></script>
<?php
session_start();
include 'mainmenu.php';

print "<table width=100% height='400' border='0'>
  <tr>
    
	<td width='700' valign=top  >";
  
       
  #if(isset($_GET["action"]))
  
switch($_GET['action'])
{
case 'sale': sale(); break;
case 'wanted': wanted(); break;
case 'free': free(); break;
case 'add' : add(); break;


default:
home();
break;

}
function home()
{
print "<center><h3>for sale,wanted or free items</h3><br />
here you can view <br />
<a href='sale.php?action=sale'>Items For Sale</a><br />
<a href='sale.php?action=wanted'>Items Wanted</a><br />
<a href='sale.php?action=free'>Free items</a><br />
<a href='sale.php?action=add'>Post items</a><br />
";

}

function sale()
{
print "<center><table BORDER=4 CELLPADDING=2  CELLSPACING=2 WIDTH=95% valign=top>
<tr style='background:gray'>	<th>Item name</th> 	<th>Description</th>	</tr></center><br />";

include "config.php";

	$result= mysql_query("SELECT * FROM sale ")or die(mysql_error());

while($r = mysql_fetch_array($result))

			{
				print "<tr>	<td><center>{$r['iname']}</center></td>	<td>{$r['dec']}</td>	</tr>";
			}
		print "</table>";
}
function wanted()
{
print "<center><table BORDER=4 CELLPADDING=2  CELLSPACING=2 WIDTH=95% valign=top>
<tr style='background:gray'>	<th>Item Wanted</th> 	<th>Description</th>	</tr></center><br />";

include "config.php";

	$result= mysql_query("SELECT * FROM wanted ")or die(mysql_error());

while($r = mysql_fetch_array($result))

			{
				print "<tr>	<td><center>{$r['iname']}</center></td>	<td>{$r['dec']}</td>	</tr>";
			}
		print "</table>";
}
function free()
{
print "<center><table BORDER=4 CELLPADDING=2  CELLSPACING=2 WIDTH=95% valign=top>
<tr style='background:gray'>	<th>Free Item </th> 	<th>Description</th>	</tr></center><br />";

include "config.php";

	$result= mysql_query("SELECT * FROM free ")or die(mysql_error());

while($r = mysql_fetch_array($result))

			{
				print "<tr>	<td><center>{$r['iname']}</center></td>	<td><center>{$r['dec']}</center></td>	</tr>";
			}
		print "</table>";
}
function add()
{

print " <center><table BORDER=4 CELLPADDING=2  CELLSPACING=2 WIDTH=95% valign=top>
<tr style='background:gray'>	<th>Item posting</th>	</tr></center><br />";

include "config.php";


		print "<th>Your Name: <br />
    <form method='post' action='pros.php'>
  <input type='text' name='name' size='35' />
      <br />
      Your Email:<br />
      <input type='text' name='email' size='35' />
      <br />
        <select name='type' size='1'>		
        <option value='sale'>For Sale</option>
        <option value='wanted'>Wanted</option>
        <option value='free'>Items for free</option>
              </select>
      <br /><br />
  Item Name:
  <br />
  <textarea name='iname' rows='2' cols='15'></textarea><br />
       Message:
      <br />
      <textarea name='dec' rows='4' cols='40'></textarea>
      <br />
  <img src='captcha.php'><br />
Enter the code above: <input type='text' name='captcha'>

      <input type='submit' value='post' /></th>";
			}
		print "</table>";


print " </tr>
    </table></td>
  </tr>
</table>";
?>

Link to comment
Share on other sites

looks like you already started this.. but you could just check if it's set beforehand

 

if(isset($_GET["action"])){
switch($_GET['action'])
{
  case 'sale': sale(); break;
  case 'wanted': wanted(); break;
  case 'free': free(); break;
  case 'add' : add(); break;
  default: home(); break;
}
} else {
home();
}

 

edit:actually scootstah's way would be cleaner

Link to comment
Share on other sites

 

 

// functions.php
function home()
{
echo <<<HTML
         <center>
            <h3>for sale,wanted or free items</h3><br />
            here you can view <br />
            <a href='sale.php?action=sale'>Items For Sale</a><br />
            <a href='sale.php?action=wanted'>Items Wanted</a><br />
            <a href='sale.php?action=free'>Free items</a><br />
            <a href='sale.php?action=add'>Post items</a><br />
HTML;
}
function sale()
{
echo <<<HTML
         <table border="4" cellpadding="2" cellspacing="2" width="95%" valign="top" align="center">
            <tr style="background:gray">
               <th>Item name</th>
               <th>Description</th>
            </tr>
HTML;
include "config.php";
$result = mysql_query("SELECT * FROM sale ")or die(mysql_error());
while($r = mysql_fetch_array($result))
{
	echo <<<HTML
            <tr>
               <td align="center">{$r['iname']}</td>
               <td>{$r['dec']}</td>
            </tr>
HTML;
}
echo <<<HTML
         </table>
HTML;
}
function wanted()
{
echo <<<HTML
         <table border="4" cellpadding="2" cellspacing="2" width="95%" valign="top" align="center">
            <tr style="background:gray">
               <th>Item Wanted</th>
               <th>Description</th>
            </tr>
HTML;
include "config.php";
$result= mysql_query("SELECT * FROM wanted ")or die(mysql_error());
while($r = mysql_fetch_array($result))
{
	echo <<<HTML
            <tr>
               <td align="center">{$r['iname']}</td>
               <td>{$r['dec']}</td>
            </tr>
HTML;
}
echo <<<HTML
         </table>
HTML;
}
function free()
{
echo <<<HTML
         <table border="4" cellpadding="2" cellspacing="2" width="95%" valign="top" align="center">
            <tr style="background:gray">
               <th>Free Item </th>
               <th>Description</th>
            </tr>
HTML;
include "config.php";
$result= mysql_query("SELECT * FROM free ")or die(mysql_error());
while($r = mysql_fetch_array($result))
{
	echo <<<HTML
            <tr>
               <td align="center">{$r['iname']}</td>
               <td>{$r['dec']}</td>
            </tr>
HTML;
}
echo <<<HTML
         </table>
HTML;
}
function add()
{
echo <<<HTML
         <table border="4" cellpadding="2" cellspacing="2" width="95%" valign="top" align="center">
            <tr style="background:gray">
               <th>Item posting</th>
            </tr>
HTML;
include "config.php";
echo <<<HTML
            <tr>
               <th>
                  Your Name: <br />
                  <form method="post" action="pros.php">
                     <input type="text" name="name" size="35" />
                     <br />
                     Your Email:<br />
                     <input type="text" name="email" size="35" />
                     <br />
                     <select name="type" size="1">      
                        <option value="sale">For Sale</option>
                        <option value="wanted">Wanted</option>
                        <option value="free">Items for free</option>
                     </select>
                     <br /><br />
                     Item Name:
                     <br />
                     <textarea name="iname" rows="2" cols="15"></textarea>
                     <br />
                     Message:
                     <br />
                     <textarea name="dec" rows="4" cols="40"></textarea>
                     <br />
                     <img src="captcha.php"><br />
                     Enter the code above: <input type="text" name="captcha">
                     <input type="submit" value="post" />
               </th>
            </tr>
         </table>
HTML;
}

<?php
session_start();
include 'mainmenu.php';
include 'functions.php';
?>
<style type="text/css">
<!--
body,td,th {
   color: #FF0000;
}
body {
   background-color: #CCFFFF;
}
a:link {
   color: #FF0000;
}
a:visited {
   color: #FF0000;
}
-->
</style>
<script type="text/javascript" language="JavaScript1.2" SRC="scripts/stm31.js"></script>
<?php
echo <<<HTML
<table width="100%" height="400" border="0">
<tr>
	<td width="700" valign="top" align="center">
HTML;


$wl     = array('home', 'sale', 'wanted', 'free', 'add');
$action = in_array($_GET['action'], $wl) ? $_GET['action'] : 'home';


$action();


echo <<<HTML
      </td>
   </tr>
</table>
HTML;
?>

 

 

in_array, heredoc, variable function

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.