PHP to SharePoint “Contact Us” form

This is a PHP version of the post http://blog.bendsoft.com/2011/02/creating-a-simple-contact-us-form/

SharePoint is a powerful application for managing and sharing information and documents within companies. Nevertheless, many organisations are still using other platforms or content management systems side-by-side. This does not prevent you from using SharePoint as a central sharing point for any of these. Camelot .NET Connector enables quick integration with other systems, for example Joomla or Umbraco.

This article will show, step-by-step, how to create a simple contact form in PHP. Contact forms are typically used on a public website to provide a way for customers, business partners and others to submit questions or request information. This is a great example of using SharePoint in a creative way to simplify common tasks in any organisation. This example can practically be implemented as is in any PHP based CMS system or in a custom PHP web site.

I assume that you already know how to create and edit lists in SharePoint and are familiar with PHP 5.

Requirements

Preparing SharePoint

The first thing to do is to create a custom SharePoint list in WSS 3.0 or MOSS 2007/2010 for storing incoming contact requests. In this example, we choose to call our new list ContactForm. We will also create a set of new columns to hold the details.

The contact list is defined by the columns Title, Company, Email and Message where Title will be used to store the name of the contact person. Message is created as a multi-line text column and the other holds a single line of text. This is what the empty list default view will looks like.

The Camelot WCF Service

To allow PHP to communicate with SharePoint we need a bridge and for this we will use a WCF service in the Windows world and a SoapClient in the PHP world. Follow this blog post when setting up the WCF service, it is surprisingly easy and done in a couple of minutes.

Creating the form in PHP

As stated earlier we will work with four columns, TitleCompanyEmail and Message where Title will be used to store the name of the contact person. To keep this example as simple as possible I’ve chosen to let the form post to the same php page who is hosting the actual form.

Setting up Camelot PHP Tools

Download the lastest version of Camelot PHP Tools as described in the requirements above, this is made by unzipping the package and uploading it to your server. To ensure that PHP Tools will be able to communicate with the WCF service you need to check two constants in the settings.php.

define("DEFAULT_WSDL", "http://yourserver.com/wcf/Camelot.svc?wsdl");
define("WSDL_SHARED_KEY", md5("MySharedKey"));

Set the address to your server hosting the WCF service and type the same shared key here as you entered in the web.config file of the WCF service.

The HTML form

Lets begin by creating a very simple form in HTML

<h2>Contact Us</h2>
<p>Please use the form below to contact us. We will get back to you as quick as possible!</p>
<form method="post" name="contactform" action="contactpage.php" onsubmit="return validateForm()">
    <table>
        <tr>
            <td>Name:</td>
            <td><input type="textbox" name="name" id="name" /> *</td>
        </tr>
        <tr>
            <td>E-mail address:</td>
            <td><input type="textbox" name="email" id="email" /> *</td>
        </tr>
        <tr>
            <td>Company:</td>
            <td><input type="textbox" name="company" id="company" /></asp:TextBox></td>
        </tr>
        <tr>
            <td>Message:</td>
            <td><textarea name="message" id="message" rows="10"></textarea> *</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><input type="submit" id="submit" value="Send form" /> </td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><em>(* = required)</em></td>
        </tr>
    </table>
</form>

 

Basic form validation

<script language="javascript" type="text/javascript">
    /**
     * Very simple validate method
     */
    function validateForm()
    {
        // check name
        var name=document.forms["contactform"]["name"].value
        if (name==null || name=="")
        {
            alert("Name must be filled out");
            return false;
        }

        // check email
        var email=document.forms["contactform"]["email"].value
        var atpos=email.indexOf("@");
        var dotpos=email.lastIndexOf(".");
        if (atpos<1 || dotpos<atpos+2 || dotpos+2>=email.length)
        {
            alert("Not a valid e-mail address");
            return false;
        }

        // check message
        var message=document.forms["contactform"]["message"].value
        if (message==null || message=="" || message.length < 5)
        {
            alert("There must be a message");
            return false;
        }
    }
</script>

Posting to SharePoint

require_once 'classes/class.camelot.soap.php'; // load the necessary PHP Tools framework

if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST') {

    try {
        // Build the SQL command
        $name = addslashes($_POST['name']);
        $email = addslashes($_POST['email']);
        $company = addslashes($_POST['company']);
        $message = addslashes($_POST['message']);


        // Execute the query
        $SharePointNonQuery = new SharePointNonQuery(array(
            'sql' => "INSERT INTO ContactForm (title,email,company,message) VALUES ('$name','$email','$company','$message')",
            'method' => 'ExecuteNonQuery',
            'connString' => 'sharepoint_customers',
            'sharedKey' => constant("WSDL_SHARED_KEY")
        ));

        // On success
        if ($SharePointNonQuery->_result) {
            echo("<script>alert('Your question has been sent to our team. We will handle your enquiry as soon as possible!');</script>");
        } else {
            echo("<script>alert('We could not recieve your question at the moment, please try again or send your message to info@example.com');</script>");
        }
        
    } catch (Exception $exc) {
        echo $exc->getTraceAsString();
    }
}

This is what happens in the PHP script

  1. Include the Soap library from Camelot PHP Tools (require_once ‘classes/class.camelot.soap.php’)
  2. Check if there is a form post coming in (if (strtoupper($_SERVER['REQUEST_METHOD']) == ‘POST’))
  3. Store the form fields in variables
  4. Load the SharePointNonQuery class and
    1. create the sql command
    2. specify which method to use in the WCF service (ExecuteNonQuery or ExecuteScalar in this case)
    3. select which connection to use (the connString, this should match your web.config in the WCF Service)
    4. send the shared key (set in settings.php)
  5. The command is executed
  6. If true (or 1) is returned the command was successful, show success dialog.

Thats it!

Final word

The Camelot .NET Connector simplifies SharePoint integration. In fact, in many cases, developers do not need to have any previous experience of SharePoint development to create advanced integrations. By using your general skills and basic understanding of the SQL syntax you will be able to achive quite alot on your own. This is why the connector gives you advantage over you competitors.

If you any questions regarding the connector and how it can help you, please feel free to contact us using our very own contact form, of course powered by SharePoint 2010, at http://www.bendsoft.com/contact-us/.

 

To download the code from this tutorial press the button above or follow this link.

To download Camelot PHP Tools for SharePoint visit the PHP Tools download section at our web site.

This entry was posted in PHP. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>