在 PHP 中使用 AJAX 设置 If 语句



我即将完成这段代码/插件,在我们让它变得漂亮和像样之前,我只有一点要完成;我正在尝试让我的 PHP 文件使用从 AJAX 文件发送的 POST 变量重新加载/重新刷新,所有这些都来自 AJAX 发送的响应:

$("#CategoryTree").load("poster.php #CategoryTree");

现在,在单击列表项时,CSS 会触发,ajax 会触发并响应,变量被发送(在操作注册表中检查它们,在那里也很好(,但我调用的区域用海报重新加载.php #CategoryTree没有反应,抛出错误 500。

这是我的完整Javascript代码:

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);
});
}
});
});

这是PHP代码(第55行是检查开始的地方(:

<?php
//$message = "Started";
//echo "<script type='text/javascript'>alert('$message');</script>";
$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 . "n";
displayCategoryTree($children, $indent . '|- ');
}
}
$categoryTree = buildCategoryTree($categoryLines, '/');
function displayHtmlCategoryTree($categoryTree, $id = null, $pathSeparator = '/', $parents = '') {
if (empty($categoryTree)) return '';
$str = '<ul' . (!empty($id) ? ' id="'.$id.'"' : '') . '>';
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 .= '<li title="' . $currentPath . '">' . '<input value ="0" class="first" type="checkbox" id="' . $currentPath . '">' . '<label  for="' . $currentPath . '">' . '<a href="' . $thelink .'">' . $node . '</a></label>' . 
/*displayHtmlCategoryTree($children, null, $pathSeparator, $currentPath) . 
*/'</li>';}
else        
{
$cat = 0;
$catState = 0;
if (isset($_POST['catID'])){
$cat = $_POST['catID'];
}
if (isset($_POST['catState'])){
$catState = $_POST['catState'];
}
$str .= '<li title="' . $currentPath . '">' . '<input value="0" class="first" type="checkbox" id="' . $currentPath . '">' . '<label for="' . $currentPath . '">' . $node . '</label>';
if ($cat == $currentPath && $catState == 1 ){$str.=displayHtmlCategoryTree($children, null, $pathSeparator, $currentPath);}

}

}
$str .= '</li></ul>';
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' );
*/
?>

这是PHP文件中的相关代码

$cat = 0;
$catState = 0;
if (isset($_POST['catID'])){
$cat = $_POST['catID'];
}
if (isset($_POST['catState'])){
$catState = $_POST['catState'];
}
$str .= '<li title="' . $currentPath . '">' . '<input value="0" class="first" type="checkbox" id="' . $currentPath . '">' . '<label for="' . $currentPath . '">' . $node . '</label>';
if ($cat == $currentPath && $catState == 1 ){$str.=displayHtmlCategoryTree($children, null, $pathSeparator, $currentPath);}

如果大括号后删除分号。if{};if{}

if (isset($_POST['catID'])){$cat = $_POST['catID'];};

将其替换为

if (isset($_POST['catID'])){
$cat = $_POST['catID'];
}

现在你的代码看起来像。

if (isset($_POST['catID'])){
$cat = $_POST['catID'];
}
if (isset($_POST['catState'])){
$catState = $_POST['catState'];
}
$str .= '<li title="' . $currentPath . '">' . '<input value="0" class="first" type="checkbox" id="' . $currentPath . '">' . '<label for="' . $currentPath . '">' . $node . '</label>';
if ($cat == $currentPath && $catState == 1){
$str.=displayHtmlCategoryTree($children, null, $pathSeparator, $currentPath);
}

最新更新