Page 1 of 1

Notice: Undefined index: submited in

Posted: Tue Dec 09, 2003 11:19 am
by Deddog
Hello All,
recently started to trawl through an existing intranet so that it will work on the latest versions of PHP and MYSQL.

I am aware that i have to change the way variable's are handled i.e
$bob = $_POST["bob"] instead of $bob = $bob

Problem lies in using the new method where the $_POST variable has not been defined yet. One example is isset($submit) where the form posta to itself $PHP_SELF.

I get "Notice: Undefined index: submited in " - How can i change my code to correct this problem? Please see below example:

Code: Select all

<?php
if ($submited == "yes"){
ConnectNoticeBoard();
$ip = $_SERVER['REMOTE_ADDR']; 
$time = date(Ymd);
$url = "No thanks";
$sql = "INSERT INTO shoutbox
VALUES(NULL,'$names','$shouts','$url','$time','$ip')";
mysql_query($sql)or die(mysql_error().'<p>'.$sql.'</p>');
//	mysql_close();
//	print("window.location.reload()");
}
?>
<div align="left"></div>
<form name="shoutForm" id="shoutForm" method="post" action="<? $PHP_SELF ?>" >
<div align="center" valign="top"> 
<input type="text" id="names"  name="names" size="22" maxlength="30" class="shoutform" value="name" onFocus="ClearField(this)">
<input type="text" id="shouts" name="shouts" size="22" value="shout" maxlength="500" class="shoutform" onFocus="ClearField(this)">
<br>
<br>
<input type="button" name="shout" value="shout"  class="shoutbutton" onClick="verifyShoutbox(this.form)"><input name="submited" type="hidden" id="submited">
</div>
</form>
<?php 
ConnectNoticeBoard();
$sql = "select t.shout, t.name from shoutbox t order by id desc LIMIT 15";
$result = mysql_query($sql);
mysql_close();
print("<table width="90%" border="0" cellspacing="2" cellpadding="2" valign="top" align="center">");		
while ($record = mysql_fetch_array($result,MYSQL_ASSOC)) {
$output[$count]=array( 'nick' => stripslashes($record["name"]),
'shout' => stripslashes($record["shout"]));
$count++;
}
//  $output = array_reverse($output);
//---------------------------------------
foreach ($output as $shout) {
$row = mysql_fetch_array($result);
$Shout = $row["shout"];
$name = $row["name"];
print("<tr valign="top" align="left">");
print("<td width="90%">");
printf("<span class="shouttext"><b>%s</b>%s%s</span>",$shout["nick"],": ",$shout["shout"]);
print("</td>");
print("</tr>");
}
print("</table>");
$submited ="no"
?>
[edit : Infolock- added php tags for eye candy]

Posted: Tue Dec 09, 2003 12:29 pm
by Weirdan
may be:

Code: Select all

$submitted=isset($_POST['something'])?"yes":"no";

Posted: Wed Dec 10, 2003 4:40 am
by twigletmac
Something like this:

Code: Select all

if ($submited == "yes"){
could be changed to:

Code: Select all

if (isset($_POST['submitted']) && $_POST['submitted'] == 'yes') {
Just check whether the variable is set first and then run the test once that has been determined.

Mac

Many Thanks

Posted: Wed Dec 10, 2003 5:00 am
by Deddog
Phew, go there in the end - what with undefined index or variables..

Below is my finished product, a kind of Twigletmac-weirdan hybrid :D .

Thanks again for your help.

<?PHP
function draw_shouts(){
ConnectNoticeBoard();
$sql = "select t.shout, t.name from shoutbox t order by id desc LIMIT 5";
$result = mysql_query($sql);
mysql_close();
$count = 0;
print("<table width=\"90%\" border=\"0\" cellspacing=\"2\" cellpadding=\"2\" valign=\"top\" align=\"center\">");
while ($record = mysql_fetch_array($result,MYSQL_ASSOC)) {
$output[$count]=array( 'nick' => stripslashes($record["name"]),
'shout' => stripslashes($record["shout"]));
$count++;
} // end of while
// $output = array_reverse($output);
//---------------------------------------
foreach ($output as $shout) {
$row = mysql_fetch_array($result);
$Shout = $row["shout"];
$name = $row["name"];
print("<tr valign=\"top\" align=\"left\">");
print("<td width=\"90%\">");
printf("<span class=\"shouttext\"><b>%s</b>%s%s</span>",$shout["nick"],": ",$shout["shout"]);
print("</td>");
print("</tr>");
} // end of foreach
print("</table>");
} // end of function draw shouts
?>
<div align="left"></div>
<form name="shoutForm" id="shoutForm" method="post" action="<? $_SERVER['PHP_SELF'] ?>" >
<div align="center" valign=\"top\">
<input type="text" id="names" name="names" size="22" maxlength="30" class="shoutform" value="name" onFocus="ClearField(this)">
<input type="text" id="shouts" name="shouts" size="22" value="shout" maxlength="500" class="shoutform" onFocus="ClearField(this)">
<br>
<br>
<input type="button" name="shout" value="shout" class="shoutbutton" onClick="verifyShoutbox(this.form)">
<input name="submited" type="hidden" id="submited" >
</div>
</form>
<?php
//----------------------------------------------------------------------------------------
$submit =isset($_POST['submited'])?"yes":"nope";
if(isset($submit) && ($submit == "yes")){
ConnectNoticeBoard();
$ip = $_SERVER['REMOTE_ADDR'];
$time = date('Ymd');
$url = "No thanks";
$name = $_POST['names'];
$shout = $_POST['shouts'];
$sql = "INSERT INTO shoutbox
VALUES(NULL,'$name','$shout','$url','$time','$ip')";
mysql_query($sql)or die(mysql_error().'<p>'.$sql.'</p>');

} // end of issest
draw_shouts();
?>

Posted: Wed Dec 10, 2003 2:52 pm
by Weirdan
as $submit is always defined you don't need to check if it is set so you can safely rewrite

Code: Select all

if(isset($submit) && ($submit == "yes")){
as

Code: Select all

if($submit == "yes"){
so your script will be few nanoseconds faster ;)

And if you want your source to be more readable use bool variables:

Code: Select all

$submitted =isset($_POST['submited']);
if($submitted){
  // do something
}