PHP checkbox scripting problems.

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
webdev
Forum Commoner
Posts: 25
Joined: Tue Jun 05, 2007 2:07 am

PHP checkbox scripting problems.

Post by webdev »

Checkboxes Buttons

Having trouble with you checkboxes and radio buttons. Checkboxes are mainly use when presenting the user with a number of options, of which, they can select more than one option. For example, a form asking a user what topics they are interested in, obviously the user may be interested in more than one topic. When the user fill the form, the form will be automatically send though to my email but not user's email.

Okay, lets construct a simple form asking the user what topics they like. There will be multiple options of which the user can select none, some or all the options.

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Contact Test Form</title>

</head>

<body>
Required fields are marked with an asterisk ( * ).
<form action="mail.php" method="post">

<table width="275" border="0" cellspacing="4" cellpadding="0">
    <tr>
      <td width="75">Name *</td>
      <td width="210"><input name="name" type="text" class="textfield"></td>
    </tr>
	<tr>
      <td valign="top" width="75">Address *<img src="../images/spacer.gif" width="65" height="1"></td>
      <td width="210"><textarea name="address" cols="25" rows="3" class="textfield"></textarea></td>
    </tr>
	<tr>
      <td width="40">City *</td>
      <td width="210"><input name="city" type="text" class="textfield"></td>
      <td width="40">State</td>
      <td width="10"><input name="state" type="text" class="textfield" size="4" maxlength="4"></td>
	  <td width="5"></td>
      <td width="65">Postcode</td>
      <td width="10"><input name="postcode" type="text" class="textfield" size="4" maxlength="4"></td>
	</tr>
	<tr>
    <td valign="top">Telephone *</td>
      <td ><input name="phone" type="text" class="textfield" size="14" maxlength="12"></td>
    </tr>
	<tr>
      <td valign="top" width="75" >Email *</td>
      <td width="210"><input name="email" type="text" class="textfield"></td>
    </tr>
		<tr>
	<td valign="top" width="65" >Adviser Group</td>
      <td width="210"><input name="advisergroup" type="text" class="textfield"></td>
    </tr>
	    <tr>
	<td width="150" valign="top" >Preferred method of contact:</td>
	<td valign="top" >
	<table border="0" cellpadding="0" cellspacing="0" width="155">
	<tbody>
		<tr>
	<td width="40"><input name="contact" value="Post" type="radio"></td>
	<td valign="bottom" width="35">Post&nbsp;</td>
	<td width="40"><input name="contact" value="Telephone" type="radio"></td>
	<td valign="bottom" width="35">Telephone&nbsp;</td>
	<td width="40"><input name="contact" value="Email" type="radio"></td>
	<td valign="bottom" width="40">Email</td>
          </tr>
	</tbody>
	</table>
	</td>
	</tr>
	<td width="150" valign="top" >Are you a:</td>
    <td valign="top">
	<table border="0" cellpadding="0" cellspacing="0" width="155">
    <tbody>
    	<tr>
	<td width="40"><input name="are" value="Adviser" type="radio"></td>
	<td valign="bottom" width="35">Adviser</td>
	<td width="40"><input name="are" value="Account" type="radio"></td>
	<td valign="bottom" width="35">Accountant </td>
	<td width="40"><input name="are" value="New Investor" type="radio"></td>
    <td valign="bottom" width="60">New Investor</td>
    </tr>
	</tbody>
    </table>
	</td>
    </tr>
	<tr>
	<td colspan="2" valign="top" >Please RUSH me Product Disclosure Statement for:</td>
    </tr>
    <tr>
	<td valign="top" >&nbsp;</td>
	<td valign="top" ><table border="0" cellpadding="0" cellspacing="0" width="200">
	<tbody>
	<tr>
		<td width="22"><input name="pds[]" type="checkbox" value="AREIT Augusta Development Fund"></td>
        <td valign="bottom" width="171">AREIT Augusta Development Fund</td>
		</tr>
        <tr>
        <td width="22"><input name="pds[]" type="checkbox" value="AREIT Diversified Fund"></td>
        <td valign="bottom" width="171">AREIT Diversified Fund</td>
		</tr>
		<tr>
		<td width="22"><input name="pds[]" type="checkbox" value="Agricultural Mortgage Trust"></td>
		<td valign="bottom" width="171">Agricultural Mortgage Trust</td>
        </tr>
        </tbody>
	</table>
	</td>
    </tr>
    <tr>
      <td valign="top" >Comments *</td>
      <td valign="top" ><textarea name="comment" cols="25" rows="10" class="textfield"></textarea></td>
    </tr>
    <tr>
      <td valign="top">&nbsp;</td>
      <td align="right" ><input type="submit" name="submit" value="Submit" /></td>
    </tr>
  </table>
</form>
</body>
</html>

Code: Select all

<?php
//Headers
$emailFromName = $_POST['name'];
$emailAddress = $_POST['address'];
$emailCity = $_POST['city'];
$emailState = $_POST['state'];
$emailPostcode = $_POST['postcode'];
$emailTelephone = $_POST['phone'];
$emailFrom = $_POST['email'];
$emailAdviser = $_POST['advisergroup'];
$emailContact = $_POST['contact'];
$emailAre = $_POST['are'];
$emailPds = $_POST['pds'];
$emailComment = $_POST['comment'];
$action = (isset($_POST['submit']) ? "submit" : "entry");

//Checkbox
foreach ($emailPds as $val) {
echo "$val\n";
} 

//Errors
$error_lines = array();
if ($action == "submit")
{
if (empty($emailFromName)) {
$error_lines['emailFromName'] = 'You forgot to enter your name';  	
}
   
if (empty($emailAddress)) {
$error_lines['emailAddress'] = 'You forgot to enter your address';
}

if (empty($emailCity)) {
$error_lines['emailCity'] = 'You forgot to enter your city';
}

if (empty($emailState)) {
$error_lines['emailState'] = 'You forgot to enter your city';
}

if (empty($emailPostcode)) {
$error_lines['emailPostcode'] = 'You forgot to enter your city';
}

if (empty($emailTelephone)) {
$error_lines['emailTelephone'] = 'You forgot to enter your telephone';
}
   
if (!preg_match('/^([A-Z0-9\.\-_]+)@([A-Z0-9\.\-_]+)?([\.]{1})([A-Z]{2,6})$/i', $emailFrom) || empty($emailFrom)) {
$error_lines['emailFrom'] = 'You forgot to enter a valid email address';
}
   
if (empty($emailComment)) {
$error_lines['emailComment'] = 'You must enter a message for me';
}
}

if($action == "submit" && count($error_lines) == 0) {
 
//Send an email  					
$to = "paul.crawford@austasiagroup.com";
$Subject = "AREIT Form Feedback";
$form_results = "The following enquiry was submitted via the AREIL website on ".date('l, d M Y H:i:s') . "\n\nName: ". $emailFromName ."\n\nAddress: ". $emailAddress ."\nCity: ". $emailCity ."\nState: ". $emailState ."\nPostcode: ". $emailPostcode ."\n\nTelephone: ". $emailTelephone ."\n\nEmail: ". $emailFrom ."\n\nAdviser Group: ". $emailAdviser ."\n\nPreferred method of contact: ". $emailContact ."\n\nAre you a: ". $emailAre ."\n\nPlease rush me Product Disclosure Statement for: ". $val ."\n\nComments: ". $emailComment;
mail($to, "$Subject" ,"$form_results", "From: $emailFrom");
   
echo "

Thank you for contacting me, ". $emailFromName .". Your mail was sent successfully.

Your Message

<div class=\"quote\"><h1>Name</h1>
$emailFromName

<h1>Address</h1>
$emailAddress

<h1>City</h1>
$emailCity

<h1>State</h1>
$emailState

<h1>Postcode</h1>
$emailPostcode

<h1>Telephone</h1>
$emailTelephone

<h1>Email</h1>
$emailFrom

<h1>Adviser Group</h1>
$emailAdviser

<h1>Preferred method of contact</h1>
$emailContact

<h1>Are you a</h1>
$emailAre

<h1>Please rush me Product Disclosure Statement for</h1>
$val

<h1>Comments</h1>
$emailComment</div>";

} else {
if (count($error_lines) > 0) {
echo "<div class=\"error\"><h1>Error!</h1>You have the following errors:\n\n";
foreach($error_lines as $line) {
echo $line;
}
echo "
Please complete the form and re-send it.</div>\n\n";
}
   
echo "<form method=\"post\" action=\"mail.php\">
						
If you would like to contact me regarding work or anything that you have seen on this website please use the form below and I will get back to you as soon as possible.

</p>
  						
Name" . (isset($error_lines["emailFromName"]) ? "*" : "") . "

<INPUT TYPE=\"TEXT\" NAME=\"name\" size=\"40\" value=\"$emailFromName\">

Address " . (isset($error_lines["emailAddress"]) ? "*" : "") . "

<INPUT TYPE=\"TEXT\" NAME=\"name\" cols=\"25\" rows=\"3\" value=\"$emailAddress\">

City " . (isset($error_lines["emailCity"]) ? "*" : "") . "

<INPUT TYPE=\"TEXT\" NAME=\"name\" value=\"$emailCity\">

State " . (isset($error_lines["emailState"]) ? "*" : "") . "

<INPUT TYPE=\"TEXT\" NAME=\"name\" value=\"$emailState\">

Postcode " . (isset($error_lines["emailPostcode"]) ? "*" : "") . "

<INPUT TYPE=\"TEXT\" NAME=\"name\" value=\"$emailPostcode\">

Telephone " . (isset($error_lines["emailTelephone"]) ? "*" : "") . "

<INPUT TYPE=\"TEXT\" NAME=\"name\" value=\"$emailTelephone\">

Email Address" . (isset($error_lines["emailFrom"]) ? "*" : "") . "

<input type=\"text\" name=\"email\" size=\"40\" value=\"$emailFrom\">

Comments " . (isset($error_lines["emailComment"]) ? "*" : "") . "

<textarea rows=\"10\" cols=\"40\" name=\"comment\" value=\"$emailComment\"></textarea>

<input type=\"submit\" name=\"submit\" value=\"Submit\">
</p>
</form>";
}

?>
Can you please advise me what is the problem? I need this done by this week. Help me!!!

Thanks alot. :D
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I've removed "VERY URGENT" from your title because that implies your thread is more important than the others, which it is not. This is a community forum where people volunteer their time, if it is truly urgent we have a job forum where you can contract someone to complete your job for you in a timely fashion.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

You appear to have forgotten to tell us what the problem is. What error messages are you getting? What's not happening that is supposed to be happening?
webdev
Forum Commoner
Posts: 25
Joined: Tue Jun 05, 2007 2:07 am

Post by webdev »

There was no error messages but it doesn't showing the checkbox display on the email. For example, if the user select more than 1 checkbox and send the form to my email address, I would like to see more than 1 checkboxes on my email. Something using the code like array, I don't know.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$val is the only reference to data inside $emailPds. It will only be the last element.

Maybe you're looking for implode()?
webdev
Forum Commoner
Posts: 25
Joined: Tue Jun 05, 2007 2:07 am

Post by webdev »

Feyd, could you please give me example of this code?

Thanks alot.
Paul
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

There are examples of its usage on the manual page linked.
webdev
Forum Commoner
Posts: 25
Joined: Tue Jun 05, 2007 2:07 am

Post by webdev »

Thanks, Feyd. It getting pretty close. When I select the 2 checkboxes and send the form to my email, the value of the checkboxes come up 3 checkboxes (AREIT Augusta Dev, AREIT Diversified Fund, Agricultural Mortgage Trust) on my email display. Very strange! For example, there was 3 checkboxes on the form - checkbox 1) AREIT Augusta Dev / checkbox 2) AREIT Diversified Fund / checkbox 3) Agricultural Mortgage Trust. So I want to select AREIT Augusta Dev and AREIT Diversified Fund. These 2 checkboxes will be display on my email. So what is the code for this?
Post Reply