FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » General » PHP discussions » Undefined Index Troubles... [Help Please]
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Undefined Index Troubles... [Help Please] [message #26995] Wed, 24 August 2005 10:46 Go to next message
Linguistic is currently offline  Linguistic   Australia
Messages: 3
Registered: August 2005
Location: Australia
Karma: 0
Junior Member
I'm using a simple form using basic HTML and PHP. Everytime I run the script, I get this message "Notice: Undefined index: action in C:\Inetpub\habboresort\contact.php on line 49" I have no idea what is wrong with my script either. The code as follows:
<?php
//Contact Script Made By Jordan Craig
//Define Variables
$your_email="liquid-crystal(at)hotmail(dot)com";
//Show Mail Form
function showForm(){
echo "<form name='contact' method='post' action='contact.php?action=sendEmail' onSubmit='return FormCheck()'>\n"
 ."<table width='40%' border='1' cellpadding='3' cellspacing='3' bordercolor='#000000' style='border-style: dashed;'>\n"
 ."<tr>\n"
 ."<td width='40%' bgcolor='#999999'><strong>Your Habbo Name: </strong><em>Please provide your real Habbo name.</em></td>\n"
 ."</tr><tr>\n"
 ."<td bgcolor='#CCCCCC'><input type='text' name='name' size='60'></td>\n"
 ."</tr><tr>\n"
 ."<td bgcolor='#999999'><strong>Your Email: </strong><em>Please provide a valid Email address.</em></td>\n"
 ."</tr><tr>\n"
 ."<td valign='top' bgcolor='#CCCCCC'><input type='text' name='email' size='60'></td>\n"
 ."</tr><tr>\n"
 ."<td valign='top' bgcolor='#999999'><strong>Subject: </strong><em>Please select the most relevant subject for your query.</em></td>\n"
 ."</tr><tr>\n"
 ."<td valign='top' bgcolor='#CCCCCC'><select name='subject' size='0'>
    <option>Option Goes Here</option>
    <option>Option Goes Here</option>
    <option>Option Goes Here</option>
    <option>Option Goes Here</option>
    <option>Option Goes Here</option>
    <option>Option Goes Here</option>
  </select></td>\n"
 ."</tr><tr>\n"
 ."<td valign='top' bgcolor='#999999'><strong>Your Message: </strong><em>Please provide any additional information via this field.</em></td>\n"
 ."</tr><tr>\n"
 ."<td valign='top' bgcolor='#CCCCCC'><textarea name='message' rows='5' cols='45'></textarea></td>\n"
 ."</tr><tr>\n"
 ."<td align='right' valign='top' bgcolor='#999999'><input name='submit' type='submit' value='Submit Query'>\n<input name='reset' type='reset' value='Reset All Fields'></td>\n"
 ."</tr></table>\n";
}
//Send Email
function sendEmail(){
global $your_email,$name,$email,$subject,$message;
if ( !$name | !$email | !$subject | !$message ){
header ("Location: contact.php");
}else{
$email2 = "$email ($name)";
mail ( $your_email, $subject, $message, "From: $email2");
echo "<h1>Your query was successfully sent!</h1>\n"
 ."</head><body>\n";
}
}
//Switch Statement
switch ( $_GET['action'] ){
case 'sendEmail':
sendEmail();
break;
default:
showForm();
}
?>
Any help would be much appreciated. Smile


Null
Re: Undefined Index Troubles... [Help Please] [message #27003 is a reply to message #26995] Wed, 24 August 2005 13:28 Go to previous messageGo to next message
Ilia is currently offline  Ilia   Canada
Messages: 13241
Registered: January 2002
Karma: 0
Senior Member
Administrator
Core Developer
It means you are using $_GET['action'] without it having a value, probably on the 1st load of the form.

FUDforum Core Developer
Re: Undefined Index Troubles... [Help Please] [message #27004 is a reply to message #26995] Wed, 24 August 2005 13:31 Go to previous messageGo to next message
Linguistic is currently offline  Linguistic   Australia
Messages: 3
Registered: August 2005
Location: Australia
Karma: 0
Junior Member
Yeah, now I am using
//Switch Statement
if(array_key_exists('action', $_GET)){
switch ( $_GET['action'] ){
case 'sendEmail':
sendEmail();
break;
default:
showForm();
}
}else{
showForm();
}
But now it seems I have a problem with it sending the form... The code shown when I press submit is:
Warning:  Cannot modify header information - headers already sent by (output started at C:\Inetpub\habboresort\contact.php:16) in C:\Inetpub\habboresort\contact.php on line 124
I am stumped yet again.


Null
Re: Undefined Index Troubles... [Help Please] [message #31405 is a reply to message #27004] Fri, 21 April 2006 08:59 Go to previous messageGo to next message
richardlynch is currently offline  richardlynch   United States
Messages: 8
Registered: April 2006
Karma: 0
Junior Member
A minimal HTTP document from Server to Browser might look like:

---------------------- this line is not part of it ------------
200 Success
Content-type: text/html
Content-length: 4325
X-Powered-by: PHP

<html><body>Hello World</body></html>
---------------------- this line is not part of it ------------

Now, remember, this is going out piece by piece, line by line, letter by letter.

At any point in time, in slow-motion instant replay, maybe only SOME of the lines have gone from the server to the browser.

Look closely at the data above.

You see that blank line after the X-Powered-by: PHP and before the HTML tags?

*THAT* line is super crucial.

THAT line is a blank line, and the very first blank line indicates to the browser that the "headers" (those lines above the blank) are "done" and the HTML is about to start.

Now, you don't normally see the headers, though Firefox has a nifty plug-in to show them which you should get. You are using Firefox,right?

Anyway, here's the thing.

As soon as PHP has to send out some content -- some part of the HTML (like <html> usually) or even a blank line *before* your HTML, then it *has* to send all the headers out, and the blank line to indicate the END of the headers.

So what the error message is telling you is that on line 16 of your script, you printed/echo'ed something out, or you have a blank line, or you have an error message of some kind printing something out.

And when that happened, PHP sent all the headers, and then a blank line, and then started sending out your HTML (or blank lines or whatever you have on line 16).

Now, come line 124, you're trying to send a header out using the header function.

Only problem is, it's WAY too late.

The headers all got sent out at line 16, and PHP has already started sending content.

If line 16 is just a bad line or something, fix it and this problem goes away.

Otherwise, you can do one of two things:

1. The lazy, disorganized, quick, dirty solution is to turn on "output buffering" with http://php.net/ob_start (or in php.ini)

2. Re-arrange your code logically so that the HEADERS (which come oat the "head" of an HTTP stream, are all worked out at the tip-top of the document, and the actual HTML all comes later. This will make your code more organized and more logical, but requires actual thinking and work.

I always recommend #2, personally. Smile
Re: Undefined Index Troubles... [Help Please] [message #31409 is a reply to message #26995] Fri, 21 April 2006 09:15 Go to previous message
Linguistic is currently offline  Linguistic   Australia
Messages: 3
Registered: August 2005
Location: Australia
Karma: 0
Junior Member
Hi Richard!

Thank you very much for the extensive explanation of what was causing the problem. Although I had already fixed that problem by turning on output buffering, I was unsure of what was causing the problem. Thank you very much for enlightening me.

Kind regards,
Jordan


Null
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: strange behaviour - fwrite
Next Topic: invalid argument
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Dec 11 21:23:37 GMT 2024

Total time taken to generate the page: 0.02171 seconds