PHP Form Builder Class / Examples / Ajax

Version: Released:

Ajax - To submit your form's data via AJAX, simply apply the "ajax" form attribute in the setAttributes function. That's it - you're all set. Most all of the complexity is handled for you by the project in the background, which saves you development time. Below, you'll find several more form attributes that are specific to ajax functionality within the project.

Review the final section titled "Validation w/Ajax" in the validation example file provided for instructions on how to handle php validation when using the "ajax" form attribute.

Processing

<?php
$form 
= new form("ajax_0");
$form->setAttributes(array(
    
"width" => "500",
    
"map" => array(122213),
    
"ajax" => 1
));

$form->addHidden("cmd""submit_0");
$form->addTextbox("Username:""Username""", array("required" => 1));
$form->addPassword("Password:""Password""", array("required" => 1));
$form->addPassword("Re-Enter Password:""Password2""", array("required" => 1));
$form->addTextbox("First Name:""FName""", array("required" => 1));
$form->addTextbox("Last Name:""LName""", array("required" => 1));
$form->addEmail("Email Address:""Email""", array("required" => 1));
$form->addTextbox("Phone Number:""Phone");
$form->addTextbox("Address:""Address");
$form->addTextbox("City:""City");
$form->addState("State:""State");
$form->addTextbox("Zip Code:""Zip");
$form->addButton("Sign Up");
$form->render();
?>

This example, displayed below, uses Google's Geocoding API combined with this project's AJAX functionality to provide users with a quick way to convert an address into a latitude/longitude pair. Also demonstrated here is the use of the "ajaxCallback" form attribute.

Processing

<?php
$form 
= new form("ajax_1");
$form->setAttributes(array(
    
"width" => "400",
    
"ajaxCallback" => "parseJSONResponse",
    
"ajax" => 1
));

$form->addHidden("cmd""submit_1");
$form->addTextbox("Enter your address below to view the geocoded latitude/longitude.""Address""", array("required" => 1));
$form->addHTMLExternal('<div id="geocodingInfoDiv" style="display: none;">');
$form->addTextbox("Recognized Address:""RecognizedAddress""", array("readonly" => "readonly"));
$form->addTextbox("Latitude/Longitude:""LatitudeLongitude""", array("readonly" => "readonly"));
$form->addHTMLExternal("</div>");
$form->addButton("Submit");
$form->render();

?>
<script type="text/javascript">
    function parseJSONResponse(latlng) {
        var formObj = document.getElementById("ajax_1");
        if(latlng.status == "OK") {
            var result = latlng.results[0];
            formObj.RecognizedAddress.value = result.formatted_address;
            formObj.LatitudeLongitude.value = result.geometry.location.lat + ', ' + result.geometry.location.lng;
        }
        else {
            formObj.RecognizedAddress.value = "Address Unrecognized by Google's Geocoding API";
            formObj.LatitudeLongitude.value = "N/A";
        }    
        document.getElementById("geocodingInfoDiv").style.display = "block";
    }
</script>
?>