Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.

jQuery

In this section, we explain some useful explanation on how to perform jquery manipulations which you can use in your development on PHP-Fusion.


Combine serialized form ajax post data.

Sometimes when you do an ajax form post in jQuery, you need to merge another object into your post data. The idea behind this solution is to serialize the existing form data using jQuery’s $().serialize method. Then we use the $.param() utility method to create a serialized version of any javascript object or array. Simply concatenate the two and you are off to the races.

Code

var $form = $(form);
 
var data = {
 'foo' : 'bar'
};
 
data = $form.serialize() + '&' + $.param(data);
 
$.ajax({
 'type' : 'POST',
 'dataType' : 'json',
 'url' : $form.attr('action'),
 'data': data,
 
 'success': function(data) {
 //do something
 }
});



Warning:
A downside to this approach is the possibility of duplicated key names in your serialized string. Concatenating the two serialize strings won’t catch that. In that case you may be better off turning the form elements into an object and merging them using $.extend().

How to debug jQuery codes?

jQuery events are bind on event. On browser load = document.load() , on mouse click, on hover, on escape, on click of refresh button. In other words, it is event based, and therefore cannot be caught by var_dump() or the internal print_p() debugging function.

For this article, I am going to write on for Firefox, although it is same for Chrome.

Where is the Java debugger in Firefox?
1. Right Click on Browser window, and choose "Inspect Element".
2. Find a Console Tab in the newly popped up Inspector.
3. In the Console tab, highlight choose JS and Logging

Now you are set.

To start your debugging, add in console.log() into your Java codes to see what is actually happening on the code run during an event. For example:

Code

var foo = 'bar';
console.log(foo);



Code

echo form_text('Field Example', 'field_name', 'field_id', 'You will see this in console');
add_to_jquery("

$('#field_name').bind('focus', function(){
var value = $(this).val();
console.log( value );
});

");



Console log is priceless in debugging jQuery codes when handling with complex variables, arrays, and iterate over loops in real time.

Token & Ajax Forms & FUSION_NULL

Background Information
Since the increment of PHP-Fusion 9, our functions are protected by default CSRF token sniffer. Every post is accompanied by a hidden input of such:

Code






And as such during POST event, maincore.php will declare FUSION_NULL should these values are not posted. Albeit, our functions will not run. And especially the token_rings are made not to be selectable by external files as it is created randomly every refresh.

The following will enable you to post your form to be validated in convention:

In your form, you should have these.

Code

echo openform('myform', 'myform_id', 'post', FUSION_SELF);
echo form_text('Example', 'example', 'example', '');
echo form_textarea('Example2', 'example2', 'example2', '');
echo form_button('Save', 'save', 'save', 'save');
echo closeform();

add_to_jquery("

$('#save').bind('click', function(e) {

var formData = $('myform').serialize();
$.ajax({
      type: 'POST',
      url: 'your form action',
      data: formData,
      success: function(report) {
         console.log(report);
      },
      error: function(report) {
         console.log(report);
      }
});
e.preventDefault();
");



And as such, together send everything into your action file to be processed as per normal.

File: your form action () according to your paths.

Code

// will protect your ajax file from CSRF by default.
require_once dirname(__FILE__)."maincore.php";

if (defined('FUSION_NULL')) {
echo "This file is protected now. Fusion_Null declared.";
} else {
   $data['example'] = form_sanitizer($_POST['example'], '', 'example');
   $data['example2'] = form_sanitizer($_POST['example2'], '', 'example2');
   if (defined('FUSION_NULL')) {
      echo "Again, this file is protected by fusion null, because error found in validating example and example 2 ";
   } else {         
      echo 'Yes, so far so good, no FUSION_NULL found.';
   }
   // again this will not run if you have a FUSION_NULL declared.
   dbquery_insert(DB_NEWS, $data, 'save');
}



FUSION_NULL is generated by either:

Code

if (!defined('FUSION_NULL') {
 define("FUSION_NULL", true);
}



or after maincore.php is loaded...

Code

$defender->stop();