Jump to content


Error with script


12 replies to this topic

#1 kozine

    Young Padawan

  • Members
  • Pip
  • 43 posts

Posted 14 May 2007 - 10:16 AM

Hey guys,

I started having a go at some PHP yesterday. I successfully coded a small BBCodeParser which I am proud of ;)

I decided to code a small shoutbox.

I get 4 errors with my code:

Quote

Notice: Use of undefined constant name - assumed 'name' in C:\Program Files\EasyPHP 2.0b1\www\Chat\index.php on line 20

Notice: Undefined index: name in C:\Program Files\EasyPHP 2.0b1\www\Chat\index.php on line 20

Notice: Use of undefined constant message - assumed 'message' in C:\Program Files\EasyPHP 2.0b1\www\Chat\index.php on line 21

Notice: Undefined index: message in C:\Program Files\EasyPHP 2.0b1\www\Chat\index.php on line 21

Here is my code:
<?php
//Simple Shoutbox
//Coded by Ben Youle AKA kozine

//BBcodeparser
function bbcode($str)
{
$bbcode = array
(
'[b]'  => '<b>',
'[/b]' => '</b>',
'[u]' => '<u>',
'[/u]' => '</u>',
);
    $str = str_replace(array_keys($bbcode), array_values($bbcode), $str);
    return $str;
}

//Define the form variables to be wrote to the text file
$name = $_POST[name];
$message = $_POST[message];

//Open the file and add it to the variable $chat
$chat = fopen("chat.log", "r+");

//Write to the text file
fwrite($chat, $name, $message);

//Close the file
fclose($chat);
?>
<html>
<head>
<title>Simple Shoutbox</title>
</head>

<body>
<form method="post" action="index.php" name="messageform">

Name:<br>
<input type="text" name="name" id="name" size="50" maxlength="100"><br><br>
Message:<br>
<input type="text" name="message" id="message" size="50" maxlength="100"><br><br>

<input type="submit" value="Submit"><img src="spacer.gif" width=5 height=1 alt="spacer">
<input type="reset" value="Try again"></form>

You can use the following BBcodes:<br><br>
Bold: [b]Text Here[/b]<br>
Underline: [u]Text Here[/u]

</body>
</html>

I think you should be able to see what i'm doing.

Any help would be appreciated :)

#2 Frozen_W

    Young Padawan

  • Members
  • Pip
  • 186 posts
  • Gender:Male
  • Location:Belgium, Gistel
  • Interests:Just gimme a PC and i'm happy!<br />Also my friends, some concerts, parties,...<br />After the pc, the usual stuff;)

Posted 14 May 2007 - 10:25 AM

Firstly why do you use a log file to write down messages???
I should use MySql if possible!

But on the subject you should use a switch or atleast an if(!isset($_POST[...]) because now if you open your .php it first checks to write to your file, but it can't write nothing cause it's not specified

Greetz Frozen

Edited by Frozen_W, 14 May 2007 - 10:25 AM.


#3 kozine

    Young Padawan

  • Members
  • Pip
  • 43 posts

Posted 14 May 2007 - 10:31 AM

Sorry I don't quite understand.

That is the only file I have.

For the form I did have action="$_SERVER[PHP_SELF]" to make the form post to itself but that didnt work.

(!isset($_POST[...])

Please can you explain that a little bit more for me?

Sorry im still new to PHP!

Thanks for the quick reply.

#4 Frozen_W

    Young Padawan

  • Members
  • Pip
  • 186 posts
  • Gender:Male
  • Location:Belgium, Gistel
  • Interests:Just gimme a PC and i'm happy!<br />Also my friends, some concerts, parties,...<br />After the pc, the usual stuff;)

Posted 14 May 2007 - 10:42 AM

Sorry wasn't quite clear here I guess,

You first let your php document open, and you want it to create a form, right so far?
Then after the submit button is pressed you want that same document to check the $name ($_POST['name']) etc...
And then save them into your log document....

But here get's the problem, PHP isn't smart enough to first create the form and then check the values, it first will check the values and then create a form unless you let php do the opposite in your case

<?php
//Simple Shoutbox
//Coded by Ben Youle AKA kozine

//BBcodeparser
function bbcode($str)
{
$bbcode = array
(
'' => '<b>',
'' => '</b>',
'' => '<u>',
'' => '</u>',
);
$str = str_replace(array_keys($bbcode), array_values($bbcode), $str);
return $str;
}

//First check if the form already is made!
if(!isset($_POST['name']))
{//this whole chunk

//Define the form variables to be wrote to the text file
$name = $_POST[name];
$message = $_POST[message];

//Open the file and add it to the variable $chat
$chat = fopen("chat.log", "r+");

//Write to the text file
fwrite($chat, $name, $message);

//Close the file
fclose($chat);

}
else
{
include ("form.inc");
}
?>

//this is your include file!

<html>
<head>
<title>Simple Shoutbox</title>
</head>

<body>
<form method="post" action="index.php" name="messageform">

Name:<br>
<input type="text" name="name" id="name" size="50" maxlength="100"><br><br>
Message:<br>
<input type="text" name="message" id="message" size="50" maxlength="100"><br><br>

<input type="submit" value="Submit"><img src="spacer.gif" width=5 height=1 alt="spacer">
<input type="reset" value="Try again"></form>

You can use the following BBcodes:<br><br>
Bold: Text Here<br>
Underline: Text Here

</body>
</html>

So bassicaley you need two documents
A php, and an inc
Otherwise if you want it all in one document
you should echo all the html

#5 kozine

    Young Padawan

  • Members
  • Pip
  • 43 posts

Posted 14 May 2007 - 10:53 AM

Ah ok thanks! Ill try it out now and see if it works. Thanks for the quick reply!

Ok so i've used the code above and saved the form.inc file and I get the following errors:

Quote

Notice: Use of undefined constant name - assumed 'name' in C:\Program Files\EasyPHP 2.0b1\www\Chat\index.php on line 23

Notice: Undefined index: name in C:\Program Files\EasyPHP 2.0b1\www\Chat\index.php on line 23

Notice: Use of undefined constant message - assumed 'message' in C:\Program Files\EasyPHP 2.0b1\www\Chat\index.php on line 24

Notice: Undefined index: message in C:\Program Files\EasyPHP 2.0b1\www\Chat\index.php on line 24

Im confused.

Edited by kozine, 14 May 2007 - 10:56 AM.


#6 Frozen_W

    Young Padawan

  • Members
  • Pip
  • 186 posts
  • Gender:Male
  • Location:Belgium, Gistel
  • Interests:Just gimme a PC and i'm happy!<br />Also my friends, some concerts, parties,...<br />After the pc, the usual stuff;)

Posted 14 May 2007 - 10:59 AM

Are you testing on or offline?

#7 kozine

    Young Padawan

  • Members
  • Pip
  • 43 posts

Posted 14 May 2007 - 11:01 AM

Offline. Im using EasyPHP2.01b

Edited by kozine, 14 May 2007 - 11:01 AM.


#8 Frozen_W

    Young Padawan

  • Members
  • Pip
  • 186 posts
  • Gender:Male
  • Location:Belgium, Gistel
  • Interests:Just gimme a PC and i'm happy!<br />Also my friends, some concerts, parties,...<br />After the pc, the usual stuff;)

Posted 14 May 2007 - 11:04 AM

Can you perhaps send your php document?
(I'll check it out in macromedia)
Greetz
Because you have reall weard errors!

#9 kozine

    Young Padawan

  • Members
  • Pip
  • 43 posts

Posted 14 May 2007 - 11:05 AM

Do you have MSN? If so ill add you to MSN and send it via there.

#10 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 14 May 2007 - 12:22 PM

You guys are way over complicating things here.

You should start by researching your problem.
http://php.net/manua...r-reporting.php - Read this and pay specific attention to anything mentioned with E_NOTICE. Then put the following to your code.
error_reporting(E_ALL ^ E_NOTICE);

Then, read the section on Array do's and don'ts. You should put every array key inside quotes, they are not constants, they are strings. Numerical indexes don't count, and it would be a waste of time to put variable indexes in quotes.

Quote

Notice: Use of undefined constant name - assumed 'name' in C:\Program Files\EasyPHP 2.0b1\www\Chat\index.php on line 20

Notice: Use of undefined constant message - assumed 'message' in C:\Program Files\EasyPHP 2.0b1\www\Chat\index.php on line 21
<?php
//Simple Shoutbox
//Coded by Ben Youle AKA kozine

//BBcodeparser
function bbcode($str)
{
$bbcode = array
(
'' => '<b>',
'' => '</b>',
'' => '<u>',
'' => '</u>',
);
$str = str_replace(array_keys($bbcode), array_values($bbcode), $str);
return $str;
}

if($_POST['name'] && $_POST['message']){
	//Define the form variables to be wrote to the text file
	$name = $_POST['name'];
	$message = $_POST['message'];

	//Open the file and add it to the variable $chat
	 $chat = fopen("chat.log", "r+");

	//Write to the text file
	fwrite($chat, $name, $message);

	//Close the file
	fclose($chat);
}
?>
<html>
<head>
<title>Simple Shoutbox</title>
</head>

<body>
<form method="post" action="index.php" name="messageform">

Name:<br>
<input type="text" name="name" id="name" size="50" maxlength="100"><br><br>
Message:<br>
<input type="text" name="message" id="message" size="50" maxlength="100"><br><br>

<input type="submit" value="Submit"><img src="spacer.gif" width=5 height=1 alt="spacer">
<input type="reset" value="Try again"></form>

You can use the following BBcodes:<br><br>
Bold: Text Here<br>
Underline: Text Here

</body>
</html>
That would now leave you with these:

Quote

Notice: Undefined index: name in C:\Program Files\EasyPHP 2.0b1\www\Chat\index.php on line 20

Notice: Undefined index: message in C:\Program Files\EasyPHP 2.0b1\www\Chat\index.php on line 21
I'm not sure if adding the quotes will help a whole lot, but those errors are pretty much worthless, which is why most people run php without turning notices on.
It basically says you didn't explicitly define the 'name' and 'message' keys. If you did, your post wouldn't work, so don't worry about them to much.

p.s. I simplified what Frozen_w did back into one file.

Edited by rc69, 14 May 2007 - 12:25 PM.


#11 kozine

    Young Padawan

  • Members
  • Pip
  • 43 posts

Posted 14 May 2007 - 01:25 PM

Thanks for the reply.

I have now got the problem fixed.

Cheers for the help guys!

#12 Frozen_W

    Young Padawan

  • Members
  • Pip
  • 186 posts
  • Gender:Male
  • Location:Belgium, Gistel
  • Interests:Just gimme a PC and i'm happy!<br />Also my friends, some concerts, parties,...<br />After the pc, the usual stuff;)

Posted 14 May 2007 - 02:52 PM

Lol rc69, I never make things complicated!
They only get hard by themselves;)

LOL
Greetz
PS, if you have any probs, further you've got me on msn!

#13 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 14 May 2007 - 03:30 PM

I don't care to read what you two are getting on about, but you are simply running into small notice errors. They aren't really significant errors, they are just tips generated by PHP saying that you aren't making 100% valid code.

The errors you have is you aren't accessing array keys correctly.

$array[value]; // Don't do
$array['value']; // Correct

See, when it isn't used as a string (as array keys are either strings or numerical values), it is assumed as a constant. Since it is in brackets, and its such a stupidly common mistype by so many programmers, PHP just simply discards it and issues a notice.

The other notice is saying you are trying to access a key in the array that does not exist. This can be solved by using isset(), and only grabbing the value if that function returns true. Otherwise, I wouldn't worry about it.

You can simply ignore most of these notices and turn them off using this.

error_reporting(E_ALL & ~E_NOTICE);

Edit:
Odd... rc's posts weren't there when I looked... Oh well, I just re-instated what he said. :angrylooking:

Edited by Demonslay, 14 May 2007 - 03:33 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users