We can use Drupal inbuilt function “drupal_add_js” to save  data on the fly.

Save data in drupal_add_js() as below:

<?php
$max_limit = 10;
$message_resp = array(
"message" => "You have exceeded your ".$max_limit." limit.
Visit the site tomorrow for more info.",
"status" => 'reached_limit'
);

//Saving message response in "istatus" array key through drupal_add_js.
drupal_add_js(array('istatus' => json_encode($message_resp)), 'setting');

?>

Retrieve data in JS format,
Below is javascript code to retrieve data in JS Drupal way:

/* checking "istatus" key has value"*/
if (Drupal.settings.istatus) {
/* This is error message coming from backed server side */
var hasError = $.parseJSON(Drupal.settings.istatus).message;
}

if (hasError) {
/* Display this error message front side overlay box content */
$('#overlayConnection').fadeIn('fast', function() {
$('#box').fadeIn(500);
$('#box .textContent p').html(hasError);
});
}

 

 

Advertisements