View Single Post
  #2  
Old 02-01-2015, 06:57 AM
Medi0cr3 Medi0cr3 is offline
 
Join Date: Aug 2014
Posts: 27
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Next we need a PHP file to take all the header requests that jquery is sending. Create a php file named "TemplateJQ.php". I Like to put JQ at the end of all my php files that have this role as its easy to identify later.
Code:
<?php
//Needed for the javascript calls
require_once('./global.php');
require_once( './includes/adminfunctions.php'); // Allows you to work with the datamanager. 
require_once('A_p.php');
require_once('A_fnc.php');
//Allows you to bring in all global vbulletin built variables
global $vbulletin; 	
$username = mysql_real_escape_string($vbulletin->userinfo['username']);
$usergroupid = $vbulletin->userinfo[usergroupid];
$membergroupids = $vbulletin->userinfo[membergroupids];
if(!$con) { die("Database connection services failed connection: <font color='red' size='4' $pagetitle</font>  " . mysql_error()); } 
mysql_select_db("DATABASE NAME", $con) or die("Database selection failed: <font color='red' size='8' $pagetitle</font> " . mysql_error());

if(isset($_POST['phptrigger'])) {
$trigger1 = mysql_real_escape_string($_POST['phptrigger']);
     //do some php here. 
    //Maybe do some database work here.
   $thisVariable = "I'm awesome";
   $thisVariable2 = "I'm awesome as well";
   $json = array('result1' => $thisVariable, 'result2' => $thisVariable2);
   echo json_encode($json);
}
if(isset($_POST['secondPHPtrigger'])) {
   $trigger2 = mysql_real_escape_string($_POST['secondPHPtrigger']);
     //do some php here. 
    //Maybe do some database work here.
   $thisVariable = "I'm awesome";
   $thisVariable2 = "I'm awesome as well";
   $json = array('result1' => $thisVariable, 'result2' => $thisVariable2);
   echo json_encode($json);
}
?>
Rule #1: Always escape the data you bring into your PHP files in preparation for database work.
Rule #2: With the Jquery function configuration we have, Jquery expects a return of data. There's options to set that differently and save time loading a page, however I like to have error control and handshake agreements between my codes. With the 2 way handshake you MUST send data back to the Jquery call. Even if it is NULL, it's still something sent back.
Rule #3: It is super important to think about how to control your errors. Always send something back to the Jquery function so you know what part of the code has failed. 10 times out of 10 if you have a PHP code failure, the way the Jquery function was handled will tell you that you had a "failure message:". Just be ontop of your game and you will soon see why. There is no way for php to send back that there was an error that I know of. Just comment out your code and ensure that you use the $Json and Echo Json to send back to your Jquery to hopefully tell you if you have an error or not. Use the "Console" in your browser. Most browser's consoles are brought up with F12. I use it heavily to ensure my code is being sent, received, and if it straight out fails, it will tell me.


I hope this helps out the new beginners that want to see some Jquery bring their custom pages to life.

-Medi0cr3
Reply With Quote
 
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01209 seconds
  • Memory Usage 1,771KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD_SHOWPOST
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)bbcode_code
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_box
  • (1)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit_info
  • (1)postbit
  • (1)postbit_onlinestatus
  • (1)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • reputationlevel
  • showthread
Included Files:
  • ./showpost.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showpost_start
  • bbcode_fetch_tags
  • bbcode_create
  • postbit_factory
  • showpost_post
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • showpost_complete