Hi everybody ,
I have spent couple of days trying to solve this problem :
I am trying to get the values users put into a webform I created , & I 'm trying to do this using hook_form_alter
however it never gets me those values after submit or I dunno how to reach them
it works with the search form and I have no clue why it never works with forms made by the webform module !!
I just wanna know how to get those values of a form created by webform module I also tried to use #form_after_build
with hook_form_alter , anybody can help , please ?

Comments

nevets’s picture

In your hook_form_alter() you need to add a submit handler using #submit.

brand-newbie’s picture

I'm a newbie , so I'm still looking how to add a submit handler using #submit :(
and just wanted to tell ya one more confusing thing to me that I could get whatever I type in the search module and my small module code is :

<? php
function raafatabc_form_alter(&$form, $form_state, $form_id) {
print_r ($form['basic']['inline']['keys']['#default_value']);
}

I'm pretty sure this is a cheesy way cause I dun really get what somebody puts in the main-search textfield ,

I tried to do this with the webform I created as follows:

<? php
function raafat_testing_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'webform_client_form_129'){
print_r ($form['submitted']['first_name']['#post']['submitted']['first_name']);
}

but it never worked out , I tried many paths , non of them worked that's why I am thinking of the after submit thing I dunno precisely how to do it !
In the search form I pointed at the after submit page which already has the value I typed saved in it
so what I get is not the text in the main search textfield after submit :s
sorry for talking too much , I'm really very concerned in this I wanna learn how to do it .
if you can help me alittle bit more tell me how to do this code .
thanks again :)

Raf’s picture

You're making two mistakes here:

1) You need $form_state['values']
2) hook_form_alter is called before a form is built. That means it hasn't gotten input from the user yet. The best way to do what you're trying to do, is by putting $form_state['values'] in a session variable in the submit handler and, when the session variable is set, printing it when the form's rendered (so that part can go in form_alter, or just where you print the form) and unsetting the session variable.

Basically, it comes down to this:

function mymodule_my_form_submit(&$form, $form_state) {
  $_SESSION['user_input'] = $form_state['values'];
}

function mymodule_form_alter(&$form, $form_state) {
  if (isset($_SESSION['user_input']) {
    print('<pre>');
    print_r($_SESSION['user_input']);
    print('</pre>');
    unset($_SESSION['user_input']);
  }
}

brand-newbie’s picture

Thanx Raf
but :

function mymodule_my_form_submit(&$form, $form_state) {
  $_SESSION['user_input'] = $form_state['values'];
}
function mymodule_form_alter(&$form, $form_state) {
  if (isset($_SESSION['user_input'])) {
    print('<pre>');
    print_r($_SESSION['user_input']);
    print('</pre>');
    unset($_SESSION['user_input']);
  }
}

doesn't seem to be working with me , I think my problem now is that the values are put in -- std class object -- instead of being put just in an array and I don't know how to point at that like I do in the array for example $form['first']['second']['third']['value'] , I dunno how to pick something from the std class object , thanx again

Raf’s picture

$my_array['my_value']

$my_object->my_value

brand-newbie’s picture

I guess that hook_form_submit is deprectaed from Drupal 6 :(
but I am gonna try other handlers and thanks alot for your help Raf :)

Raf’s picture

hook_form_submit works just fine in Drupal 6.
This is how your functions should be called:

modulename_formname_form(&$node)
modulename_formname_form_validate($form, &$form_state)
modulename_formname_form_submit($form, &$form_state)

So for example, if you got a module called workflow and a form called contactform, it will be as follows:
workflow_contactform_form(&$node)
workflow_contactform_form_validate($form, &$form_state)
workflow_contactform_form_submit($form, &$form_state)

Maybe my explanation wasn't clear enough. I'm sorry if it was a bit confusing with the my_form part.

brand-newbie’s picture

Man , your explanation is perfect , it's just me I'm a newbie in Drupal and php and all those things
I dunno really how to thank you for all what yot taught me .
I tried everything with the code , but it just seems that the hook_form_name_form_submit() is not working with me cause whatever functions I put inside it , I get nothing , that's why I thought It's deprecated ! but I revised and it doesn't say it's deprecated or whatsoever , and I'm really very thankful for ya man !:)

Raf’s picture

So it works now? :D

brand-newbie’s picture

still working to understand how the hook_myform_form_submit functions cause like I said i dunno whatever function I put into it , it doesn't function , and I dunno how to solve this yet I am trying to understand it a little bit more

Raf’s picture

You yourself won't call hook_myform_form_submit. Drupal will do that for you.

In basic terms, it works like this:

1) You get and print the form
2) A visitor on your site fills the form in and submits it
3) Drupal will go through all functions mentioned in $form['#validate'] to validate the entire form
4) If it passes all validations, Drupal will go through all functions mentioned in $form['#submit'] and executes the code mentioned in there.
5) The site redirects to the page set to redirect to. This, by default, is the same page as the one the form is on. In other words: a fresh, new version of the form will be rendered.

$form['#validate'] and $form['#submit'] both are arrays. By default, they have hook_myform_form_validate and hook_myform_form_submit as first function in there.

If you want to get some output from hook_myform_form_submit, you need to put it in a session variable, since the page renders a fresh version of the form. That means all submitted values are reset, unless you store them in a way you can carry across pages (meaning a session variable or a cookie. A session variable's better for accessibility reasons)

So basically, you don't need to call hook_myform_form_submit. You just need to provide it and put all the code you need for handling the input in that function.

brand-newbie’s picture

Man , I am sorry again , but I've been trying this for three days now , no results whatever things Iput in the submit func. never works , I mean this never functions with me I dunno where do I go wrong :S

session_start();
function raafat_testing_webform_client_form_1_form_submit(&$form, $form_state) {

// whatever functions or code I put here never works ...!!??

$_SESSION['user_input'] = $form_state['#parameters'];
}

function raafat_testing_form_alter(&$form, $form_state) {
if ($form_id == 'webform_client_form_1'){
if (isset($_SESSION['user_input'])) {

/* of course also here , cause I thing the thing is with  $_SESSION , I guess the session I make is not known by drupal or something .... */

                   print('<pre>');
                   print_r($_SESSION['user_input']);
                   print('</pre>');
                   unset($_SESSION['user_input']);
}
}

}

brand-newbie’s picture

eventually I turned to use another system to make the form by myself using the code not the webform module , and this works for me but I'm still looking for a way where I can control the webforms created by webform module , anyways here's the code I used to create and submit a webform then prints out all inputs put by user( so I can use them send them to other fields in other forms ) :) :

<?php
function catch_form_get_form_input(){
$all_webform_param="";
foreach ($_REQUEST as $key=>$value){
$all_webform_param[$key]=$value;
}

submit_to_civicrm($all_webform_param);
return submit_to_civicrm($all_webform_param);
}
function submit_to_civicrm($param){
$result=getURL_by_curl('http://192.168.1.5/arabacademy/abdo/drupal_crm/index.php?q=civicrm/contact/add",$param');
return$result;
}

/*$form['#action'] = url("http://192.168.1.5/arabacademy/abdo/drupal_crm/index.php?q=civicrm/contact/add", array('absolute' => true));
//"http://192.168.1.5/arabacademy/abdo/drupal_crm/index.php?q=civicrm/contact/add"
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit')
);
return '#action functions';
*/

function catch_form_form_alter(&$form, $form_state, $form_id) {
if ( ereg('^webform_client_form_', $form_id) ) {

global $base_url;

$form['#action'] = url("catch_form", array('absolute' => true));
$form['submitted']['#tree'] = FALSE;
}
   }
/**
* Add menu item for administering the module.
*/

function catch_form_menu() {
$items['catch_form'] = array(
'title' => t('catch_form'),
'description' => t('Web form catch of all fields'),
'page callback' => 'catch_form_get_form_input',
'access arguments' => array('access pages'),
'type' => MENU_NORMAL_ITEM
);

return $items;
}

function getURL_by_curl($script_path, $script_parameters_arr){
try{
$script_parameters="";

foreach($script_parameters_arr as $key=>$value){
$script_parameters .=$key.'='.urlencode($value).'&';

}
rtrim($script_parameters,'&');

$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, $script_path);
curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, 1);
$tt = curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl, CURLOPT_POSTFIELDS, $script_parameters);
$result22 = curl_exec($curl);
curl_close($curl);
return $result22;
}catch(Exception $e){
echo $e->getMessage();
}
}

brand-newbie’s picture

I'm trying to do a new thing now , to get all variables from the webform and send them to civicrm byt this module , but things are going wrong in a way :S would you kindly tell me where do I go wrong ?

<?php
function catch_form_get_form_input(){   
   
$all_webform_param="";
    foreach (
$_REQUEST as $key=>$value){
       
$all_webform_param[$key]=$value;
    }
   
   
submit_to_civicrm($all_webform_param);
    return
submit_to_civicrm($all_webform_param);
}


function
submit_to_civicrm($param){

   
   
$result=getURL_by_curl('http://192.168.1.5/arabacademy/abdo/drupal_crm/index.php?q=civicrm/contact/add",$param');
   
   
   
//print_r('submit_to_civicrm functions');
    //foreach ($param as $key=>$value){
   
    //    $form[$key]=$value;
    //     return $form;
   
   
return$result;
    }



/*$form['#action'] = url("http://192.168.1.5/arabacademy/abdo/drupal_crm/index.php?q=civicrm/contact/add", array('absolute' => true));
    //"http://192.168.1.5/arabacademy/abdo/drupal_crm/index.php?q=civicrm/contact/add"
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Submit')
        );

     // catch_form_submit_form($form_id, $form, $callback = NULL);
    //    drupal_submit_form($form);

    return '#action functions';
*/



function catch_form_form_alter(&$form, $form_state, $form_id) {

   
// handle webform client forms (instances of webforms that are submitted)   
    // webform names client forms with the pattern web_form_client_form_$nid, use this knowledge to
    // manipulate the forms that webform displays
   
if ( ereg('^webform_client_form_', $form_id) ) {       

        global
$base_url;
       
// debug output shows that the node is the third parameter -- fragile but the only way we can access it


        // change the action to submit directly to sugar
        //$form['#action'] = url($node->webform['sugarsettings']['sugarcrm_action']);   

       
$form['#action'] = url("catch_form", array('absolute' => true));   
               
       
// flatten the submitted fields so that sugar can handle them   
       
$form['submitted']['#tree'] = FALSE;
        
//$redirect = url('node/'. $node->nid .'/done', array('absolute' => true));
   
//    $form['redirect'] = array(      '#type' => 'hidden',      '#value' => $redirect,    );

    
}
    
// handle editing of webform nodes
  
}
/**
 * Add menu item for administering the module.
 */


function catch_form_menu() {
$items['catch_form'] = array(
       
'title' => t('catch_form'),
       
'description' => t('Web form catch of all fields'),
       
'page callback' => 'catch_form_get_form_input',
       
'access arguments' => array('access pages'),
       
'type' => MENU_NORMAL_ITEM
   
);

return
$items;
}


function
getURL_by_curl($script_path, $script_parameters_arr){
    try{
       
$script_parameters="";
       
        foreach(
$script_parameters_arr as $key=>$value){
       
$script_parameters .=$key.'='.urlencode($value).'&';
       
        }
   
rtrim($script_parameters,'&');
   
   
$curl = curl_init();
   
curl_setopt ($curl, CURLOPT_URL, $script_path);
   
curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, 1);
   
$tt = curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
   
curl_setopt ($curl, CURLOPT_POSTFIELDS, $script_parameters);
   
$result22 = curl_exec($curl);
   
curl_close($curl);
    return
$result22;
}catch(
Exception $e){
    echo
$e->getMessage();
}
}
?>
Raf’s picture

1) Don't use $_REQUEST. The Form API's made in such a way that it cleans your input. If you use $_REQUEST, you'll need to do the cleaning yourself. Easier and better to let the FAPI handle that.
2) Exactly how's it going wrong? And did you get any error messages? Can't go through this code, finding what's wrong, if I don't know what's wrong ;)
3) Did you write this yourself? :P

brand-newbie’s picture

I am just a newbie how could I type it myself :S , i got something from the sugarCRM-webform integration and used it and the curl thing I got it from books well i dun get any error messages but still is not working properly I mean I get what I print sometimes and sometimes not , but the thing is I never get the values i wrote all of this to get :s which is alittle bit too weird :(

Raf’s picture

Do a print of $form_state['values'] and see if that contains what you filled in after submitting the form.