TinyPortal

Development => Support => Topic started by: Sander on March 11, 2007, 06:16:19 PM

Title: Undefined index problems with PHP
Post by: Sander on March 11, 2007, 06:16:19 PM
Hi,

I've modified a contact form script into a newsletter subscribing script and it works flawless. Only everytime I open the (php) article it writes some errors to the error log. The same happens with the original script so it's not the changes I have made. To make things clear I will post the code and errors.

Code:
Quote
$contact_name = $_POST['FullName'];
$contact_email = $_POST['EmailAddress'];
$contact_subscribe = $_POST['Subscribe'];
$mydate = date ( 'l, F d Y g:i A',time()+240 );
// where to send e-mail to
$to = 'removed@removed.com';
// e-mail subject
$subject = "Planet Core Newsletter";
// e-mail message
$message = "You have received a new subscription:\r\n"
."----------------------------------------------------------------\r\n"
."Contact Name: $contact_name\r\n"
."E-mail: $contact_email\r\n"
."Subscribe: $contact_subscribe\r\n"
."Submitted: $mydate\r\n"
."Form Address: {$_SERVER['SERVER_NAME']}{$_SERVER['REQUEST_URI']}";
$headers = "From: $contact_name <$contact_email>\n"
."Reply-To: $contact_email\n"
."X-Mailer: PHP/".phpversion();
// check for validation, then send the e-mail
if(empty($contact_name) || empty($contact_email)) {
echo '<p>Subscribe to our newsletter!</p>
<form method="post" action="">
<table id="Form-Details">
<tbody>

<tr>
      <td height="22"<div align="left">Name :

            <input type="text" name="FullName" size="25">
      </div></td>
    </tr>
    <tr>
      <td height="22" colspan="2"><div align="left">E-mail :</font>
           
            <input type="text" name="EmailAddress" size="25">
      </div></td>
      </tr>
    <tr>
      <td height="21" colspan="2">
            <div align="left">
              <input type="radio" value="subscribe" checked name="Subscribe" checked="checked">
            Subscribe

            <input type="radio" name="Subscribe" value="unsubscribe">
            Unsubscribe</font>

            <input type="submit" value="Send">
            <input type="reset" value="Reset">
            </div></td>
      </tr>
</tbody>
</table>
</form>';
} elseif(!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $contact_email)) {
echo "<p>ERROR: Please enter a valid e-mail address. <a href='index.php'>Ã,«Ã,«</a></p>";
} else {
mail( $to, $subject, $message, $headers );
if($_POST['Subscribe'] == subscribe) {
echo "<p>Hi $contact_name,

$contact_email is added to the Planet Core mailing database.

<a href='index.php'>Ã,«Ã,«</a>";
} elseif($_POST['Subscribe'] == unsubscribe) {
echo "<p>Hi $contact_name,

$contact_email is removed from the Planet Core mailing databse.

<a href='index.php'>Ã,«Ã,«</a>";
}
}

Errors: (paths are changed by me)
Quote
8: Undefined index: Subscribe
File: /path_to_domain/public_html/Themes/default/TPortal.template.php (eval?)
Line: 3

8: Undefined index: EmailAddress
File: /path_to_domain/public_html/Themes/default/TPortal.template.php (eval?)
Line: 2

8: Undefined index: FullName
File: /path_to_domain/public_html/Themes/default/TPortal.template.php (eval?)
Line: 1

As you can see the problems are the first 3 lines in the script. I only have very little knowledge of PHP so I don't know what the problem really is. So that's why I'm here now ;)
Title: Re: Undefined index problems with PHP
Post by: JPDeni on March 11, 2007, 06:29:04 PM
Change your first three lines to


isset($_POST['FullName']) ? $contact_name = $_POST['FullName'] : $contact_name = '';
isset($_POST['EmailAddress']) ? $contact_email = $_POST['EmailAddress'] : $contact_email = '';
isset($_POST['Subscribe']) ? $contact_subscribe = $_POST['Subscribe'] : $contact_subscribe = '';


The problem is when you first open the article and nothing has been posted. Since $_POST['FullName'] and the others have not been defined, you get the error. By using the "isset" function, it checks first to see if the index has been defined and only uses it if it has been.
Title: Re: Undefined index problems with PHP
Post by: Sander on March 11, 2007, 06:40:18 PM
JPDeni,

I can't thank you enough! Thanks for the quick solution!
Title: Re: Undefined index problems with PHP
Post by: JPDeni on March 11, 2007, 07:17:12 PM
Glad to help. :) I ran into the same problem and often forget about it when I write other code, too. It's one of those things that I recognize right off the bat now, though. :)