Difference between revisions of "Coding Standards AJAX"
(Initial version of this page) |
|||
Line 32: | Line 32: | ||
For example, the 'email' status return below has a player id concatenated to it. | For example, the 'email' status return below has a player id concatenated to it. | ||
Two status returns which should always be provided for are "success" and " | Two status returns which should always be provided for are "success" and "fail". | ||
Most other status returns will be editing exceptions which should be | Most other status returns will be editing exceptions which should be | ||
reported back to the player so that he can try again. | reported back to the player so that he can try again. | ||
Line 80: | Line 80: | ||
} | } | ||
} | } | ||
</pre> | </pre> | ||
=== JSON File Return Type === | === JSON File Return Type === |
Revision as of 11:50, 24 October 2013
This coding standards document is very incomplete!
At the present I intend it to cover mostly interface standards.
The JavaScript to PHP interface will be the first thing that I standardize here.
Callback Function Coding Standards
Each time an AJAX call is made by a JavaScript program to a PHP program a callback function is required to process the return from the PHP program .
The first three lines of every callback function, except those in board18index.js, handle login timeout situations. These lines are always:
if (response.indexOf("<!doctype html>") !== -1) { // User has timed out. window.location = "access-denied.html"; }
If the PHP program's auth.php code detects a timeout it will transfer control to the access-denied.html page. This has the result of sending the code for this page to the callback function. The first line of this code is <!doctype html>. The above three lines handle this situation.
Echo Status Return Type
Most AJAX called PHP programs use the echo command to return short text strings indicating the status of the return. Sometimes an extra short text field is concatenated to a particular such string. For example, the 'email' status return below has a player id concatenated to it.
Two status returns which should always be provided for are "success" and "fail". Most other status returns will be editing exceptions which should be reported back to the player so that he can try again.
This is an example of a valid callback function which handles this type of status return scheme. It was taken from board18index.js.
/* * Function regResult is the callback function * for the ajax newUser call. */ function regResult(response) { if (response.indexOf("<!doctype html>") !== -1) { // User has timed out. window.location = "access-denied.html"; } if(response === 'success') { $('#login #password').val(''); $('#login :text').val(''); $('#register form').slideUp(300); $('#login form').slideDown(300); var loginNote ='You have successfully registered. '; $('#lognote').text(loginNote); } else if(response === 'duplicate') { $("#newuser_error").text('Username is already in use.').show(); $("#newuser").focus(); } else if(response === 'bademail') { $("#email_error").text('Invalid email format, please correct.').show(); $("#email").focus(); } else if(response.substring(0,5) === 'email') { var logmessage = 'Player ' + response.substring(5); logmessage += ' is already using this email address.'; $("#email_error").text(logmessage).show(); $("#email").focus(); } else if(response === 'fail') { var errmsg1 ='Program error in newUser.php.\n'; errmsg1 += response + 'Please contact the BOARD18 webmaster.'; alert(errmsg1); } else { // Something is definitly wrong in the code. var errmsg2 ='Invalid return code from newUser.php.\n'; errmsg2 += response + 'Please contact the BOARD18 webmaster.'; alert(errmsg2); } }