/    Sign up×
Community /Pin to ProfileBookmark

A simple form – ‘Undefined Index’ errors

Hello – php isn’t my thing so I’ve copied a form_mail script which has worked perfectly on my own hosting but it doesn’t work on a new site I’m working on (different server). I get ‘undefined index’ errors inside the form’s text boxes.

I’m guessing this might need a modification for php5 but I’m not sure? Any help would be much appreciated! Here’s the code in it’s entirety as per the site I downloaded it from:

[code=php]<?php
// modify the two lines below
$emailAddress = “[email protected]”; // your email address
$thankyouPage = “http://www.site.co.uk”; // put in a url (or relative page) to go to a Thank You page
// modify the two lines above
session_start();
if (!empty($_POST)) {
foreach ($_POST as $key=>$value) {
$_POST[$key] = stripslashes($_POST[$key]);
$_POST[$key] = htmlspecialchars($_POST[$key],ENT_QUOTES);
}
}
if (isset($_POST[‘send’]) AND isset($_SESSION[‘msgCount’])) {
if (!preg_match(“(^[-w.]+@([-a-z0-9]+.)+[a-z]{2,4}$)i”,$_POST[’email’])) $alert = “You have entered an invalid email address.”;
if ($_SESSION[‘msgCount’] >= “3”) $alert = “Only 3 messages can be sent per session.”;
if (empty($alert)) {
$_SESSION[‘msgCount’]++;
putenv(‘TZ=EST5EDT’); // eastern time
$headers = ‘MIME-Version: 1.0’ . “rn”;
$headers .= ‘Content-type: text/html; charset=iso-8859-1′ . “rn”;
$headers .= “From: <“.$_POST[’email’].”>rn”;
$message = “<table cellpadding=’5′ border=’1′>”;
foreach ($_POST as $key => $value)
if (!preg_match(“(^send)”,$key)) {
$value = wordwrap($value,65,”<br />”);
$message .=”<tr><td><b>$key</b></td><td>$value</td></tr>”;
}
$message .= “</table>”;
$message .= “<br />Time of the message: “.date(” F d h:ia”).”<br />”;
$message .= “IP Address: “.$_SERVER[‘REMOTE_ADDR’].”<br />”;
$message .= “Hostname: “.gethostbyaddr($_SERVER[‘REMOTE_ADDR’]).”<br />”;
$subject = $_SERVER[‘HTTP_HOST’].” Message”;
mail($emailAddress,$subject,$message,$headers);
if (!empty($thankyouPage)) {
header(‘location: ‘.$thankyouPage);
die();
}
unset($_POST);
$alert = “Your message has been sent.”;
}
}
if (!isset($_SESSION[‘msgCount’])) $_SESSION[‘msgCount’] = 0;
?>
<html>
<head>
</head>
<body>
<form method=”post” action=”<?php echo $_SERVER[‘SCRIPT_NAME’]; ?>”>
<div style=”text-align: center; width: 500px; margin: 50px auto 0px auto; border: solid 1px black;”>
<br>
Send us a message<br><br>
your name<br><input type=”text” style=”width: 330px;” name=”name” value=”<?php echo $_POST[‘name’]; ?>” maxlength=”50″><br><br>
your email address<br><input type=”text” style=”width: 330px;” name=”email” value=”<?php echo $_POST[’email’]; ?>” maxlength=”50″><br><br>
your message<br><textarea name=”content” style=”width: 330px; height: 100px;” rows=”6″ cols=”80″><?php echo $_POST[‘content’]; ?></textarea>
<br><br>
<input type=”submit” name=”send” value=”submit”>
<br /><br />&nbsp;
</div>
</form>
<?php if (isset($alert)) echo “<script type=’text/javascript’>alert(‘$alert’);</script>”; ?>
</body></html>[/code]

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@SyCoAug 13.2009 — Quickest fix is to add a @ to the front of the variable. eg
[code=php]
<?php echo @$_POST['email']; ?>[/code]


Look for the line number of the error and the name of the variable.
Copy linkTweet thisAlerts:
@whoauthorAug 13.2009 — Thanks for that SyCo - much appreciated ?

That's cleared up all the error messages appearing in the text boxes.


Now I have another problem... although the form appears to submit OK, the emails aren't getting delivered and there aren't any error messages (I've made sure the email address is valid by the way)

Any ideas about that?

Many thanks in advance
Copy linkTweet thisAlerts:
@SyCoAug 13.2009 — First thing to check is the POST and SESSION vars are as expected, so add this to the very top of the page inside the <?php tags

[code=php]<?php
echo '<pre>';
print_r(@@$_POST);
print_r(@$_SESSION);
echo '</pre>';

// modify the two lines below
$emailAddress = "[email protected]"; // your email address
$thankyouPage = "http://www.site.co.uk"; // put in a url (or relative page) to go to a Thank You page

...etc
[/code]


Check that '$_POST['send'] and $_POST['msgCount'] are in the two printed arrays. This code
[code=php]if (isset($_POST['send']) AND isset($_SESSION['msgCount'])) {[/code]
requires them.


Next check that mail() is working.

Create a new script and add this to it.

[code=php]<?php
mail('[email protected]','test subject','test message body');
?>[/code]


Just that nothing else and visit it in a browser. You should get the test email. If you don't you'll need to contact your host to see why mail() is broken.
Copy linkTweet thisAlerts:
@whoauthorAug 14.2009 — Thanks for your help again SyCo!

Firstly, I tried adding the code to add the POST and SESSION variables and all I got at the top of the pages was:

Array

(

)

I checked the mail function and it's available. The code you gave me got this error: Warning: mail() [function.mail]: "sendmail_from" not set in php.ini or custom "From:" header missing in [I]etc. etc...[/I], but perhaps I didn't implement it properly? I used the following code that I found via Uncle Google and it confirmed that mail was available but could this give a false answer?:
[CODE]if ( function_exists( 'mail' ) )
{
echo 'mail() is available';
}
else
{
echo 'mail() has been disabled';
} [/CODE]


So, I thought I'd include the slightly modified code in its entirety in case there's something obvious that I've done wrong. The hosts are adamant that simple php contact forms will work on their server..
[CODE]
<?php
// modify the two lines below
$emailAddress = "[email protected]"; // your email address
$thankyouPage = "http://www.website.co.uk"; // put in a url (or relative page) to go to a Thank You page
// modify the two lines above
session_start();
if (!empty($_POST)) {
foreach ($_POST as $key=>$value) {
$_POST[$key] = stripslashes($_POST[$key]);
$_POST[$key] = htmlspecialchars($_POST[$key],ENT_QUOTES);
}
}
if (isset($_POST['send']) AND isset($_SESSION['msgCount'])) {
if (!preg_match("(^[-w.]+@([-a-z0-9]+.)+[a-z]{2,4}$)i",$_POST['email'])) $alert = "You have entered an invalid email address.";
if ($_SESSION['msgCount'] >= "3") $alert = "Only 3 messages can be sent per session.";
if (empty($alert)) {
$_SESSION['msgCount']++;
putenv('TZ=EST5EDT'); // eastern time
$headers = 'MIME-Version: 1.0' . "rn";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";
$headers .= "From: <".$_POST['email'].">rn";
$message = "<table cellpadding='5' border='1'>";
foreach ($_POST as $key => $value)
if (!preg_match("(^send)",$key)) {
$value = wordwrap($value,65,"<br />");
$message .="<tr><td><b>$key</b></td><td>$value</td></tr>";
}
$message .= "</table>";

$message .= "<br />Time of the message: ".date(" F d h:ia")."<br />";
$message .= "IP Address: ".$_SERVER['REMOTE_ADDR']."<br />";
$message .= "Hostname: ".gethostbyaddr($_SERVER['REMOTE_ADDR'])."<br />";
$subject = $_SERVER['HTTP_HOST']." Message";
mail($emailAddress,$subject,$message,$headers);
if (!empty($thankyouPage)) {
header('location: '.$thankyouPage);
die();
}
unset($_POST);
$alert = "Your message has been sent.";
}
}
if (!isset($_SESSION['msgCount'])) $_SESSION['msgCount'] = 0;
?>
<html>
<head>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
<div style="text-align: center; width: 500px; margin: 50px auto 0px auto; border: solid 1px black;">
<br>
Send us a message<br><br>
your name<br><input type="text" style="width: 330px;" name="name" value="<?php echo @$_POST['name']; ?>" maxlength="50"><br><br>
your email address<br><input type="text" style="width: 330px;" name="email" value="<?php echo @$_POST['email']; ?>" maxlength="50"><br><br>
your message<br><textarea name="content" style="width: 330px; height: 100px;" rows="6" cols="80"><?php echo @$_POST['content']; ?></textarea>
<br><br>
<input type="submit" name="send" value="submit">
<br /><br />&nbsp;
</div>
</form>
<?php if (isset($alert)) echo "<script type='text/javascript'>alert('$alert');</script>"; ?>
</body></html>[/CODE]


So frustrating when it worked perfectly on a couple of other sites...
Copy linkTweet thisAlerts:
@metal_royAug 14.2009 — Try using some IDE like nusphere or Eclipse. You will be able to trace your bugs in no time as it allows you to debug the script step by step.
Copy linkTweet thisAlerts:
@SyCoAug 14.2009 — You need to contact your server's administrator. Sorry.
Copy linkTweet thisAlerts:
@whoauthorAug 14.2009 — OK, thanks a lot for your suggestions SyCo and metal_roy. I'll see what I can sort out ?
×

Success!

Help @who spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...