I'm building a form similar to attached image. enter image description here

Customer enters values for all 3 text fields and click 'Get Online quote'. This will perform a database query (using AJAX) and display a value just below 'Get Online quote' button.

If he's happy with the quote, can ask to get contacted by clicking the second button, 'Contact me for more details'. Therefore second button act as the form submit button which will redirect to a thank you page.

So far I have coded like below.

// online quote button
function fence_quote_form_alter( &$form, &$form_state,$form_id ){
  if ($form_id == 'webform_client_form_4208') {
    $form['actions']['fence_quote']  = array(
    '#type' => 'submit',
    '#value' => t('Get online estimate'),
    '#submit' => array('fence_quote_custom_submit'),
    '#ajax' => array(
      'callback' => 'fence_quote_form_callback', 
      'wrapper' => 'fence_quote-submit-button', 
    $form['#validate'][] = 'fence_quote_custom_validate';

function fence_quote_custom_validate($form, &$form_state) {
  // your validation code

function fence_quote_custom_submit($form, &$form_state) {
  $query = db_select('fence_900', 'f');
  $query->fields('f', array('total_cost'));
        ->condition('meters', 2, '=');
  $result = $query->execute();

function fence_quote_form_callback($form, &$form_state) {

  • I'm unclear on what to put under AJAX callback function. Should I have the database query in it instead of putting inside custom submit function?
  • How can I display the database query value below the 'online quote' button on a new text field? I'm unable to print using $form_state['values']['total']

1 Answer 1

  1. Db Query in Ajax callback. I would suggest to change type of Ajax-Button from submit to button.

  2. In callback return instead of print. You should also use ->fetchAll() for instance after ->execute()

  • If I add DB query in Ajax callback, what should I put under custom submit handler? I have done the modifications as you told me and now when I click on 'online estimate' button, keeps loading the GIF 'Please wait...' Oct 29, 2015 at 2:27
  • I see a typo in your query. There is a semicolon after $query->fields... Oct 29, 2015 at 7:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.