Jump to content

Archived

This topic is now archived and is closed to further replies.

outsidaz

PHP Form only returns first line

Recommended Posts

I added a PHP Mail contact form on my site and everything works fine except for my comment field. Whenever the user adds a carriage return in the field, only the first line of the comment variable gets returned. Is there any way to prevent the user from entering a carriage return in the form since I have heard that carriage returns are bad. Or, is there a way to allow carriage returns in this variable? Any help would be appreciated. Thanks!

[code]
<?
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$compname = $_POST['compname'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$country = $_POST['country'];
$phone = $_POST['phone'];
$fax = $_POST['fax'];
$email = $_POST['email'];
$comments = $_POST['comments'];
$http_referrer = getenv( "HTTP_REFERER" );

if (!isset($_POST['email'])) {
    header( "Location: $formurl" );
    exit;
}
if (empty($firstname) || empty($lastname) || empty($compname) || empty($address1) || empty($city) || empty($state) || empty($zip) || empty($country) || empty($phone) || empty($email) || empty($comments)) {
   header( "Location: $errorurl" );
   exit;
}
$date = date('l dS \ F Y h:i:s A');
$firstname = strtok( $firstname, "\r\n" );
$lastname = strtok( $lastname, "\r\n" );
$compname = strtok( $compname, "\r\n" );
$address1 = strtok( $address1, "\r\n" );
$address2 = strtok( $address2, "\r\n" );
$city = strtok( $city, "\r\n" );
$state = strtok( $state, "\r\n" );
$zip = strtok( $zip, "\r\n" );
$country = strtok( $country, "\r\n" );
$phone = strtok( $phone, "\r\n" );
$fax = strtok( $fax, "\r\n" );
$email = strtok( $email, "\r\n" );
$comments = strtok( $comments, "\r\n" );
$referral = strtok( $referral, "\r\n" );
if (get_magic_quotes_gpc()) {
    $comments = stripslashes( $comments );

}

$messageproper =

    "This message was sent from: " .
    "$firstname " .
    "$lastname\n" .
    " at " .
    "$date\n" .
    "\n" .
    "$firstname " .
    "$lastname\n" .
    "$compname\n" .
    "$address1\n" .
    "$address2\n" .
    "$city " .
    "," .
    "$state " .
    "$zip\n" .
    "$country\n" .
    "$phone\n" .
    "Fax: " .
    "$fax\n" .
    "$email\n" .
    "\n" .
    "How did you hear about us? " .
    "$referral\n" .
    "\n" .
    "Comments: " .
    "$comments\n";

mail($mailto, $subject, $messageproper, "From: \"$firstname $lastname\" <$email>\r\nReply-To: \"$firstname $lastname\" <$email>\r\nX-Mailer: chfeedback.php 2.04" );
header( "Location: $thankyouurl" );
exit;

?>
[/code]

Share this post


Link to post
Share on other sites
Why are you using the strtok() function on these lines?
[code]<?php
$firstname = strtok( $firstname, "\r\n" );
$lastname = strtok( $lastname, "\r\n" );
$compname = strtok( $compname, "\r\n" );
$address1 = strtok( $address1, "\r\n" );
$address2 = strtok( $address2, "\r\n" );
$city = strtok( $city, "\r\n" );
$state = strtok( $state, "\r\n" );
$zip = strtok( $zip, "\r\n" );
$country = strtok( $country, "\r\n" );
$phone = strtok( $phone, "\r\n" );
$fax = strtok( $fax, "\r\n" );
$email = strtok( $email, "\r\n" );
$comments = strtok( $comments, "\r\n" );
$referral = strtok( $referral, "\r\n" );
?>[/code]

That is why you're only getting the first line of your comments. The strtok() fuction splits a string (str) into smaller strings (tokens), with each token being delimited by any character from token. (taken from the PHP manual) You're telling it to get the first string until the first "\r\n" or new-line character. The newline character is sent when someone presses the return key.

Don't use this function at all any you will be fine.

Ken

Share this post


Link to post
Share on other sites
Thanks, Ken! I copied the script off of some site so I wasn't really sure what the strok was for. Everything works like a charm now.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.