Setting an If Statement with AJAX in PHP
A PHP Error was encountered
Severity: Notice
Message: Undefined index: userid
Filename: views/question.php
Line Number: 191
Backtrace:
File: /var/www/html/cnasolution/application/views/question.php
Line: 191
Function: _error_handler
File: /var/www/html/cnasolution/application/controllers/Questions.php
Line: 419
Function: view
File: /var/www/html/cnasolution/index.php
Line: 315
Function: require_once
Setting an If Statement with AJAX in PHP
I am nearing completion of this code / plugin and I just have one bit to finish up before we make it all pretty and presentable; I am attempting to get my PHP file to re-load/re-fresh with the POST variables sent from the AJAX file, all from the response of the AJAX send:
$("#CategoryTree").load("poster.php #CategoryTree");
Right now on clicking a list item the CSS fires, the ajax fires and responds, variables are sent (checked them in the action registry as well all good there) but the area I call to re-load with the poster.php #CategoryTree does not react, error 500 is thrown.
Here is my full Javascript code:
jQuery(document).ready(function($) { $('#CategoryTree').on('click', 'input[type=checkbox]', function() { var $this = $(this); var data = ''; if ($this.is(":checked")) { $this.addClass('selectCheckbox'); //$this.val("1"); data = { action: 'catID_Callback', catID: $this.attr('id'), catState: 1 }; $.post(the_ajax_script.ajaxurl, data, function(response) { //alert('Got this from the server: ' + response); $("#CategoryTree").load("poster.php #CategoryTree"); //alert( "Load was performed." );}); console.log(response); }); } else { $this.removeClass('selectCheckbox'); //$this.val("0"); data = { action: 'catID_Callback', catID: $this.attr('id'), catState: 0 }; $.post(the_ajax_script.ajaxurl, data, function(response) { // alert('Got this from the server: ' + response); console.log(response); }); } }); });
And here is the PHP code (Line 55 is where the check starts):
alert('$message');"; $thearray = []; $terms = get_terms("pa_mymelp"); foreach ( $terms as $term ) { $categories = $term->name; array_push($thearray, $categories); } $categoryLines = $thearray; function buildCategoryTree($categoryLines, $separator) { $catTree = array(); foreach ($categoryLines as $catLine) { $path = explode($separator, $catLine); $node = & $catTree; foreach ($path as $cat) { $cat = trim($cat); if (!isset($node[$cat])) { $node[$cat] = array(); } $node = & $node[$cat]; } } return $catTree; } function displayCategoryTree($categoryTree, $indent = '') { foreach ($categoryTree as $node => $children) { echo $indent . $node . "
"; displayCategoryTree($children, $indent . '|- '); } } $categoryTree = buildCategoryTree($categoryLines, '/'); function displayHtmlCategoryTree($categoryTree, $id = null, $pathSeparator = '/', $parents = '') { if (empty($categoryTree)) return ''; $str = ''; foreach ($categoryTree as $node => $children) { $currentPath = $parents . (empty($parents) ? '' : $pathSeparator) . $node; $thelink = ''; $opener = 0; if (substr_count($currentPath, '/')==5){ $patterns = array(" ", "/", "."); $thelink = 'http://caap.co.nz/?pa_mymelp=' . strtolower(str_replace($patterns, '-', $currentPath)); $str .= '- ' . '' . '' . /*displayHtmlCategoryTree($children, null, $pathSeparator, $currentPath) . */'
';} else { $cat = 0; $catState = 0; if (isset($_POST['catID'])){ $cat = $_POST['catID']; } if (isset($_POST['catState'])){ $catState = $_POST['catState']; } $str .= '- ' . '' . ''; if ($cat == $currentPath && $catState == 1 ){$str.=displayHtmlCategoryTree($children, null, $pathSeparator, $currentPath);} } } $str .= '
'; return $str; } echo displayHtmlCategoryTree($categoryTree, "CategoryTree", '/'); /* function add_query_vars_filter( $vars ){ foreach (array_keys(Contracts::$query_params) as $name) $vars[] = $name; return $vars; } add_filter( 'query_vars', 'add_query_vars_filter' ); */ ?>
Here is the pertinent code from the PHP file
$cat = 0; $catState = 0; if (isset($_POST['catID'])){ $cat = $_POST['catID']; } if (isset($_POST['catState'])){ $catState = $_POST['catState']; } $str .= '' . '' . ''; if ($cat == $currentPath && $catState == 1 ){$str.=displayHtmlCategoryTree($children, null, $pathSeparator, $currentPath);}