【问题标题】:Setting an If Statement with AJAX in PHP在 PHP 中使用 AJAX 设置 If 语句
【发布时间】:2020-04-10 15:30:41
【问题描述】:

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

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

现在单击 CSS 触发的列表项,ajax 触发并响应,发送变量(在操作注册表中检查它们也都很好)但是我调用的区域重新加载 poster.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);}

【问题讨论】:

  • 请务必使用htmlspecialchars() 来处理您注入到 HTML 上下文中的任意数据。没有它,您可能会生成无效的 HTML,并面临严重的安全问题。

标签: php ajax wordpress woocommerce


【解决方案1】:

删除 if 花括号后的分号。 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);
}

【讨论】:

  • 感谢您指出这一点,文件运行方式没有变化,但语法肯定更好!
猜你喜欢
  • 1970-01-01
  • 2019-08-03
  • 1970-01-01
  • 1970-01-01
  • 2018-01-08
  • 2017-03-10
  • 2014-05-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多