<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
  <title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
John, <br>
I'm not sure exactly what you mean by "a hook function".&nbsp; Are you
implementing an already defined hook or are you defining a hook for
other modules to implement?<br>
You can use drupal_add_js() anywhere you want JS to be inserted into a
page, so yes, within hook functions too.<br>
<br>
If the resource is on a different server, that's fine, then the Drupal
php will only be acting as a client in your situation.&nbsp;&nbsp; I think what
you are looking for is a way to write php code that will get translated
into JS ajax code magically via the platform?&nbsp; That does happen in some
platforms (such as CakePHP) but not in Drupal 6.&nbsp; In Drupal 6, there is
some integration between the form api and AHAH calls (see: <a
 href="http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/6#ahah">http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/6#ahah</a>)
but if you want to do pure AJAX, you'll need to write the JS calls and&nbsp;
success handler yourself and then add that JS code to the page with
drupal_add_js().<br>
<br>
Example of a form function:<br>
&lt;?php<br>
function example_my_form() {<br>
&nbsp; $form['email'] = array(<br>
&nbsp; &nbsp; '#type' =&gt; 'textfield', <br>
&nbsp; &nbsp; '#title' =&gt; t('E-mail'), <br>
&nbsp;&nbsp;&nbsp; '#attributes' =&gt; array('id' =&gt; 'example-my-form-email');<br>
&nbsp; );<br>
&nbsp; $form['bttn'] = array(<br>
&nbsp; &nbsp; '#type' =&gt; 'button', <br>
&nbsp;&nbsp;&nbsp; '#value' =&gt; t('Click Me'), <br>
&nbsp;&nbsp;&nbsp; '#attributes' =&gt; array('id' =&gt; 'example-my-form-button');<br>
&nbsp; );<br>
&nbsp; $form['div'] = array(<br>
&nbsp;&nbsp;&nbsp; '#value' =&gt; '&lt;div id="example-my-form-div"&gt;&lt;/div&gt;',<br>
&nbsp; );<br>
<br>
&nbsp; $js = "<br>
&nbsp;&nbsp;&nbsp; Drupal.behaviors.example = function() {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $.ajax({<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; url : 'path/to/my/external/tomcat/resource?email=' +
$('#example-my-form-email').val(),<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; success : function(data) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // parse the return data and add it to the div or whatever...<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $('#example-my-form-div').html(data.someValue);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; });<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp; ";<br>
&nbsp; drupal_add_js($js, 'inline');<br>
<br>
&nbsp; return $form;<br>
}<br>
?&gt;<br>
<br>
<br>
If you want to do AJAX in Drupal 7, you can use the above approach, or
have a look again at the link in my first email.<br>
<br>
Seth<br>
<br>
John Mitchell wrote:
<blockquote
 cite="mid:k2tfa2ac0581004150558g843784bk17b24c096db02217@mail.gmail.com"
 type="cite">
  <pre wrap="">So you start with a form and within the form you use drupal_add_js to
embed the ajax within the form.  Correct?

If I have a custom module that has a hook function and within the hook
function can I just use drupal_add_js to do an ajax call?

Sorry for the stupid questions I am new to Drupal and php.

Thanks,

John

On 4/15/10, Jamie Holly <a class="moz-txt-link-rfc2396E" href="mailto:hovercrafter@earthlink.net">&lt;hovercrafter@earthlink.net&gt;</a> wrote:
  </pre>
  <blockquote type="cite">
    <pre wrap="">Look at drupal_add_js

<a class="moz-txt-link-freetext" href="http://api.drupal.org/api/function/drupal_add_js">http://api.drupal.org/api/function/drupal_add_js</a>


Jamie Holly
<a class="moz-txt-link-freetext" href="http://www.intoxination.net">http://www.intoxination.net</a>
<a class="moz-txt-link-freetext" href="http://www.hollyit.net">http://www.hollyit.net</a>


On 4/15/2010 6:51 AM, John Mitchell wrote:
    </pre>
    <blockquote type="cite">
      <pre wrap="">I am trying to make this as simple as possible:

As an example (listed below) I have a javascript ajax call to a java
servlet within my existing Apache Tomcat web application not Drupal.
All that would need to be modified would that the URL have to be the
full path.

Is their a way within Drupal to embed a javascript call similar to the
one listed below?

Thanks,

John

&lt;script type = "text/javascript"&gt;
function download(link){
       var email = document.getElementById('email');
       var xmlHttpReq = new XMLHttpRequest();
       var url = 'ecommerce?command=serve&amp;name=' + link + '&amp;email=' +
email.value;
       xmlHttpReq.open('post', url, true);
       xmlHttpReq.onreadystatechange = function() {
             if (xmlHttpReq.readyState != 4)  {
                   return;
             }
             else {
                   var responseText = xmlHttpReq.responseText;
                   return false;
             }
       }
       xmlHttpReq.send(null);
}
&lt;/script&gt;



On 4/15/10, sumeet pareek<a class="moz-txt-link-rfc2396E" href="mailto:positivecharge@gmail.com">&lt;positivecharge@gmail.com&gt;</a>  wrote:
      </pre>
      <blockquote type="cite">
        <pre wrap=""> Does this help - <a class="moz-txt-link-freetext" href="http://drupal.org/node/44895">http://drupal.org/node/44895</a> ?

 On Thu, Apr 15, 2010 at 3:43 PM, John Mitchell<a class="moz-txt-link-rfc2396E" href="mailto:mitchelljj98@gmail.com">&lt;mitchelljj98@gmail.com&gt;</a>
 wrote:
        </pre>
        <blockquote type="cite">
          <pre wrap=""> Let me rephrase my question: I currently within the same server have
 Apache/Drupal and Apache Tomcat/Java.  I would like to be able within
 a Drupal module's hook do an ajax call to a Java Servlet.

 How would I do this within Drupal version 6?

 For future reference how would I do this within Drupal version 7?

 Thanks,

 John

 On 4/14/10, Seth Freach<a class="moz-txt-link-rfc2396E" href="mailto:sfreach@gmail.com">&lt;sfreach@gmail.com&gt;</a>  wrote:
          </pre>
          <blockquote type="cite">
            <pre wrap=""> John Mitchell wrote:
            </pre>
            <blockquote type="cite">
              <pre wrap=""> How would I do an ajax call within my own custom module?

 Thanks,

 John


              </pre>
            </blockquote>
            <pre wrap=""> You will probably need 2 menu items:
  - The first to define the page that will be the ajax client.  Ie,
the
 url of the page that this all happens on, a regular drupal page that
has
 called drupal_add_js() to add some JS code that will do a
$.ajax({...});
 call (or other jquery asynchronous call).  This menu item might not
be
 needed though if the JS is added in a block, or inserted via
 hook_form_alter or hook_nodeapi, etc.
  - The second menu item will define the ajax server.  It should be
 'type'=&gt;MENU_CALLBACK and have the appropriate access checks defined
as
 well.  The callback function associated with this path should,
instead
 of returning themed output, end with: drupal_json(array(...));
exit();
 where the array(...) is an associative array that you want to hand
back
 to the calling page in JSON format.

 If you're doing this for D7, look at:

<a class="moz-txt-link-freetext" href="http://api.drupal.org/api/drupal/developer--examples--ajax_example--ajax_example.module/7">http://api.drupal.org/api/drupal/developer--examples--ajax_example--ajax_example.module/7</a>

 Seth
 --
 [ Drupal support list | <a class="moz-txt-link-freetext" href="http://lists.drupal.org/">http://lists.drupal.org/</a> ]

            </pre>
          </blockquote>
          <pre wrap="">
 --
 John J. Mitchell
 --
 [ Drupal support list | <a class="moz-txt-link-freetext" href="http://lists.drupal.org/">http://lists.drupal.org/</a> ]

          </pre>
        </blockquote>
        <pre wrap="">

 --
 Cheers
 Sumeet Pareek
 --
 [ Drupal support list | <a class="moz-txt-link-freetext" href="http://lists.drupal.org/">http://lists.drupal.org/</a> ]

        </pre>
      </blockquote>
      <pre wrap="">

      </pre>
    </blockquote>
    <pre wrap="">--
[ Drupal support list | <a class="moz-txt-link-freetext" href="http://lists.drupal.org/">http://lists.drupal.org/</a> ]

    </pre>
  </blockquote>
  <pre wrap=""><!---->

  </pre>
</blockquote>
</body>
</html>