Help - email form/script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
blue sam3
Forum Newbie
Posts: 3
Joined: Fri Dec 04, 2009 11:56 am

Help - email form/script

Post by blue sam3 »

I have got a form that uses PHP to send an email to a 'contact us' address. However, it doesn't always work. The majority of the time, it works perfectly, but when one specific form element (a multi-select box) has anything selected in it, it returns the following error:
Warning: implode() [function.implode]: Invalid arguments passed in /home/dysaili1/public_html/train.php on line 43

Warning: Cannot modify header information - headers already sent by (output started at /home/dysaili1/public_html/train.php:43) in /home/dysaili1/public_html/train.php on line 50
and does not send the email. The element that causes the problem (on line 43) is:

Code: Select all

( empty( $data['help'] )?" -not selected -": implode( "," , $data['help'] ) )
Line 50 is:

Code: Select all

       header( "Location: " . $_SERVER["PHP_SELF"] . "?thankyou" );

This is the full code:

Code: Select all

<?php
function htmlhead()
{
 
    ?>
<html>
<head>
<title>Registration Sent</title>
</head>
 
<body>
<?php
}
 
function foot()
{
 
    ?>
<form method="LINK" action="index.htm"><input type="submit" value="Return to home page"></form>
</body>
</html><?php
}
 
if ( !empty( $_POST ) ) {
    $to = "training@dysailing.com";
    $subject = "DYS Training Event Registraion";
 
    function stripandfilterhtml( $string )
    {
        if ( empty( $string ) )
            return " -empty- ";
 
        if ( is_array( $string ) )
            $string = array_map( "stripandfilterhtml" , $string );
        else
            $string = htmlspecialchars( strip_tags( $string ) ) ;
        return $string;
    }
 
    $data = array_map( "stripandfilterhtml" , $_POST );
 
    $email = $data['email'] ;
    $message = "Competitor Info: <br>Helm Name:" . $data['helmname'] . "<br>Email:" . $data['email'] . "<br>Club:" . $data['club'] . "<br>Boat Class:" . $data['class'] . "<br>Sail Number:" . $data['sailno'] . "<br>Event: " . $data['event'] . "<br><br>Parents Info:<br>Name:" . $data['name'] . "<br>Can Help With:" . ( empty( $data['help'] )?" -not selected -": implode( "," , $data['help'] ) ) . "<br>Contact Number:" . $data['telno'] . " <br>Email Address: " . $data['helperemail'];
 
    $headers = "From: $email";
    $headers = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $sent = mail( $to, $subject, $message, $headers ) ;
    if ( $sent ) {
        header( "Location: " . $_SERVER["PHP_SELF"] . "?thankyou" );
    } else {
        header( "Location: " . $_SERVER["PHP_SELF"] . "?email_failed" );
    }
}
 
htmlhead();
 
if ( isset( $_GET["thankyou"] ) ) {
 
    ?>Registration Received<?php
}
 
if ( isset( $_GET["email_failed"] ) ) {
 
    ?>There has been an issue with your registration, please try again. If the problem persists, please inform us through the contact webmaster link on the home page.<?php
}
foot();
 
?>
Any ideas?
hostingon
Forum Newbie
Posts: 6
Joined: Fri Dec 04, 2009 5:00 pm
Location: USA

Re: Help - email form/script

Post by hostingon »

Hi man - I think that problem is just in that you want to implode $data['help'], but $data['help'] is not array.
Please test this
print_r($data['help']);
Is $data['help'] array or not? :?:
blue sam3
Forum Newbie
Posts: 3
Joined: Fri Dec 04, 2009 11:56 am

Re: Help - email form/script

Post by blue sam3 »

Changed it, now returns the following:

Code: Select all

 
event presentation
Warning: Cannot modify header information - headers already sent by (output started at /home/dysaili1/public_html/train.php:43) in /home/dysaili1/public_html/train.php on line 50
 
EDIT:It does send the email though, but the 'help' field returns 1, rather than it's value.
blue sam3
Forum Newbie
Posts: 3
Joined: Fri Dec 04, 2009 11:56 am

Re: Help - email form/script

Post by blue sam3 »

Anyone?
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Help - email form/script

Post by Apollo »

The implode function will only return a string if you give it an array. In your case, $data['help'] is not an array.

And the warning this generates (or the output you print with print_r) is outputted by PHP before the header you write at line 50. This is where the 2nd warning comes from: headers need to be written before any regular HTML output. And errors or warnings or debug messages are also output.
Post Reply