call javascript function within hook_form_submt() in d7

Posted: Mon, Oct 03, 2011 - 01:00pm

hi,

in the following code , in form_submit() function I am trying to call javascript function but its not working(even alerts are not poping up  )

 I have created signup.js under my module's directory where signup is my module name.

But call to javascript is not working..

Basically In my form_submit () function I am getting a responce from an xml file using querypath module.

Based on the responce I want to show a message in the div  ..

In d6 I am able to call javascript function within form_submit()

and everithing works fine..

Ihave include misc/jquery.js file also..

Can you please suggest me ..what cahnges do I need to do.

 

signup_module_upload_form_submit($form, &$form_state) { // signup_module_upload_form is form name

  drupal_add_js('jQuery(document).ready(function () { alert("Hello1!"); });', 'inline');

drupal_add_js(drupal_get_path('module','signup') . "/signup.js");

  drupal_add_js('jQuery(document).ready(function () { hideMessage1("'.$email_id.'","'.$someurl.'","'.$signup_request_sent.'"); });', 'inline');

}

Hi Shanbhang, please check

  • Mon, Oct 03, 2011 - 04:22pm

Hi Shanbhang, please check the docs. The second arguments to drupal_add_js in D7 is an array:
http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_add_js/7 

__________________

Rahul Singla
Signature

drupal_add_js not working in d7

  • Mon, Oct 03, 2011 - 06:50pm

HI ,

 

here is my function:

function signup_module_upload_form_submit($form, &$form_state) {
drupal_add_js('jQuery(document).ready(function () { alert("Hello!"); });',
    array('type' => 'inline')
  );

require_once 'sites/all/modules/querypath/QueryPath/QueryPath.php';

require_once  'bvurl.php';

$bvurl = setURL();
$first_name=0;

$last_name=0;

$email_id=$_POST['email_field'];

$company_name=0;

$signup_password=0;

$signup_xml_url=$bvurl."/gpn/services/UserSignUpDetails/signUp/".$first_name."/".$last_name."/".$email_id."/".$company_name."/".$signup_password;
//$signup_xml_url=$bvurl."/gpn/gpn/services/UserSignUpDetails/signUp/0/0/wwww123@asdfs.com/0/0";

$signup_xml_response=qp($signup_xml_url,NULL,array('ignore_parser_warnings' => TRUE))->find('return')->text();

if($signup_xml_response == "1"){

        $signup_request_sent="false";

        $displaymessage = "style='display:block;'";

    }

    elseif($signup_xml_response == "-1"){

        $signup_request_sent="exists";

        $displaymessage = "style='display:block;'";

    }

    else{

        $signup_request_sent="true";

        $displaymessage = "style='display:block;'";

            $homepage_email_field= "Thanks";

                $setDef=1; // registration succes  and display default message in email textbox after 10 sec

    }

 drupal_add_js(drupal_get_path('module','signup') . "signup.js");

  drupal_add_js('jQuery(document).ready(function () { hideMessage2("'.$email_id.'","'.$bvurl.'","'.$signup_request_sent.'"); });', 'inline');
 

    }
but popup messages are not shown,

if i call the same  alert   outside this function , popup will appear...but not in form_submit()

Is your page redirected after

  • Mon, Oct 03, 2011 - 08:40pm

Is your page redirected after form submission?

__________________

Rahul Singla
Signature

Page is not redirecting..I am

  • Tue, Oct 04, 2011 - 09:21am

Page is not redirecting..I am able to see messges written using drupa_set_message..

Drupal is a smart framework,

  • Tue, Oct 11, 2011 - 12:20am

Drupal is a smart framework, messages written using drupal_set_message get saved to db and shown on next page, if the page gets redirected before the message is shown to the user.

Obviously, calls to drupal_add_js are page specific and do not get saved. The sure-shot way of checking if a redirection is happening is through Fiddler, check if there is more than 1 html request/response after you submit the form.

__________________

Rahul Singla
Signature