Start New Discussion!

Welcome to the Wufoo forums!

Here you can browse around to find answers to questions you may have. Or, click the button to the right to start a new discussion.

Display Error Messages with API

  • thinkwellthinkwell November 2011
    I am using the API V3 to submit data from a form to my Wufoo account. I followed the example on the Entries POST API page, and I was able to successfully the data to my account.

    I was wondering how I am able to check for error messages, and display the ErrorText and FieldErrors using PHP?

    For example, if someone does not provide their email address in a required email field.

    Here is the code I have so far:

    <?
    function submit() {
    $ref = curl_init('https://myname.wufoo.com/api/v3/forms/xxxxxx/entries.json');
    curl_setopt($ref, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data'));
    curl_setopt($ref, CURLOPT_POST, true);
    curl_setopt($ref, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ref, CURLOPT_POSTFIELDS, getPostParams());
    curl_setopt($ref, CURLOPT_USERPWD, 'XXXX-XXXX-XXXX-XXXX');
    curl_setopt($ref, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($ref, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ref, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ref, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ref, CURLOPT_USERAGENT, 'Wufoo.com');
    echo htmlentities(curl_exec($ref));
    }

    function getPostParams() {
    return array( 'Field4' => "you@company.com");
    }

    submit();
    ?>
  • thinkwellthinkwell November 2011
    I am now able to display the error messages with the help of the Wufoo API wrapper. I have a couple of follow up questions:

    1. I was wondering how I do set up the form on index.php (See below) to post the entries upon pressing the submit button to my-wrapper.php (See below)?

    2. Is it possible to do this using AJAX so the page does not refresh?

    Here's the code for index.php:


    <? require_once('my-wrapper.php'); ?>
    <html>
    <head>
    <title>Form</title>
    </head>
    <body>
    <form id="form1" name="form1" autocomplete="off" enctype="multipart/form-data" method="post" action="">
    <!--Begin Form-->
    <div <? echo addErrors('Field4', $fieldError, 'class') ?>>
    <label id="profile" class="icons" for="Field4">Email</label>
    <input id="Field4" name="Field4" type="text" class="formreg" tabindex="1"/>
    <? echo addErrors('Field4', $fieldError, 'message')?>
    </div>

    <div <? echo addErrors('Field6', $fieldError, 'class') ?>>
    <label id="profile" class="icons" for="Field6">Name</label>
    <input id="Field6" name="Field6" type="text" class="formreg" tabindex="1"/>
    <? echo addErrors('Field6', $fieldError, 'message')?>
    </div>
    <!--End Form-->
    <input type="submit" name="saveForm" value="Submit" id="submit" class="submit" />
    <input type="hidden" id="idstamp" name="idstamp" value="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=" />
    </form>
    </body>
    </html>


    Here's the code for my-wrapper.php:

     
    <?
    // API
    require_once('WufooApiExamples.php');

    // Wufoo
    $wrapper = new WufooApiWrapper('XXXX-XXXX-XXXX-XXXX', 'mysubdomain','wufoo.com');


    // Post Entries
    $postArray = array();
    foreach ($this->postValues as $key => $value) {
    $postArray[] = new WufooSubmitField($key, $value);
    }

    try {
    $result = $wrapper->entryPost('xxxxxx', $postArray);
    if (!$result->Success) {
    foreach ($result->FieldErrors as $key => $value) {
    $fieldError[$value->ID] = $value->ErrorText;
    }
    }
    } catch (Exception $e) {
    //Read the error message.
    }

    // Add Errors
    function addErrors($fieldName, $fieldError, $add){
    if($fieldError[$fieldName] != ''){
    if ($add == 'message') return '<p class="errors">'.$fieldError[$fieldName].'</p>';
    else if ($add == 'class') return 'class ="errors"';
    }
    }
    ?>

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In Join the Forums!

In this Discussion

Documentation

How to use Wufoo and all of Wufoo's features are documented in our official documentation.

Go to Docs

Support

Not finding what you need in the forums or the documentation? You can submit a support request.

Go to Support