PHP contact form

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

jwoodford77
Forum Newbie
Posts: 8
Joined: Mon Jan 12, 2009 9:59 pm

PHP contact form

Post by jwoodford77 »

Hello everyone, I am a novice when it comes to PHP code can someone look at this code and tell me what I am doing wrong please

at <form action line its is giving me a "Parse error:" I am trying to email the contents of the form when filled out and also post it to salesforce. I am not sure if this is possible if someone could help me that would be great.

Thanks,

J




<?php
$errorMessage = '';
$validForm = true;

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

if(empty($_POST['Name'])) {
$errorMessage.= 'Invalid name<br>';
$validForm = false;
}
if(empty($_POST['Email'])) {
$errorMessage.= 'Invalid email<br>';
$validForm = false;
}

if($validForm) {

$message = 'Name: '.$_POST['Name']."\r\n";
$message.= 'Email: '.$_POST['Email']."\r\n";
$message.= 'Phone: '.$_POST['Phone']."\r\n";
$message.= 'Company: '.$_POST['Company']."\r\n";
$message.= 'Services: ';
if(isset($_POST['Services'])) {
foreach($_POST['Services'] as $service)
$message.= $service."\r\n";
}
$message.= 'Comments: '.$_POST['Comments']."\r\n";

$to = 'jwoodford@building.com';
$subject = 'Building.com Contact Form';
$headers = 'From: Building.com <contactform@building.com>' . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/plain; charset=iso-8859-1' . "\r\n";
mail($to, $subject, $message, $headers);
{
<form action="https://www.salesforce.com/servlet/serv ... ding=UTF-8" method="POST" />
<input type="hidden" name="oid" value="00D700000009cd" />
$errorMessage = '<strong>Your information has been sent.</strong>';
}
}
?>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: PHP contact form

Post by califdon »

The error is because those two lines are not PHP, they are HTML, so of course they won't parse as PHP. You have to either put them outside any PHP tags or echo them within quotation marks.
jwoodford77
Forum Newbie
Posts: 8
Joined: Mon Jan 12, 2009 9:59 pm

Re: PHP contact form

Post by jwoodford77 »

Hey thanks for the help, is there anyway to rewrite the html function in PHP. This maybe a stupid question but I need it to email the contents of the form and also post it to salesforce. If I move the html lines out of the PHP it only will post to salesforce and not email.

Any ideas would be much appreciated.

Thanks,

J
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: PHP contact form

Post by jaoudestudios »

califdon is right. But it is difficult to see as you have not put code tags around your code - which will indent and color the code properly. I am unable to edit your post, but you can edit it.

I think you have an extra opening bracket in there, but difficult to see.

I can see if you need to echo that html or save it in a string to display later on. Is there more to the script with the rest of the form?
jwoodford77
Forum Newbie
Posts: 8
Joined: Mon Jan 12, 2009 9:59 pm

Re: PHP contact form

Post by jwoodford77 »

Here is the entire form, this one I tried to echo the html but I think the quotes in the html want allow me to echo it.

Code: Select all

 
<?php
$errorMessage = '';
$validForm = true;
 
if(isset($_POST['Submit'])) {
 
    if(empty($_POST['Name'])) {
        $errorMessage.= 'Invalid name<br>';
        $validForm = false; 
    }
    if(empty($_POST['Email'])) {
        $errorMessage.= 'Invalid email<br>';
        $validForm = false; 
    }
    
    if($validForm) {
    
        $message = 'Name: '.$_POST['Name']."\r\n";
        $message.= 'Email: '.$_POST['Email']."\r\n";
        $message.= 'Phone: '.$_POST['Phone']."\r\n";                
        $message.= 'Company: '.$_POST['Company']."\r\n";
        $message.= 'Services: ';
        if(isset($_POST['Services'])) {
            foreach($_POST['Services'] as $service)
            $message.= $service."\r\n";     
        }           
        $message.= 'Comments: '.$_POST['Comments']."\r\n";                              
    
        $to = 'jwoodford@building.com'; 
        $subject = 'Building.com Contact Form';
        $headers  = 'From: Building.com <contactform@building.com>' . "\r\n";
        $headers .= 'MIME-Version: 1.0' . "\r\n"; 
        $headers .= 'Content-type: text/plain; charset=iso-8859-1' . "\r\n";        
        mail($to, $subject, $message, $headers);    
        echo "<form action= https://www.salesforce.com/servlet/serv ... ding=UTF-8 method="POST">
<input type=hidden name="oid" value="00D700000009Lt5">";
        $errorMessage = '<strong>Your information has been sent.</strong>';
    }
}
?>
 
 
<!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">
<head>
</head>
 
<body>
<div id="wrapper">
<div id="folio">
<div id="sitewrapper">
 
<div class="fullcolwrap">
    <div id="logo"></div>
    </div>
 
<div id="contentwrapper"><div id="contentsubwrapperleft"><div id="submodules"></div>
</div>
 
<div id="contentsubwrapperright"><div id="contentright"> 
<table width="348" border="0">
  <tr>
    <td width="7" rowspan="7"><!--  ----------------------------------------------------------------------  -->
<!--  NOTE: Please add the following <META> element to your page <HEAD>.      -->
<!--  If necessary, please modify the charset parameter to specify the        -->
<!--  character set of your HTML page.                                        -->
<!--  ----------------------------------------------------------------------  -->
 
<META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8">
 
<!--  ----------------------------------------------------------------------  -->
<!--  NOTE: Please add the following <FORM> element to your page.             -->
<!--  ----------------------------------------------------------------------  -->
  
</td>
    <td width="110"><label for="first_name"><strong>First Name:</strong></label></td>
    <td width="124"><input  id="first_name" maxlength="40" name="first_name" size="20" type="text" /></td>
  <td width="89">
    <!--  ----------------------------------------------------------------------  -->
    <!--  NOTE: These fields are optional debugging elements.  Please uncomment   -->
    <!--  these lines if you wish to test in debug mode.                          -->
    <!--  <input type="hidden" name="debug" value=1>                              -->
    <!--  <input type="hidden" name="debugEmail"                                  -->
    <!--  value="jwoodford@.com">                                  -->
    <!--  ----------------------------------------------------------------------  -->
    <label for="first_name"></label>
    <br>
  </form></td>
  </tr>
  <tr>
    <td><label for="last_name"><strong>Last Name:</strong></label></td>
    <td><input  id="last_name" maxlength="80" name="last_name" size="20" type="text" /></td>
  <td>&nbsp;</td>
  </tr>
  <tr>
    <td><label for="email"><strong>Email:</strong></label></td>
    <td><input  id="email" maxlength="80" name="email" size="20" type="text" /></td>
  <td>&nbsp;</td>
  </tr>
  <tr>
    <td><label for="phone"><strong>Phone:</strong></label></td>
    <td><input  id="phone" maxlength="40" name="phone" size="20" type="text" /></td>
  <td>&nbsp;</td>
  </tr>
  <tr>
    <td><label for="company"><strong>Company:</strong></label></td>
    <td><input  id="company" maxlength="40" name="company" size="20" type="text" /></td>
  <td>&nbsp;</td>
  </tr>
  <tr>
    <td><label for="city"><strong>City:</strong></label></td>
    <td><input  id="city" maxlength="40" name="city" size="20" type="text" /></td>
  <td>&nbsp;</td>
  </tr>
  <tr>
    <td><label for="state"><strong>State/Province:</strong></label></td>
    <td><input  id="state" maxlength="20" name="state" size="20" type="text" /></td>
  <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td><input type="submit" name="submit"></td>
  </tr>
</table>
 
<div id="testimonials">
  <p>
    <script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
    </script>
    <script type="text/javascript">
_uacct = "UA-4120911-1";
urchinTracker();
    </script>
</p>
  </div>
</div>
</div>
</div>
 
</div>
 
</div>
</div>
</body>
</html>
 
jthayne
Forum Newbie
Posts: 5
Joined: Fri Jan 09, 2009 10:51 am

Re: PHP contact form

Post by jthayne »

Code: Select all

 
<?php
$errorMessage = '';
$validForm = true;
 
if(isset($_POST['Submit'])) {
 
    if(empty($_POST['Name'])) {
        $errorMessage.= 'Invalid name<br>';
        $validForm = false; 
    }
    if(empty($_POST['Email'])) {
        $errorMessage.= 'Invalid email<br>';
        $validForm = false; 
    }
    
    if($validForm) {
    
        $message = 'Name: '.$_POST['Name']."\r\n";
        $message.= 'Email: '.$_POST['Email']."\r\n";
        $message.= 'Phone: '.$_POST['Phone']."\r\n";                
        $message.= 'Company: '.$_POST['Company']."\r\n";
        $message.= 'Services: ';
        if(isset($_POST['Services'])) {
            foreach($_POST['Services'] as $service)
            $message.= $service."\r\n";     
        }           
        $message.= 'Comments: '.$_POST['Comments']."\r\n";                              
    
        $to = 'jwoodford@building.com'; 
        $subject = 'Building.com Contact Form';
        $headers  = 'From: Building.com <contactform@building.com>' . "\r\n";
        $headers .= 'MIME-Version: 1.0' . "\r\n"; 
        $headers .= 'Content-type: text/plain; charset=iso-8859-1' . "\r\n";        
        mail($to, $subject, $message, $headers);
        { 
?>
<form action="https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST" />
<input type="hidden" name="oid" value="00D700000009cd" />           
<?php
    $errorMessage = '<strong>Your information has been sent.</strong>';
    }
}
?>
I have reposted your PHP code (formatted as PHP). I also added in the PHP tags so that the HTML will parse as it should. This will solve your parsing error. Your second issue with sending an email as well as posting to salesforce is altogether different. If you want it done automatically, a form will not be the way to do it. That will require an extra mouse click in order to send the information on. You will want to look into cURL for posting information to another site.
User avatar
blue-printf
Forum Newbie
Posts: 12
Joined: Mon Jan 12, 2009 9:27 am

Re: PHP contact form

Post by blue-printf »

is this the entire script? some brackets are missing. your not ending all if's

looking at your script your not ending the if($validForm).

you should output the html using echo or somthing alike.

something you should pay attention to aswell is that the input(from the form input) your sending in the email might not be cleam. so use htmlentities() to be safe.
jwoodford77
Forum Newbie
Posts: 8
Joined: Mon Jan 12, 2009 9:59 pm

Re: PHP contact form

Post by jwoodford77 »

I really appreciate the help, so I tried the code out and it is giving me a Parse error: on line 146, there is no code on line 146.

Code: Select all

 
<?php
$errorMessage = '';
$validForm = true;
 
if(isset($_POST['Submit'])) {
 
    if(empty($_POST['Name'])) {
        $errorMessage.= 'Invalid name<br>';
        $validForm = false; 
    }
    if(empty($_POST['Email'])) {
        $errorMessage.= 'Invalid email<br>';
        $validForm = false; 
    }
   
    if($validForm) {
   
        $message = 'Name: '.$_POST['Name']."\r\n";
        $message.= 'Email: '.$_POST['Email']."\r\n";
        $message.= 'Phone: '.$_POST['Phone']."\r\n";               
        $message.= 'Company: '.$_POST['Company']."\r\n";
        $message.= 'Services: ';
        if(isset($_POST['Services'])) {
            foreach($_POST['Services'] as $service)
            $message.= $service."\r\n";    
        }          
        $message.= 'Comments: '.$_POST['Comments']."\r\n";                             
   
        $to = 'jwoodford@buildingengines.com';
        $subject = 'Building.com Contact Form';
        $headers  = 'From: Building.com <contactform@buildingengines.com>' . "\r\n";
        $headers .= 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/plain; charset=iso-8859-1' . "\r\n";       
        mail($to, $subject, $message, $headers);
        {
?>
<form action="https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST" />
<input type="hidden" name="oid" value="00D700000009Lqr" />          
<?php
    $errorMessage = '<strong>Your information has been sent.</strong>';
    }
}
?>
 
 
 
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="description" content="We are facility maintenance and workflow specialists delivering online data collection and messaging tools to help manage buildings more efficiently and increase your asset value">
<meta name="keywords" content="building management, building facility management, building management operations, commercial property management, real estate management, building maintenance, building risk revenue, property management efficiency, liability, work order solutions, building return on investment, wireless building management systems">
<Meta name="Robots" content="INDEX,FOLLOW">
<title>Building Engines - Contact Building Engines</title>
<link href="../building-engines.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.style2 {color: #CC0000}
.style3 {color: #FF0000}
-->
</style>
</head>
 
<body>
<div id="wrapper">
<div id="folio">
<div id="sitewrapper">
 
<div class="fullcolwrap">
    <div id="logo"></div>
    </div>
 
<div id="contentwrapper"><div id="contentsubwrapperleft"><div id="submodules"></div>
</div>
 
<div id="contentsubwrapperright"><div id="contentright"> 
<table width="348" border="0">
  <tr>
    <td width="7" rowspan="7"><!--  ----------------------------------------------------------------------  -->
<!--  NOTE: Please add the following <META> element to your page <HEAD>.      -->
<!--  If necessary, please modify the charset parameter to specify the        -->
<!--  character set of your HTML page.                                        -->
<!--  ----------------------------------------------------------------------  -->
 
<META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8">
 
<!--  ----------------------------------------------------------------------  -->
<!--  NOTE: Please add the following <FORM> element to your page.             -->
<!--  ----------------------------------------------------------------------  -->
  
</td>
    <td width="110"><label for="first_name"><strong>First Name:</strong></label></td>
    <td width="124"><input  id="first_name" maxlength="40" name="first_name" size="20" type="text" /></td>
  <td width="89">
    <!--  ----------------------------------------------------------------------  -->
    <!--  NOTE: These fields are optional debugging elements.  Please uncomment   -->
    <!--  these lines if you wish to test in debug mode.                          -->
    <!--  <input type="hidden" name="debug" value=1>                              -->
    <!--  <input type="hidden" name="debugEmail"                                  -->
    <!--  value="jwoodford@buildingengines.com">                                  -->
    <!--  ----------------------------------------------------------------------  -->
    <label for="first_name"></label>
    <br>
  </form></td>
  </tr>
  <tr>
    <td><label for="last_name"><strong>Last Name:</strong></label></td>
    <td><input  id="last_name" maxlength="80" name="last_name" size="20" type="text" /></td>
  <td>&nbsp;</td>
  </tr>
  <tr>
    <td><label for="email"><strong>Email:</strong></label></td>
    <td><input  id="email" maxlength="80" name="email" size="20" type="text" /></td>
  <td>&nbsp;</td>
  </tr>
  <tr>
    <td><label for="phone"><strong>Phone:</strong></label></td>
    <td><input  id="phone" maxlength="40" name="phone" size="20" type="text" /></td>
  <td>&nbsp;</td>
  </tr>
  <tr>
    <td><label for="company"><strong>Company:</strong></label></td>
    <td><input  id="company" maxlength="40" name="company" size="20" type="text" /></td>
  <td>&nbsp;</td>
  </tr>
  <tr>
    <td><label for="city"><strong>City:</strong></label></td>
    <td><input  id="city" maxlength="40" name="city" size="20" type="text" /></td>
  <td>&nbsp;</td>
  </tr>
  <tr>
    <td><label for="state"><strong>State/Province:</strong></label></td>
    <td><input  id="state" maxlength="20" name="state" size="20" type="text" /></td>
  <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td><input type="submit" name="submit"></td>
  </tr>
</table>
 </form>
</body>
</html>
 
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: PHP contact form

Post by jaoudestudios »

Like I said, there is something wrong with your brackets. I have highlighted a suspect bracket in red

EDIT: it wont let me color the code, line 36

Code: Select all

 
<?php
$errorMessage = '';
$validForm = true;
 
if(isset($_POST['Submit'])) {
 
    if(empty($_POST['Name'])) {
        $errorMessage.= 'Invalid name<br>';
        $validForm = false;
    }
    if(empty($_POST['Email'])) {
        $errorMessage.= 'Invalid email<br>';
        $validForm = false;
    }
   
    if($validForm) {
   
        $message = 'Name: '.$_POST['Name']."\r\n";
        $message.= 'Email: '.$_POST['Email']."\r\n";
        $message.= 'Phone: '.$_POST['Phone']."\r\n";              
        $message.= 'Company: '.$_POST['Company']."\r\n";
        $message.= 'Services: ';
        if(isset($_POST['Services'])) {
            foreach($_POST['Services'] as $service)
            $message.= $service."\r\n";    
        }          
        $message.= 'Comments: '.$_POST['Comments']."\r\n";                            
   
        $to = 'jwoodford@buildingengines.com';
        $subject = 'Building.com Contact Form';
        $headers  = 'From: Building.com <contactform@buildingengines.com>' . "\r\n";
        $headers .= 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/plain; charset=iso-8859-1' . "\r\n";      
        mail($to, $subject, $message, $headers);
        { // error what is this doing here?
?>
<form action="https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST" />
<input type="hidden" name="oid" value="00D700000009Lqr" />          
<?php
    $errorMessage = '<strong>Your information has been sent.</strong>';
    }
}
?>
 
 
 
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="description" content="We are facility maintenance and workflow specialists delivering online data collection and messaging tools to help manage buildings more efficiently and increase your asset value">
<meta name="keywords" content="building management, building facility management, building management operations, commercial property management, real estate management, building maintenance, building risk revenue, property management efficiency, liability, work order solutions, building return on investment, wireless building management systems">
<Meta name="Robots" content="INDEX,FOLLOW">
<title>Building Engines - Contact Building Engines</title>
<link href="../building-engines.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.style2 {color: #CC0000}
.style3 {color: #FF0000}
-->
</style>
</head>
 
<body>
<div id="wrapper">
<div id="folio">
<div id="sitewrapper">
 
<div class="fullcolwrap">
    <div id="logo"></div>
    </div>
 
<div id="contentwrapper"><div id="contentsubwrapperleft"><div id="submodules"></div>
</div>
 
<div id="contentsubwrapperright"><div id="contentright"> 
<table width="348" border="0">
  <tr>
    <td width="7" rowspan="7"><!--  ----------------------------------------------------------------------  -->
<!--  NOTE: Please add the following <META> element to your page <HEAD>.      -->
<!--  If necessary, please modify the charset parameter to specify the        -->
<!--  character set of your HTML page.                                        -->
<!--  ----------------------------------------------------------------------  -->
 
<META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8">
 
<!--  ----------------------------------------------------------------------  -->
<!--  NOTE: Please add the following <FORM> element to your page.             -->
<!--  ----------------------------------------------------------------------  -->
 
</td>
    <td width="110"><label for="first_name"><strong>First Name:</strong></label></td>
    <td width="124"><input  id="first_name" maxlength="40" name="first_name" size="20" type="text" /></td>
  <td width="89">
    <!--  ----------------------------------------------------------------------  -->
    <!--  NOTE: These fields are optional debugging elements.  Please uncomment   -->
    <!--  these lines if you wish to test in debug mode.                          -->
    <!--  <input type="hidden" name="debug" value=1>                              -->
    <!--  <input type="hidden" name="debugEmail"                                  -->
    <!--  value="jwoodford@buildingengines.com">                                  -->
    <!--  ----------------------------------------------------------------------  -->
    <label for="first_name"></label>
    <br>
  </form></td>
  </tr>
  <tr>
    <td><label for="last_name"><strong>Last Name:</strong></label></td>
    <td><input  id="last_name" maxlength="80" name="last_name" size="20" type="text" /></td>
  <td>&nbsp;</td>
  </tr>
  <tr>
    <td><label for="email"><strong>Email:</strong></label></td>
    <td><input  id="email" maxlength="80" name="email" size="20" type="text" /></td>
  <td>&nbsp;</td>
  </tr>
  <tr>
    <td><label for="phone"><strong>Phone:</strong></label></td>
    <td><input  id="phone" maxlength="40" name="phone" size="20" type="text" /></td>
  <td>&nbsp;</td>
  </tr>
  <tr>
    <td><label for="company"><strong>Company:</strong></label></td>
    <td><input  id="company" maxlength="40" name="company" size="20" type="text" /></td>
  <td>&nbsp;</td>
  </tr>
  <tr>
    <td><label for="city"><strong>City:</strong></label></td>
    <td><input  id="city" maxlength="40" name="city" size="20" type="text" /></td>
  <td>&nbsp;</td>
  </tr>
  <tr>
    <td><label for="state"><strong>State/Province:</strong></label></td>
    <td><input  id="state" maxlength="20" name="state" size="20" type="text" /></td>
  <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td><input type="submit" name="submit"></td>
  </tr>
</table>
 </form>
</body>
</html>
 
User avatar
blue-printf
Forum Newbie
Posts: 12
Joined: Mon Jan 12, 2009 9:27 am

Re: PHP contact form

Post by blue-printf »

i believe your missing a bracket for your if($validForm)
i think i should end before $message.= 'Comments: '.$_POST['Comments']."\r\n";
jwoodford77
Forum Newbie
Posts: 8
Joined: Mon Jan 12, 2009 9:59 pm

Re: PHP contact form

Post by jwoodford77 »

so removed the bracket on line 35 and now I am not getting the error but the submit button is not working. I really appreciate the help.

Code: Select all

 
<?php
$errorMessage = '';
$validForm = true;
 
if(isset($_POST['Submit'])) {
 
    if(empty($_POST['Name'])) {
        $errorMessage.= 'Invalid name<br>';
        $validForm = false; 
    }
    if(empty($_POST['Email'])) {
        $errorMessage.= 'Invalid email<br>';
        $validForm = false; 
    }
   
    if($validForm) {
   
        $message = 'Name: '.$_POST['Name']."\r\n";
        $message.= 'Email: '.$_POST['Email']."\r\n";
        $message.= 'Phone: '.$_POST['Phone']."\r\n";               
        $message.= 'Company: '.$_POST['Company']."\r\n";
        $message.= 'Services: ';
        if(isset($_POST['Services'])) {
            foreach($_POST['Services'] as $service)
            $message.= $service."\r\n";    
        }          
        $message.= 'Comments: '.$_POST['Comments']."\r\n";                             
   
        $to = 'jwoodford@buildingengines.com';
        $subject = 'Building.com Contact Form';
        $headers  = 'From: Building.com <contactform@buildingengines.com>' . "\r\n";
        $headers .= 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/plain; charset=iso-8859-1' . "\r\n";       
        mail($to, $subject, $message, $headers);
        
?>
<form action="https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST" />
<input type="hidden" name="oid" value="00D700000009Lqr" />          
<?php
    $errorMessage = '<strong>Your information has been sent.</strong>';
    }
}
?>
 
 
 
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="description" content="We are facility maintenance and workflow specialists delivering online data collection and messaging tools to help manage buildings more efficiently and increase your asset value">
<meta name="keywords" content="building management, building facility management, building management operations, commercial property management, real estate management, building maintenance, building risk revenue, property management efficiency, liability, work order solutions, building return on investment, wireless building management systems">
<Meta name="Robots" content="INDEX,FOLLOW">
<title>Building Engines - Contact Building Engines</title>
<link href="../building-engines.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.style2 {color: #CC0000}
.style3 {color: #FF0000}
-->
</style>
</head>
 
<body>
<div id="wrapper">
<div id="folio">
<div id="sitewrapper">
 
<div class="fullcolwrap">
    <div id="logo"></div>
    </div>
 
<div id="contentwrapper"><div id="contentsubwrapperleft"><div id="submodules"></div>
</div>
 
<div id="contentsubwrapperright"><div id="contentright"> 
<table width="348" border="0">
  <tr>
    <td width="7" rowspan="7"><!--  ----------------------------------------------------------------------  -->
<!--  NOTE: Please add the following <META> element to your page <HEAD>.      -->
<!--  If necessary, please modify the charset parameter to specify the        -->
<!--  character set of your HTML page.                                        -->
<!--  ----------------------------------------------------------------------  -->
 
<META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8">
 
<!--  ----------------------------------------------------------------------  -->
<!--  NOTE: Please add the following <FORM> element to your page.             -->
<!--  ----------------------------------------------------------------------  -->
  
</td>
    <td width="110"><label for="first_name"><strong>First Name:</strong></label></td>
    <td width="124"><input  id="first_name" maxlength="40" name="first_name" size="20" type="text" /></td>
  <td width="89">
    <label for="first_name"></label>
    <br>
</td>
  </tr>
  <tr>
    <td><label for="last_name"><strong>Last Name:</strong></label></td>
    <td><input  id="last_name" maxlength="80" name="last_name" size="20" type="text" /></td>
  <td>&nbsp;</td>
  </tr>
  <tr>
    <td><label for="email"><strong>Email:</strong></label></td>
    <td><input  id="email" maxlength="80" name="email" size="20" type="text" /></td>
  <td>&nbsp;</td>
  </tr>
  <tr>
    <td><label for="phone"><strong>Phone:</strong></label></td>
    <td><input  id="phone" maxlength="40" name="phone" size="20" type="text" /></td>
  <td>&nbsp;</td>
  </tr>
  <tr>
    <td><label for="company"><strong>Company:</strong></label></td>
    <td><input  id="company" maxlength="40" name="company" size="20" type="text" /></td>
  <td>&nbsp;</td>
  </tr>
  <tr>
    <td><label for="city"><strong>City:</strong></label></td>
    <td><input  id="city" maxlength="40" name="city" size="20" type="text" /></td>
  <td>&nbsp;</td>
  </tr>
  <tr>
    <td><label for="state"><strong>State/Province:</strong></label></td>
    <td><input  id="state" maxlength="20" name="state" size="20" type="text" /></td>
  <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td><input type="submit" name="submit"></td>
  </tr>
</table>
</form>
</body>
</html>
 
 
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: PHP contact form

Post by jaoudestudios »

Side note...This is where SVN would speed this process up...reference to viewtopic.php?f=6&t=93386 :P

Move your <form.... down to where the input fields are (just above)
jwoodford77
Forum Newbie
Posts: 8
Joined: Mon Jan 12, 2009 9:59 pm

Re: PHP contact form

Post by jwoodford77 »

Thanks again everyone for the help, I have several different forms now with cleaner code. The problem still is the form will only do one function if the html code to post to salesforce is in the that is what the form will do when the user submits. If you take out the code then the php code works fine and emails me. Is it possible to make the form post the contact to salesforce and email me when someone submits?

Thanks again,

J
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: PHP contact form

Post by califdon »

Everything is possible. The thing about PHP is that you can intermix it with HTML, so you can apply logic at the server, making choices among various code operations, as long as you have the data at the server, at that time, to make such choices.

In general, you can can stop and start PHP parsing wherever you want, so, for example:

Code: Select all

<?php
session_start();
?>
<!-- now this is straight html: -->
<html>
<head>
...
</head>
<body>
<h2>This is HTML that will be sent directly to the browser</h2>
...
<?php  // now I have turned PHP parsing back on
if(!isset($_SESSION['loggedin'])) {  // if the session variable 'loggedin' is NOT set:
?>
<!-- now I have turned PHP parsing off, so the next line is plain HTML: -->
    <h3>You must log in first</h3>
<?php  // now I have turned it on again because I need to use more PHP syntax:
} else {  // if the session variable 'loggedin' IS set:
?>
<!-- now I have turned it off again, so the following is HTML: -->
    <h4>Thanks for logging in, here is your page:</h4>
    Now is the time for all good men ....
    ...
<?php  // now I have to finish the PHP syntax (the ending bracket of the if block):
}
?>
<!-- the rest is HTML:  -->
</body>
</html>
 
jwoodford77
Forum Newbie
Posts: 8
Joined: Mon Jan 12, 2009 9:59 pm

Re: PHP contact form

Post by jwoodford77 »

Once again thanks for all of the help the parser is not a problem any more. Truly sorry for beating this subject to death but how would I make both functions work correctly. Here is the code for my form that is working halfway right, it will post the data correctly to salesforce but will not email it to me. If I take out the salesforce code it will email it to me how would I make it do both?

Code: Select all

 
<?php
$errorMessage = '';
$validForm = true;
 
if(isset($_POST['Submit'])) {
 
    if(empty($_POST['first_name'])) {
        $errorMessage.= 'Invalid name<br>';
        $validForm = false; 
    }
    if(empty($_POST['email'])) {
        $errorMessage.= 'Invalid email<br>';
        $validForm = false; 
    }
    
    if($validForm) {
    
        $message = 'first_name: '.$_POST['first_name']."\r\n";
        $message.= 'email: '.$_POST['email']."\r\n";        
        $message.= 'company: '.$_POST['company']."\r\n";
        $message.= 'state: '.$_POST['state']."\r\n";
        $message.= 'phone: '.$_POST['phone']."\r\n";    
        }                               
    
        $to = 'jwoodford@buildingengines.com'; 
        $subject = 'BuildingEngines.com Contact Form';
        $headers  = 'From: BuildingEngines.com <contactform@buildingengines.com>' . "\r\n";
        $headers .= 'MIME-Version: 1.0' . "\r\n"; 
        $headers .= 'Content-type: text/plain; charset=iso-8859-1' . "\r\n";        
        mail($to, $subject, $message, $headers);
            
        $errorMessage = '<strong>Your information has been sent.</strong>';
    }
?>
<!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">
<head>
</head>
 
<body>
 
<h1>&nbsp;</h1>
<form method="post" action="https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8">
    <input type="hidden" value="00D700000009Lqr" name="oid" /> <input type="hidden" value="http://www.buildingengines.com/index.php/thanks.html" name="retURL" />
<form id="form1" name="form1" method="post" action="">
<p class="alert"><?php echo $errorMessage;?></p>
 
<table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td width="35%" class="contactcells"></td>
      <td width="65%" class="contactcells"></td>
    </tr>
    <tr>
      <td width="35%" class="contactcells"><strong>Your First Name:</strong></td>
      <td width="65%" class="contactcells"><input name="first_name" type="text" class="contactfields" size="32" />
        <span class="style2">*</span></td>
    </tr>
    <tr>
      <td class="contactcells">Last Name: </td>
      <td class="contactcells"><input name="last_name" type="text" class="contactfields" size="32" /></td>
    </tr>
    <tr>
      <td width="35%" class="contactcells"><strong>Your Email Address: </strong></td>
      <td width="65%" class="contactcells"><input name="email" type="text" class="contactfields" size="32" />
        <span class="style2">*</span></td>
    </tr>
    <tr>
      <td width="35%" class="contactcells"><strong>Your Company's Name:</strong></td>
      <td width="65%" class="contactcells"><input name="company" type="text" class="contactfields" size="32" /></td>
    </tr>
    <tr>
            <td width="65%" class="contactcells">      </td>
    </tr>
    <tr>
      <td class="contactcells"><strong>State:</strong></td>
      <td class="contactcells"><input name="state" type="text" class="contactfields" size="32" /></td>
    </tr>
    <tr>
      <td class="contactcells"><span class="style2">*</span> Indicates a required
        field </td>
      <td class="contactcells"><input name="Submit" type="submit" class="contactformbutton" value="Submit" /></td>
    </tr>
  </table>
</form>
 
 
</div>
 
</body>
</html>
 
 
Post Reply