Rectangle 27 0

I think the easier way is with javascript / Jquery. You create a Jquery script which is called only in certain types of nodes and pass the number of views from drupal to the jscript.

You can call drupal_add_js() inside your module_preprocess_node and pass a variable which contains the number of views or the image link to the script. Something like this:

<?php
drupal_add_js("var mymodule_imagelink = " . drupal_to_js($imagelink) . ";", 'inline');
drupal_add_js("my_js_file.js");
?>

then in my_js_file.js just change the text. There are several ways to acomplish this. For instance, you can do a search in the document and change the title to something else or you can use a determined ID, etc...

Add HTML to node title in Drupal module, not in theme layer - Stack Ov...

drupal drupal-6 drupal-modules
Rectangle 27 0

Turns out I was able to get it working using a revision of Ionut.A's suggestion;

Drupal.behaviors.ajaxview = function(context) {
    $(".ajaxclick").click(function(){
    var nid = $(".mynid").eq($('.ajaxclick').index( $(this) )).val();
    $('.container').eq($('.ajaxclick').index( $(this) )).load(Drupal.settings.basePath + 'myajax/' + nid);
    return false;
});
}

and for the html;

<input type="hidden" class="mynid" value=<?php echo $node->nid;?> />

I had to use class instead of id as the id would only return the id from the first node.

php - Loading Ajax for each node in Drupal view with nid argument - St...

php jquery ajax drupal view
Rectangle 27 0

As you're adding a setting with a parent called staff_filter your settings will be available under a settings object with the same name in javascript. Your path will be here:

Drupal.settings.staff_filter.path

I got it working earlier but thank you!

Drupal 7 php variable to jquery - Stack Overflow

jquery drupal variables module drupal-7
Rectangle 27 0

Panels main usage, is not loading content without page loads. It's primarily used

  • to create different page layouts that depends on certain criteria, that you can setup with code, or in the AI.
  • Let the content of the page, be aware of which content is being viewed, and tying to different content together.

Your current script will work, but it's a bit crude in it's current form. Fx, what will happen if a user clicks several times. If you want to dynamically update your content, you should only change the parts that needs changing instead of loading/changing the whole page. Then you might as well just load the new page instead. I guess this is where Panels can help you, but I haven't tried using Panels like that.

thanks, so the question here is: how can I only load the node content instead of the usual page ? What's the link to a node ? (of course without having to use Panels) thanks

@Patrick: ATM, there is not a way to just load the content of a node, but it should be fairly simple to make a module that does this. What makes this a bit more tricky is that not all pages is node views.

I think I've solved. I'm passing a parameter "onlyNode=true" together with the link, and I've updated my template in order to ignore the page content if this variable is true. In this way the selection is done server side and I'm not loading the entire page.

php - Drupal: Loading AJAX content in Drupal - Stack Overflow

php jquery drupal drupal-panels
Rectangle 27 0

From you question I understand that, you're trying to fetch field from database and depending on those field wanna to add fields to HTML.

Step 1: Send request to php file through AJAX and get the list of field [response may be HTML, JSON etc].

Step 2: If response is HTML then set the whole HTML to container/ if json then make looping on them and add them to container

Thanks. I solved the problem by just triggering the button by .mousedown() However, your solution probably not gonna work as the whole postdata is huge and cant construct it via human code. Thank you for instant reply

php - How to simulate "add another item" via JS or Jquery in Drupal 7,...

php javascript jquery drupal
Rectangle 27 0

So, on front-end side you can use jQuery, as guys explained above. On Drupal side, make some php script, add to it's beginning common Drupal bootstarp:

define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

After that you can use all drupal features - drupal is initialized. So, you can read the views, load nodes, what ever. Generate what ever you need (html, json) and print it out.

And you'll call that script with your ajax call. You can of course pass the parameters...as usual.

jquery - Div Refresh without page load using ajax drupal - Stack Overf...

jquery ajax drupal drupal-ajax
Rectangle 27 0

mapresults_ajax is not returning anything. in this case, I would use print instead of return to send the results back to javascript without returning the whole HTML page.

php - Drupal 7 ajax not working properly (success function called but ...

php jquery ajax json drupal
Rectangle 27 0

You can use drupal_json_output instead, this will automatically change the headers so no exit() is required.

Another option would be to change the delivery callback on that particular path combined with the data from $_SERVER using hook_page_delivery_callback and then return a valid array for the page using ajax_command's.

The first option is alot easier, I've never tried the second one myself but it might be too much for your situation. Unfortunately I don't know how either of these techniques behaves together with boost but hopefully one of them will work.

Well I found this option too. But the output is not cached.

php - Best way to load data with AJAX in Drupal 7 - Stack Overflow

php jquery ajax drupal drupal-7
Rectangle 27 0

If I am not mistaken, and understand correctly what you trying to achieve, you don't need PHP for this. Just make sure that you initialized Jquery Title Alert correclty, and replace this line

if ( displayAlert ) { inject_alert(alertMsg,alertId); }

with this

if ( displayAlert ) { inject_alert(alertMsg,alertId); $.titleAlert("Your message here!");}

if you would like to add the original alert text which appears, change the "Your message here!" to jQuery(this).find(".alert-msg").html()

I am not a smart man. Of course it's just a Javascript call, not a PHP one. Yikes. I gave this a try but sadly still no go. The only thing I can think of is that the .js file that has $titleAlert is separate from mine which is calling it, but both are included on the main page where this is all being called from. Do I need to some include the other file into mine (My search says there is no such thing for Javascript).

Could you tell us the html code from the header, where you include js files? After modifiying the code, the rest of the script works as intenteded?(is the old alert showing?) Please also check, that your jquery is included BEFORE or AFTER TitleAlert. You don't have to include the js file in any other javascript, just make sure, that Jquery is called BEFORE TitleAlert.

Sorry, I decided to get sick for a few days there. The alert still displays normally with the new code. And it looks like jquery is called before title alert (Jquery is called first out of all the .js files).

Ok, this is the answer. The TitleAlert code is just not running nicely when inside the site. I threw some other custom code in as you suggested and it worked fine. Thanks for the help. I'll have to debug or find another method to make the title flash.

Editing Jquery Ajax function to call PHP function - Stack Overflow

php jquery ajax drupal
Rectangle 27 0

<?php echo  $node->nid; ?>

You will have to use jQuery approach to get the nid you want. For example, if you have the nid in a hidden filed like:

<input type="hidden" id="ID" value="100" />

You can use the following jQuery to get the nid:

var nid = $("#ID").val();

Oddly this does the same thing as the <?php echo $node->nid; ?> which is to send only the last nid as an argument.

php - Loading Ajax for each node in Drupal view with nid argument - St...

php jquery ajax drupal view
Rectangle 27 0

mapresults_ajax is not returning anything. in this case, I would use print instead of return to send the results back to javascript without returning the whole HTML page.

php - Drupal 7 ajax not working properly (success function called but ...

php jquery ajax json drupal
Rectangle 27 0

At first, here is some explanations: 1. So you trying to render drupal messages as part of the form, you must to render drupal messages manually. 2. With using '#ajax' key, there is no additional (custom) JS required. 3. On the button click the form is rebuilded, so, all you need is to place message rendering code inside the form logic.

Code example for the simple case:

function test_error_form(&$form, &$form_state) {
  $form['test_error'] = array(
    '#type' => 'button',
    '#value' => t('Error'),
    '#ajax'  => array(
      'callback' => 'test_error_error',
      'wrapper'  => 'the-error-container',
    ),
  );
  $form['test_warning'] = array(
    '#type' => 'button',
    '#value' => t('Error'),
    '#ajax'  => array(
      'callback' => 'test_error_warning',
      'wrapper'  => 'the-error-container',
    ),
  );

  return $form;
}

function test_error_error($form) {
  drupal_set_message("Header error", 'error');
  return array(
    '#type' => 'markup'
    '#markup' => theme('status_messages'),
  );
}
function test_error_warning($form) {
  drupal_set_message("Header warning", 'warning');
  return array(
    '#type' => 'markup'
    '#markup' => theme('status_messages'),
  );
}

Note, that you must have div with id="the-error-container" on the page.

If you want to render new drupal messages on the standard place, try to use ajax commands, like this:

$commands = array();
$commands[] = ajax_command_replace($selector, theme('status_messages'));
return array(
  '#type' => 'ajax',
  '#commands' => $commands,
);

where $selector - is a css/jquery selector of your message area.

php - Using Drupal AJAX with Drupal Messages - Stack Overflow

php jquery ajax drupal drupal-7
Rectangle 27 0

Should have been 'page arguments'. It's always the small things.

php - Ajax loading in Drupal using Ctools breaks - Stack Overflow

php jquery drupal formapi drupal-ctools
Rectangle 27 0

Essentially, you need to create a menu item that delegates to a callback (be careful with your permissions) and then call it with $.ajax(). If you need to do something with the response, see the json_encode functionality.

You might want to take a look here for some menu examples:http://drupal.org/project/examples

I think there is a Drupal interface to the jQuery ajax API. Perhaps someone can comment if they know a better way.

php - How to pass jquery variable to drupal (ajax) - Stack Overflow

php drupal
Rectangle 27 0

Since you want to produce the 'add another item' via javascript, that means you need to perform the click event of the button 'add another item'.

The jQuery .trigger() does not execute the action of the button #ajax. So you just need to use:

// Base id button 'add another item'.
 base='edit-field-etape-und-add-more';
 id= '#edit-field-etape-und-add-more';
 $(id).click();
 Drupal.ajax[base].eventResponse($(id), 'click');

php - How to simulate "add another item" via JS or Jquery in Drupal 7,...

php javascript jquery drupal
Rectangle 27 0

Looks like Drupal.attachBehaviors should have fired on (target) instead of (context). It was refiring the script on the whole context instead of just the new ajaxed content. So I changed Drupal.attachBehaviors(context); to Drupal.attachBehaviors(target);

php - Ajax loaded content referencing wrong node id in Drupal - Stack ...

php jquery drupal views
Rectangle 27 0

I think the nicest way to do it would be to have your form accessible from the normal URL (for those without JavaScript) and available to AJAX. You can do that like this:

function rmessage_menu() {
  $items['sendmessage/%node'] = array( // Using the '%node' load argument ensures that the nid attempting to be accessed belongs to an existing node.
    'title' => 'Send Message',
    'description' => 'Send a message',
    'page callback' => 'rmessages_message_form',
    'page arguments' => array(1),
    'access callback' => TRUE
  );

  return $items;
}

function rmessages_message_form($node) {
  $form = drupal_get_form('rmessage_send_message_form', $node->nid);

  // Just print the form directly if this is an AJAX request
  if (isset($_GET['ajax'])) {
    echo render($form);

    // Halt page processing
    drupal_exit();
  }

  // Otherwise return the form as normal
  return $form;
}

Then in your JS you would just need to add the query string:

$('.send_message').click(function(){
   $('.send-message-dialog').dialog('open');
      $('.send-message-dialog .dialog-content').load('/sendmessage/7?ajax');
   }
);

php - How can I load a Drupal Form via ajax into a jQuery dialog? - St...

php jquery jquery-ui drupal drupal-7
Rectangle 27 0

Passing the variable data from JavaScript to Drupal/PHP is one thing. Passing the information from Drupal/PHP to JavaScript is another separate thing. I think that you meant the first one in your question. So I'll go ahead and answer that.

1) Implement a menu hook. You can do that on an existing contributed or new custom module, it depends what you are doing. It's pretty easy and straight forward, it is also well documented on the Drupal site.

$items = array();

  $items['my_custom_callback/%'] = array(
    'title' => 'My Custom Callback', 
    'description' => 'Listing of blogs.', 
    'page callback' => 'my_custom_php_function', 
    'page arguments' => array(1),
    'access arguments' => array('access content'), 
    'type' => MENU_CALLBACK,
  );

Let's break down the example above.

$items['my_custom_callback/%']

$items is just an array, and it can be named anything you like. If the URL of my website is www.alexanderallen.name, then http://www.alexanderallen.name/my_custom_callback would be the resource that I would call from my JavaScript application.

In the array's key, the percentage symbol after the word *my_custom_callback* is a placeholder. This is where you will pass your data from JS to PHP. So if the value of variable x is 10, the call from your JQuery will look like this:

http://www.alexanderallen.name/my_custom_callback/10

When you load that URL, Drupal will find your menu hook, and it will call the function you defined in the 'page callback' array key. In this case that would be:

<?php
/**
* Function that gets called from JQuery asynchronously. 
*/
function my_custom_php_function($argument) {
  // Do something with $argument...
  echo $argument;
}
?>

There you can do anything you want with the value of variable x, like storing it in the database.

If you were to put the menu hook on a custom module, and the name of your module were example_module, the menu hook and custom PHP callback would look like this:

<?php
/**
* @file example_module.module Handles fancy AJAX functionality.
* 
*/

/**
* Implementation of hook_menu
*
* @see http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hook_menu/6
*/
function example_module_menu() {
  $items = array();

  $items['my_custom_callback/%'] = array(
    'title' => 'My Custom Callback', 
    'description' => 'Listing of blogs.', 
    'page callback' => 'my_custom_php_function', 
    'page arguments' => array(1),
    'access arguments' => array('access content'), 
    'type' => MENU_CALLBACK,
  );

  return $items;
}

/**
* Function that gets called from JQuery asynchronously. 
*/
function my_custom_php_function($argument) {
  // Do something with $argument...
  echo $argument;
}

?>

If you wanted to pass multiple variables from JQuery to Drupal/PHP, you could consider JSONifying them in JavaScript, before passing it to PHP. Remember that if the payload is very large you should consider JQuery .post() instead of .get(). The PHP functions for decoding JSON are here on php.net.

Don't forget to return $items at the end of the example_module_menu() function. If you wanted to pass two or more arguments from JavaScript to PHP then the menu item would look similar to this:

function example_module_menu() {
  $items = array();

  $items['my_custom_callback/%/%/%'] = array(
    'title' => 'My Custom Callback', 
    'description' => 'Listing of blogs.', 
    'page callback' => 'my_custom_php_function', 
    'page arguments' => array(1, 2, 3),
    'access arguments' => array('access content'), 
    'type' => MENU_CALLBACK,
  );

  return $items;
}

function my_custom_php_function($arg1, $arg2, $arg3) {
  // Do something ...
  $sum = $arg2 + $arg3;

  echo "Hi". $arg1 .", X + Z = ". $sum;
}

?>

In this case I am passing three arguments to PHP. 'page arguments' specifies the part of the URL that you want to pass to PHP. If you were to write 'page arguments' => array(0),, then the argument to your PHP function would be the string *my_custom_callback*.

2) Call the menu hook from your JS code using JQuery. JQuery provides various AJAX methods. Which one you use will depend on the format of the data your dealing with and it's size, amongst other things.

Most likely you will use one of two JQuery methods. .post() or .get(), bearing in mind that that post requests will allow for a bigger data payload than get requests. So if the size of variable x = 10, you might want to stick with .get(). Both .post() and .get() methods are an extension of, or rely on the .ajax() method. The .ajax() method is more flexible because it provides you with more options, like the ability to specify the format of the data (JSON or plain-text), send a username/password along with your request, choose between synchronous or asynchronous requests, and whether the browser should cache or not the request.

This is the method signature for the JQuery .get() method:

jQuery.get( url, [ data ], [ callback(data, textStatus, XMLHttpRequest) ], [ dataType ] )

If you were going to send your data using GET to Drupal/PHP, using the example above you could do something like this:

<script type='text/javascript'>
    var name = 'alexander';  
    var x = 10;
    var z = 20;

    $.get( 
      // Callback URL.
      "http://www.alexanderallen.name/my_custom_callback/"+ name +"/"+ x +"/"+ z
    );

    </script>

Note that the usage of the data argument on the .get() method is optional, and in my example I omitted it, but achieved the same effect by concatenating the data to pass to Drupal/PHP with the rest of the URL.

If I were to run that, the HTTP GET request would look like:

http://www.alexanderallen.name/my_custom_callback/alexander/10/20

and the response would look like:

Hi Alexander, X + Z = 30

For that then you would use Behaviors, I am not entering into details since I think that wasn't your question, but you can go to the official documentation for Drupal 6 behaviors here.

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

php - How to pass jquery variable to drupal (ajax) - Stack Overflow

php drupal
Rectangle 27 0

To me the solution would be to add a new page, e.g., /site/module/page_name/json and then, on that page, have the same logic as on (page 2), except that the page returns the JSON object only. Anyhow this is the usual implementation on RESTful APIs.

If you already have the rights to modify the (page 2) content, then I would guess you have the rights to add this additional path to the module hook_menu().

I can edit page 2 and as mentioned "I have tried getting Drupal page 2 to echo a JSON object but this is done within the module and Drupal prints it out as part of the page HTML." A fix to how to get this page to echo the JSON object would be good but I really need it to relay the HTTP status it recieved from the external server.

How would you get the page to "returns the JSON object only", that is my problem. It returns an HTML output of the object within a Drupal page.

One way to do it is to call drupal_json_output() in the end of your page callback function. Did you already try that?

php - Jquery Ajax to drupal to external site, reading response - Stack...

php jquery ajax drupal drupal-7