Help with some basic PHP

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
diggity_dang
Forum Newbie
Posts: 2
Joined: Sat Mar 27, 2010 5:50 pm

Help with some basic PHP

Post by diggity_dang »

Hi there,

I'm new to the forum (and to PHP for that matter). I have set up an html page where a contact form exists, along with a checklist of 71 reports. I want a user to select the reports they are interested in and input their contact information. I have set up a .php mailer, so it delivers all of the information within the contact page to an email address, where I will email the user the reports manually. Everything works and the email address receives all of the correct information, but there is only one esthetic issue. If the user were to select the 1st and the 71st report, the email delivered has the name of the report, followed by 70 blank lines, before showing the 71st report.

If anyone can help, your assistance would be very much appreciated. Thanks in advance... here's the code I have:


---------------------

Code: Select all

<?php


if(isset($_POST['submit'])) {

$to = "diggity_dang@gmail.com";
$subject = "Request for Case Studies";
$name = $_POST['name'];
$phone = $_POST['phone'];
$cell = $_POST['cell'];
$email = $_POST['email'];
$comments = $_POST['comments'];
$report1 = $_POST['report1'];
$report2 = $_POST['report2'];
$report3 = $_POST['report3'];
$report4 = $_POST['report4'];
$report5 = $_POST['report5'];
$report6 = $_POST['report6'];
$report7 = $_POST['report7'];
$report8 = $_POST['report8'];
$report9 = $_POST['report9'];
$report10 = $_POST['report10'];
$report11 = $_POST['report11'];
$report12 = $_POST['report12'];
$report13 = $_POST['report13'];
$report14 = $_POST['report14'];
$report15 = $_POST['report15'];
$report16 = $_POST['report16'];
$report17 = $_POST['report17'];
$report18 = $_POST['report18'];
$report19 = $_POST['report19'];
$report20 = $_POST['report20'];
$report21 = $_POST['report21'];
$report22 = $_POST['report22'];
$report23 = $_POST['report23'];
$report24 = $_POST['report24'];
$report25 = $_POST['report25'];
$report26 = $_POST['report26'];
$report27 = $_POST['report27'];
$report28 = $_POST['report28'];
$report29 = $_POST['report29'];
$report30 = $_POST['report30'];
$report31 = $_POST['report31'];
$report32 = $_POST['report32'];
$report33 = $_POST['report33'];
$report34 = $_POST['report34'];
$report35 = $_POST['report35'];
$report36 = $_POST['report36'];
$report37 = $_POST['report37'];
$report38 = $_POST['report38'];
$report39 = $_POST['report39'];
$report40 = $_POST['report40'];
$report41 = $_POST['report41'];
$report42 = $_POST['report42'];
$report43 = $_POST['report43'];
$report44 = $_POST['report44'];
$report45 = $_POST['report45'];
$report46 = $_POST['report46'];
$report47 = $_POST['report47'];
$report48 = $_POST['report48'];
$report49 = $_POST['report49'];
$report50 = $_POST['report50'];
$report51 = $_POST['report51'];
$report52 = $_POST['report52'];
$report53 = $_POST['report53'];
$report54 = $_POST['report54'];
$report55 = $_POST['report55'];
$report56 = $_POST['report56'];
$report57 = $_POST['report57'];
$report58 = $_POST['report58'];
$report59 = $_POST['report59'];
$report60 = $_POST['report60'];
$report61 = $_POST['report61'];
$report62 = $_POST['report62'];
$report63 = $_POST['report63'];
$report64 = $_POST['report64'];
$report65 = $_POST['report65'];
$report66 = $_POST['report66'];
$report67 = $_POST['report67'];
$report68 = $_POST['report68'];
$report69 = $_POST['report69'];
$report70 = $_POST['report70'];
$report71 = $_POST['report71'];


 
$body = "From: $name\n Phone: $phone\n Email: $email\n Comments: $comments\n
Requested Reports: 
$report1\n $report2\n $report3\n $report4\n $report5\n $report6\n $report7\n $report8\n $report9\n $report10\n $report11\n $report12\n $report13\n $report14\n $report15\n $report16\n $report17\n $report18\n $report19\n $report20\n $report21\n $report22\n $report23\n $report24\n $report25\n $report26\n $report27\n $report28\n $report29\n $report30\n $report31\n $report32\n $report33\n $report34\n $report35\n $report36\n $report37\n $report38\n $report39\n $report40\n $report41\n $report42\n $report43\n $report44\n $report45\n $report46\n $report47\n $report48\n $report49\n $report50\n $report51\n $report52\n $report53\n $report54\n $report55\n $report56\n $report57\n $report58\n $report59\n $report60\n $report61\n $report62\n $report63\n $report64\n $report65\n $report66\n $report67\n $report68\n $report69\n $report70\n $report71\n ";
 
echo "Thank you for your interest!  We will send you all of your requested reports via email shortly.";
mail($to, $subject, $body);


}
?>
Alkis
Forum Commoner
Posts: 31
Joined: Fri Mar 26, 2010 8:41 am

Re: Help with some basic PHP

Post by Alkis »

The reason you are getting the blank lines is the new line character on every $report* variable you output. No matter if that variable has a value,
the "\n" character after it is causing a new line.

Options:

1) Using if statements add each $report* variable with a new line to the $body, only if it has a value

2) A bit more advanced method (but way more faster and robust):

Name each report form field (report1, report2, report3 etc...), with a single name: report[] (yes with the brackets), and leave each fields' value as you have them.

Then after the form post, you will have a post variable: $_POST['report'], with as you maybe guessed, is an array containing only the fields and its values that the user selected in the form.

So, in your case, the body would be created like this (and no worries for blank lines):

Code: Select all

$body = "From: $name\n Phone: $phone\n Email: $email\n Comments: $comments\n
Requested Reports: ".
( implode("\n", $_POST['report'])  )."\n";
Of course it is better to check what's in the $_POST['report'] for security reasons first, and then assign it to a variable e.g. $report ant use that on the implode().
Last edited by Alkis on Sat Mar 27, 2010 9:11 pm, edited 1 time in total.
diggity_dang
Forum Newbie
Posts: 2
Joined: Sat Mar 27, 2010 5:50 pm

Re: Help with some basic PHP

Post by diggity_dang »

Thank you so much for the fast reply, Alkis - I will give it a shot and see what happens!! I appreciate it!!
Alkis
Forum Commoner
Posts: 31
Joined: Fri Mar 26, 2010 8:41 am

Re: Help with some basic PHP

Post by Alkis »

I hope it'll work for you, let me know.
Post Reply