Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Pre-submission confirmation for Gravity Forms, follow-up to http://gravitywiz.com/2012/04/30/simple-pre-submission-confirmation/
<?php
function gf_populate_confirmation_preview_html( $form ) {
$confirmation_slug = "Kinnitamine"; # content target HTML field has to have this name
$current_page = GFFormDisplay::get_current_page( $form[ "id" ] );
$output_field = false;
$has_product_fields = false;
# http://www.gravityhelp.com/forums/topic/gform_post_paging-parameters-dont-match-form-pagination-pages-why
if ( in_array( $confirmation_slug, $form[ "pagination" ][ "pages" ] ) && $form[ "pagination" ][ "pages" ][ $current_page - 1 ] == $confirmation_slug ) :
foreach( $form[ "fields" ] as &$field ) :
if ( GFCommon::is_product_field( $field[ "type" ] ) ) $has_product_fields = true;
if ( $field[ "label" ] == $confirmation_slug ) $output_field = &$field;
endforeach;
# based on gravityforms/common.php v1.6.4.5.1 L866+
if ( $output_field ) :
$content = "";
$products = array();
foreach( $form[ "fields" ] as &$field ) :
if ( RGFormsModel::is_field_hidden( $form, $field, array() ) || rgar( $field, "adminOnly" ) ) continue;
$value = RGFormsModel::get_field_value( $field );
if ( GFCommon::is_empty_array( $value ) ) continue;
$label = sprintf( "<p class='field-label'>%s</p>", __( $field[ "label" ], "gravityforms" ) );
$debug = sprintf( "<p class='field-value'>%s %s</p>", $field[ "type" ], print_r( $value, true ) );
switch ( $field[ "type" ] ) {
case "captcha":
case "html":
case "page":
case "password":
case "section":
case "total":
break;
case "address":
$content .= $label;
$content .= "<ul class='" . $field[ "type" ] . "'>";
foreach ( $value as $k => $v ) if ( $v ) $content .= "<li>" . __( $v, "gravityforms" ) . "</li>";
$content .= "</ul>";
break;
case "checkbox":
$content .= $label;
$content .= sprintf( "<ul>" );
foreach ( $value as $k => $v )
if ( $v ) $content .= "<li class='" . $field[ "type" ] . "'>" . RGFormsModel::get_label( $field, $k ) . "</li>";
$content .= sprintf( "</ul>" );
break;
case "list":
$content .= $label;
if ( is_array( $value ) ) {
$content .= sprintf( "<table class='" . $field[ "type" ] . "'>" );
if ( isset( $field[ "choices" ] ) ) {
$content .= sprintf( "<thead><tr>" );
foreach ( $field[ "choices" ] as &$c ) {
$content .= sprintf( "<td>" . $c[ "text" ] . "</td>" );
}
$content .= sprintf( "</tr></thead>" );
}
foreach ( $value as $val ) {
$content .= "<tr>";
foreach ( $val as $v ) $content .= "<td>" . $v . "</td>";
$content .= "</tr>";
}
$content .= sprintf( "</table>" );
}
break;
case "product": # products are displayed at the end
$field[ "myvalue" ] = print_r( RGFormsModel::get_field_value( $field ), true );
$products[] = $field;
break;
default:
$content .= $label;
$content .= "<p>" . $value . "</p>";
}
endforeach;
$content .= "<p class='field-label'>Tellimus</p>";
$content .= sprintf( "<table class='product'>" );
$content .= sprintf( "<thead><tr>" );
$content .= sprintf( "<td>" . __( "Product", "gravityforms" ) . "</td>" );
$content .= sprintf( "<td class='quantity'>" . __( "Qty", "gravityforms" ) . "</td>" );
$content .= sprintf( "<td class='price'>" . __( "Unit Price", "gravityforms" ) . "</td>" );
$content .= sprintf( "</tr></thead>" );
foreach ( $products as $p ) {
# based on gravityforms/form_display.php v1.6.4.5.1 L803+
if ( rgpost( "input_" . str_replace( ".", "_", $p[ "id" ] . ".3" ) ) && rgar( $p, "inputs" ) ) {
$inputs = array();
foreach ( $p[ "inputs" ] as $input ) {
$input_name = "input_" . str_replace( ".", "_", $input[ "id" ] );
$value = rgpost( $input_name );
if ( $value ) $inputs[ $input[ "label" ] ] = $value;
}
# need to do this to rearrange Quantity, Price columns
if ( ! empty ( $inputs ) && isset( $inputs[ "Price" ] ) && GFCommon::to_number( $inputs[ "Price" ] ) != 0 ) {
$content .= "<tr>";
$content .= "<td>" . ( isset( $inputs[ "Name" ] ) ? $inputs[ "Name" ] : "" ) . "</td>";
$content .= "<td class='quantity'>" . ( isset( $inputs[ "Quantity" ] ) ? $inputs[ "Quantity" ] : "" ) . "</td>";
$content .= "<td class='price'>" . ( isset( $inputs[ "Price" ] ) ? $inputs[ "Price" ] : "" ) . "</td>";
$content .= "</tr>";
}
}
}
$content .= sprintf( "</table>" );
$output_field[ "content" ] = $content;
endif;
endif;
return $form;
}
add_filter( "gform_pre_render", "gf_populate_confirmation_preview_html" );
?>
@rajatkh143

This comment has been minimized.

Copy link

commented May 19, 2015

how to use this with proper html form please provide me a sample code....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.