welard Posted March 5, 2012 Share Posted March 5, 2012 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/258347-notice-undefined-index-action-please-help/ Share on other sites More sharing options...
scootstah Posted March 5, 2012 Share Posted March 5, 2012 Try: $action = isset($_GET['action']) ? $_GET['action'] : ''; switch($action) { ... Quote Link to comment https://forums.phpfreaks.com/topic/258347-notice-undefined-index-action-please-help/#findComment-1324285 Share on other sites More sharing options...
smerny Posted March 5, 2012 Share Posted March 5, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/258347-notice-undefined-index-action-please-help/#findComment-1324287 Share on other sites More sharing options...
welard Posted March 5, 2012 Author Share Posted March 5, 2012 lol i have to say thanks very much i cant bleave i was so close thanks very much the both of you thanks agen carl Quote Link to comment https://forums.phpfreaks.com/topic/258347-notice-undefined-index-action-please-help/#findComment-1324294 Share on other sites More sharing options...
Andy-H Posted March 5, 2012 Share Posted March 5, 2012 // 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 Quote Link to comment https://forums.phpfreaks.com/topic/258347-notice-undefined-index-action-please-help/#findComment-1324311 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.