Jump to content

[SOLVED] Looping an array


NickG21

Recommended Posts

hey everyone i have a set of radio buttons and a set of checkboxes that have the same names for each group.

ServiceName = radiobutton group

reasons = checkbox group

 

When the user selects a radio button and/or  one or many checkboxes the value is supposed to be passed into a variable and sent in an e-mail.  the only problem is that the values are not taken.  i have another checkbox that is individual in it's name on the page and the value is passed fine from it.  below is my code.  if anyone has an idea of how i can retrieve these values it would be appreciated.  thanks in advance.

 

this is only the PHP:

<?php

if (isset($_POST['submit'])) {
$company = $_POST['CompanyName'];
$name = $_POST['realname'];
$email = $_POST['email'];
$domainName = $_POST['ListorDomainName'];
$username = $_POST['Username'];
$password = $_POST['Password'];
$credit = $_POST['CreditCardNumber'];
$resend = $_POST['Resend'];
$reasons = $_POST['Reasons'];
$subject = 'Cancellation Request';
$ServiceName = $_POST['ServiceName'];
$to = '';

if (empty($username) && empty($password)){
$to = 'mail1@mail.com';
$msg .= "Company: $company\n";
$msg .= "Name: $name\n";
$msg .= "E-Mail: $email\n";
$msg .= "Domain Name: $domainName\n";
$msg .= "Credit Card: $credit\n";
$msg .= "ReSend Manual: $resend\n";
$msg .= "Service To Cancel: $ServiceName \n";
$msg .= "Reasons For Cancelling: $reasons\n";

}else{
$to = 'mail2@mail.com;
$msg .= "Company: $company\n";
$msg .= "Name: $name\n";
$msg .= "E-Mail: $email\n";
$msg .= "Domain Name: $domainName\n";
$msg .= "Credit Card: $credit\n";
$msg .= "ReSend Manual: $resend\n";
$msg .= "Service To Cancel: $ServiceName \n";
$msg .= "Reasons For Cancelling: $reasons\n";
}
mail($to, $subject, $msg, "From: $email");
header("location: http://.../thankyou.html");
}

?>

Link to comment
Share on other sites

Make sure the field name in the html is "reasons[]"

<?php
$reasons = implode(', ',$_POST['Reasons']);
?>

 

And as for the radio buttons, a group of radio buttons all should have the same name, and only the value of the selected on e is returned.  So anything with a single group of radio buttons should work fine.

Link to comment
Share on other sites

bwoch, when i try using the implode command, i get the error:

Warning: implode(): Bad arguments. in .../CancelRequest.php on line 12

Warning: Cannot modify header information - headers already sent by (output started at /home/web/.../CancelRequest.php:12) in /home/web/.../CancelRequest.php on line 41

 

 

 

here is the code for the part of the form with radio buttons and checkboxes.

 

<input name="ServiceName" id="request" type="radio" value="Announcement Email
List Hosting"> Announcement Email List Hosting<br>
					<input name="ServiceName" id="request" type="radio" value="Discussion Email
List Hosting"> Discussion Email List Hosting<br>
					<input name="ServiceName" id="request" type="radio" value="Web Hosting"> Web
Site Hosting<br>
					<input name="ServiceName" id="request" type="radio" value="Pay Per Click Bid
Management"> Pay Per Click Bid Management<br>

					<input name="ServiceName" id="request" type="radio" value="Dedicated Server">
Dedicated Server Hosting<br>
					<input name="ServiceName" id="request" type="radio" value="Enterprise Email
Filtering"> Enterprise Email Filtering<br>
					<input name="ServiceName" id="request" type="radio" value="Email / Webmail
Accounts"> Email / Webmail Accounts<br>
					<input name="ServiceName" id="request" type="radio" value="Other"> Other
<input name="ServiceName" id="request" size="30">	
			  </td>

			</tr>
			<tr>
				<td class="header">Reasons for Canceling <span class="note"
style="padding-left: 1em; font-size: 80%;">(Check all that apply.)</span> 
</td>
			</tr>
			<tr>
			  	<td class="oneColumnOptions">
			  		<input type="checkbox" name="Reasons" id="request" value="NotUsing Service"> Not Using Service<br>

			  		<input type="checkbox" name="Reasons" id="request"
value="Dissatisfied with Features of Service" id="request"> Dissatisfied with Features 
of Service<br>
					<input type="checkbox" name="Reasons"
value="Ineffective Business Solution" id="request"> Ineffective Business Solution<br>
<!-- specifically for mailing list -->				
					<input type="checkbox" name="Reasons" id="request" value="Only Send Occational Mailings">					    
					Only Send Occasional Mailings (<span class="note"
style="font-weight: normal;">For Mailing List Hosting only</span>)<br>
                        <input type="checkbox" name="Reasons" id="request" value="Poor Delivery of Email Newsletter to Subscribers">
Poor Delivery of Email Newsletter to Subscribers (<span class="note"
style="font-weight: normal;">For Mailing List Hosting only</span>)<br>

<!-- -->		
					<input type="checkbox" name="Reasons" id="request" value="Found a 
Less Expensive Alternative"> Found a Less Expensive Alternative<br>
					<input type="checkbox" name="Reasons" id="request" value="Went
with an In-house Solution"> Went with an In-house Solution<br>
					<input type="checkbox" name="Reasons" id="request"
value="Technical Issues"> Technical Issues<br>
					<input type="checkbox" name="Reasons" id="request" value="Too
Difficult to Use Service"> Too Difficult to Use Service<br>

					<input type="checkbox" name="Reasons" id="request" 
value="Dissatisfied with Technical Support"> Dissatisfied with Technical
Support<br>

					<input type="checkbox" name="Reasons" id="request" value="Poor
Customer Service"> Poor Customer Service<br>
					<input type="checkbox" name="Reasons" id="request" value="Poor
Billing Process"> Poor Billing Process<br>
					<input type="checkbox" name="Reasons" id="request" value="Went
Out of Business"> Went Out of Business<br>
					<input type="checkbox" name="Reasons" id="request" value="Sold
Company"> Sold Company<br>
					<input type="checkbox" name="Reasons" id="request" value="Other"> 
Other <input name="OtherReasons" id="request" size="30">

Link to comment
Share on other sites

You need to change the name of your checkbox from "Reasons" to "Reasons[]", then you can use the implode() function to get which boxes were checked. Make sure you use an "if isset" before the implode, incase no checkboxes were checked. When no boxes are checked the "Reasons" array will not be sent at all.

 

Ken

Link to comment
Share on other sites

i relabeled all of my checkboxes to be seen as an array Reasons[] and values are still only passed one at a time.  here is the PHP i have, i am not sure how to write the Isset function but here is what i have, and it still sends the same error.

 

<?php

if (isset($_POST['submit'])) {
$company = $_POST['CompanyName'];
$name = $_POST['realname'];
$email = $_POST['email'];
$domainName = $_POST['ListorDomainName'];
$username = $_POST['Username'];
$password = $_POST['Password'];
$credit = $_POST['CreditCardNumber'];
$resend = $_POST['Resend'];

if (isset($_POST['Reasons'])) {
$reasons = implode(', '. $_POST['Reasons']);
}

$reasonstext = $_POST['Reasonstext'];
$ServiceName = $_POST['ServiceName'];
$ServiceText = $_POST['ServiceText'];
$to = '';
$subject = 'Cancellation Request';

if (empty($username) && empty($password)){
$to = '.....com';
$msg .= "Company: $company\n";
$msg .= "Name: $name\n";
$msg .= "E-Mail: $email\n";
$msg .= "Domain Name: $domainName\n";
$msg .= "Credit Card: $credit\n";
$msg .= "ReSend Manual: $resend\n";
$msg .= "Service To Cancel: $ServiceName \n";
$msg .= "Reasons For Cancelling: $reasons\n";

}else{
$to = '.....edu';
$msg .= "Company: $company\n";
$msg .= "Name: $name\n";
$msg .= "E-Mail: $email\n";
$msg .= "Domain Name: $domainName\n";
$msg .= "Credit Card: $credit\n";
$msg .= "ReSend Manual: $resend\n";
$msg .= "Service To Cancel: $ServiceName \n";
$msg .= "Reason(s) For Cancelling: $reasons\n";
}
mail($to, $subject, $msg, "From: $email");
header("location: http://.../thankyou.html");
}

?>

Link to comment
Share on other sites

One problem I see is that you have named the input box for the "other Service Name" the same as your radiobuttons. That will effectively knock out any radiobutton that is selected. Also, all of your "<input>" lines have the same "id". That is illegal if you want valid HTML. All ids must be unique.

 

At the start of the processing script, add the following line:

<?php
echo '<pre>' . print_r($_POST,true) . '</pre>'
?>

 

This will dump all the values being sent to the script from the form.

 

Ken

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.