Forum Moderators: coopster

Message Too Old, No Replies

implode - view checkbox value in email

         

webtown06

5:56 pm on Jul 6, 2007 (gmt 0)

10+ Year Member



Hello everyone:

I am trying to use implode function to convert checkboxes' value from array format into a single string. Then I send out an email in which to present the value of checkbox selected by user.

My problem is
a) value displayed in email doesn't go by glue fomat set in implode function.
b) one of the selected values repeat itself (e.g. I select two checkboxes, one of the checkbox value repeat itself one more time)

The delimiter I used in implode function is a comma plus one space (e.g. ', ' )

Please check my code and help me identify the cause of the problem.

HTML code:

<p>Accounts for All Student</p>
<input type="checkbox" name="undergrad_account[]" id="undergrad_account1" value="dial_up" />Dial Up<br />
<input type="checkbox" name="undergrad_account[]" id="undergrad_account2" value="computer_lab" />Computer Lab<br />
<input type="checkbox" name="undergrad_account[]" id="undergrad_account3" value="music/movie download" />Music/Movie Download<br /><br />

<p>Accounts for Graduate Student Only</p>

<input type="checkbox" name="grad_account[]" id="grad_account1" value="dsl_account" onclick="checkGraduateAccount()" />DSL Account<br />
<input type="checkbox" name="grad_account[]" id="grad_account2" value="unix_account" onclick="checkGraduateAccount()" />Unix Account<br />
<input type="checkbox" name="grad_account[]" id="grad_account3" value="web_host" onclick="checkGraduateAccount()" />Web Hosting<br />
<input type="checkbox" name="grad_account[]" id="grad_account4" value="list_server" onclick="checkGraduateAccount()" />List Server<br /><br />

PHP code:
graduate student can select both undergraduate and graduate account

if($class_standing == "undergraduate")
{
$under_gradAccount .= implode(', ', $_POST['undergrad_account']);
$message .= $under_gradAccount."\n";
}

if($class_standing == "graduate")
{
$gradAccount1 .= implode(', ', $_POST['undergrad_account']); //line 132
$gradAccount .= implode(', ', $_POST['grad_account']);
$message .= $gradAccount1 . '<br />'.$gradAccount."\n";
}

I got a warning message says, that "Warning: implode() [function.implode]: Bad arguments. in C:\form_infor.php on line 132".

The actural ouput in email (in this case user selected unix and dsl account,
unix account value repeated itself one more time)
you are: graduate student.
you selected following accounts:
dial_up, computer_lab
unix_accountdsl_account, unix_account

webtown06
7-6-07

cameraman

7:43 pm on Jul 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When you do this:
$message .= $gradAccount1 . '<br />'.$gradAccount."\n";

The period just before the equal causes concatenation, that is, "take whatever's in variable 'message' right now and add the stuff to the right to it". To assign a new value without concatenating, leave the period out:
$message = $gradAccount1 . '<br />'.$gradAccount."\n";

I don't see the cause of the repeat in the code segments you posted, but I suspect you're concatenating in some spot that you don't really intend to - if you look at your other lines you'll probably spot it.

The warning message is because you didn't have any checkboxes checked in the first group - without any at all, $_POST['undergrad_account'] doesn't exist. You can test for it before trying to implode it:
if(is_array [us.php.net]($_POST['undergrad_account']))
$gradAccount1 = implode(', ', $_POST['undergrad_account']);

You also want to keep the absence of $gradAccount1 from causing a problem later on, so you'd want an 'else' to handle the condition of no checkboxes keeping it from getting defined. That brings us to:

if(is_array($_POST['undergrad_account']))
$gradAccount1 = implode(', ', $_POST['undergrad_account']);
else
$gradAccount1 ='';

webtown06

9:07 pm on Jul 7, 2007 (gmt 0)

10+ Year Member



Hello Cameraman:

Thank you for detailed explnation. I checked my code again. I think the cause is due to duplicated variable name for graduate accounts. But I can't figure out the cause for duplicated account option in email.

Before the email portion, I have similar code that will display accounts options right after user submit the form.

I declared a variable at the begining of the code
$gradAccount = "";

PHP code to display account option in confirmation page


if($class_standing == "graduate")
{
print ("You are $class_standing student. You selected the following account(s):<br />");

if(is_array($_POST['undergrad_account']))
{
foreach($_POST['undergrad_account'] as $undergradAccount)
{
print ("$undergradAccount<br />");
}
}

if(is_array($_POST['grad_account']))
{
foreach($_POST['grad_account'] as $gradAccount)
{
print ("$gradAccount<br />");
}
}
}

PHP code for email account option


if($class_standing == "undergraduate")
{
$under_gradAccount .= implode(', ', $_POST['undergrad_account']);
$message .= $under_gradAccount."\n";
}

if($class_standing == "graduate")
{
$gradAccount1 .= implode(', ', $_POST['undergrad_account']);
$gradAccount .= implode(', ', $_POST['grad_account']);
$message .= $gradAccount1 . '<br />'.$gradAccount."\n";
}

webtown06
7-7-07

webtown06

6:34 am on Jul 8, 2007 (gmt 0)

10+ Year Member



Hello everyone:

I found the cause is due to same variable name used for graduate account.

as in first foreach loop


if(is_array($_POST['grad_account']))
{
foreach($_POST['grad_account'] as $gradAccount)
{
print ("$gradAccount<br />");
}
}

variable $grad_account holds last value in checkbox group in foreach loop.

Later I use .= to concatenation new values into $gradAccount. The old value still remain in $gradAccount, hence duplication occured in email.

webtown06
7-7-08