1

My form is submitting faster than ajax can update my textarea. How can I delay the form from submitting so the ajax can update my textarea?

$form['submit']['#ajax'] = array(
'callback' => 'getCampaignString',
'event' => 'submit',
'wrapper' => 'Comments_replace',
'method' => 'replace',
);
$form['submitted']['Comments']['#prefix'] = '<div id="Comments_replace">';
$form['submitted']['Comments']['#suffix'] = '</div>';

 function getCampaignString(&$form, &$form_state){
    $campaingString = $form_state['values']['submitted']['campaignstring'];
    $comments = $form_state['values']['submitted']['Comments'];
    $comments = $comments."[".$campaingString."]";
    $form['submitted']['Comments'] = $comments;

    $form['submitted']['Comments'] = array(
        '#type' => 'textarea',
        '#title' => t('New Comments'),
        '#name' => 'submitted[Comments]',
        '#value' => $form['submitted']['Comments'],
    );

    return $form['submitted']['Comments'];

}

So basically I have this webform for getting leads. There is a textarea for comments on the form. What I need to do is append a string to the end of the comments text on POST. For example: Say they made a comment like "I want more information" and when they hit submit. I need to add a string to the end of the comments so in the post it will look like this: I want more information - campaignName. The only way I have found to add my campaingname to the post of the comments is to replace the textarea with the new information in it but I cannot seem to replace it fast enought when submitted.

  • 2
    Maybe it's just me, but I have no clue what you want. Please outline the functionality of your form, what should it do? And what is the situation that doesn't work for you? – berliner Jan 31 '14 at 1:56
  • So basically I have this webform for getting leads. There is a textarea for comments on the form. What I need to do is append a string to the end of the comments text on POST. For example: Say they made a comment like "I want more information" and when they hit submit. I need to add a string to the end of the comments so in the post it will look like this: I want more information - campaignName. The only way I have found to add my campaingname to the post of the comments is to replace the textarea with the new information in it but I cannot seem to replace it fast enought when submitted. – Django Jan 31 '14 at 15:35
  • @Django You should be doing that processing on the server side, either in a validate or submit handler for the form. Relying on the speed of a user's browser (which is what you're unintentionally doing here) isn't going to be reliable – Clive Jan 31 '14 at 15:38
  • @Clive - Doing in the validation worked. – Django Feb 5 '14 at 20:27
0
function THEME_form_alter(&$form, &$form_state, $form_id) {
    $form['#validate'][] = 'webform_validate';
}

function webform_validate($form, &$form_state) {
  //Appending comments field with campaignString
  $campaingString = $form_state['values']['submitted']['campaignstring'];
  $comments = $form_state['values']['submitted']['Comments'];
  $comments = "[".$campaingString."]  " . $comments;
  $form_state['values']['submitted']['Comments'] = $comments;
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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