【问题标题】:Change PHP variable using javascript or AJAX on the same page在同一页面上使用 javascript 或 AJAX 更改 PHP 变量
【发布时间】:2018-11-21 04:35:57
【问题描述】:

我知道这个问题已经被问过很多次了,但我认为就我而言,我正在处理不同的事情,或者更好地说,我需要不同的事情。

我正在使用作为预约预约的开源软件,但不幸的是,客户可以选择服务而不是服务的持续时间。我可以通过以不同的分钟长度手动添加更多次来重新创建相同的服务,但是这样,在下拉菜单中会出现很多选项,这不是我想要的解决方法。

所以,我想到的是使用下拉菜单来选择时间,并基于该选择,在服务下拉菜单上,将仅显示基于时间的相应选项。 该网站如下所示: site

我正在寻找的是,每当我选择 nr 小时...我只获得属于该小时的服务,而不是所有服务。 我可以使用刷新页面的按钮,但我无法创建另一个文件然后重定向到这里。

这是代码中感兴趣的部分:

<select id="select-service" class="col-xs-12 col-sm-4 form-control">

<?php
    // Group services by category, only if there is at least one service with a parent category.
    $has_category = FALSE;
    foreach($available_services as $service) {
        if ($service['category_id'] != NULL) {
            $has_category = TRUE;
            break;
        }
    }

    if ($has_category) {
        $grouped_services = array();

        foreach($available_services as $service) {
            if ($service['category_name'] == '2 HOURS' || $service['category_name'] == '1 HOUR' || $service['category_name'] == '3 HOURS') {
                if (!isset($grouped_services[$service['category_name']])) {
                    $grouped_services[$service['category_name']] = array();
                }

                $grouped_services[$service['category_name']][] = $service;
            }

        }

        // We need the uncategorized services at the end of the list so
        // we will use another iteration only for the uncategorized services.
        $grouped_services['uncategorized'] = array();
        foreach($available_services as $service) {
            if ($service['category_id'] == NULL) {
                $grouped_services['uncategorized'][] = $service;
            }
        }

        foreach($grouped_services as $key => $group) {
            $group_label = ($key != 'uncategorized')
                    ? $group[0]['category_name'] : 'Uncategorized';

            if (count($group) > 0) {
                echo '<optgroup label="' . $group_label . '">';
                foreach($group as $service) {
                    echo '<option value="' . $service['id'] . '">'
                        . $service['name'] . '</option>';
                }
                echo '</optgroup>';
            }
        }
    }  else {
        foreach($available_services as $service) {
            echo '<option value="' . $service['id'] . '">' . $service['name'] . '</option>';
        }
    }
?>
                            </select>

【问题讨论】:

    标签: javascript php html ajax xhtml


    【解决方案1】:

    我的平台只使用一个 AJAX 函数。下面是一个最小的例子:

    function ajax(method,url,param_id_container_pos,id_container)
    {
     var xhr = new XMLHttpRequest();
     xhr.open(method,url,true);
    
     xhr.onreadystatechange = function()
     {
      if (xhr.readyState=='4')
      {
       var type = xhr.getResponseHeader('content-type').split('/')[1];
       if (method=='post')
       {
        if (type=='json')
        {
         var j = JSON.parse(xhr.responseText);
         console.log(j);//Check your browser's developer network panel.
         eval(j.javascript);//eval is frowned upon though just use to call a sequel JS function.
        }
       }
      }
     }
    }
    
    //Example uses:
    ajax('get','admin/?ajax=account','inside','id_target');
    
    var fd = new FormData();
    fd.append('ajax','admin_post_account_approval');
    fd.append('parameter1',object1);
    fd.append('parameter2',object2);
    ajax('post',path+'admin/',fd);
    

    您的目标是尽可能减少代码并高度重用。

    关于服务器和 PHP,您需要记住:永远不要信任用户。这意味着您需要验证所有内容:

    <?php
    if (isset($_POST['ajax']) && $_POST['ajax']=='admin_post_account_approval')
    {
     if (!isset($_POST['parameter1'])) {/*error_handling*/}
     else if (!isset($_POST['parameter1'])) {/*error_handling*/}
     else if (![condition not met]) {}
     else
     {
      if ([all conditions met])
      {
       header('Content-Type: application/json');
       $array = array('javascript'=>'alert(\'Just a JavaScript alert triggered by PHP.\');');
       die(json_encode($array));
      }
     }
    }
    ?>
    

    服务器端代码,PHP 应该被认为是现实生活:总是失败首先并测试长度、正确字符或表单参数中的错误字符等条件。

    此外,我强烈建议让服务器使用 JSON 响应,正如我在上面的代码中一般说明的那样。因为我只编写自己的代码而不使用其他人的代码,所以这更像是一种通用的响应,而不是试图针对您正在使用的任何软件。无论您是否启用错误报告并注意您使用的任何浏览器中的开发人员工具,您都会到达那里。祝你好运。

    【讨论】:

      猜你喜欢
      • 2017-10-13
      • 1970-01-01
      • 2012-08-20
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-12
      相关资源
      最近更新 更多