douguai6716 2019-02-19 11:16
浏览 150

如何查看Ajax传递给PHP的数据以及PHP对数据 - 下拉菜单选项的作用

I want to update my database without refreshing the page when selecting an option from my drop down menu. So I will use Ajax. The problem is that without refreshing the page and without seeing the PHP code running, I cannot see if there are any errors in my code in order to fix them. Is there any way to see the data that I pass when selecting an option from my drop down to my PHP file, and see any errors the PHP file reports when processing that data?

I have tried to check the console of my browser but it doesn't display anything.

My drop down:

echo "<select id='vacationId' name='vacationId' style='background:lightblue'>
    <option selected='selected' value='' style='background:lightblue'></option>
    <option value='" . $vacationIdP . "' style='background:lightblue'>P</option>
    <option value='" . $vacationIdO . "' style='background:lightblue'>O</option>
    <option value='" . $vacationIdA . "' style='background:lightblue'>A</option>
    <option value='" . $vacationIdR . "' style='background:lightblue'>R</option>
</select>";

Ajax code to pass the option Value to a PHP file:

$('#vacationId').change(function(){
    var option = $('#vacationId').val();
    console.log(option);

    $.ajax({
        type: 'GET',
        url: 'saveVV.php',
        data:
             {
             option:option   // Does this pass the value of the option ?
// Can I access this in my PHP file with $vacationId = $_GET['option'];
             }
    }).done(function(resp){
         if(resp == 200) {
             console.log('Success!');
         }else if(resp == 0){
             console.log('Failed..');
         }
    });
});

What I want is to pass the Value of the selected option to my PHP file and then do some processing to it in PHP. And I want to see that I pass the correct info to my PHP file. And I would like to see the PHP code running with that info and maybe displaying some errors.

  • 写回答

2条回答 默认 最新

  • dongwubao4785 2019-02-19 11:22
    关注

    You can use this wrapper I wrote

    https://github.com/ArtisticPhoenix/MISC/blob/master/AjaxWrapper/AjaxWrapper.php

    class AjaxWrapper{
    
        /**
         * Development mode
         *
         * This is the least secure mode, but the one that
         * diplays the most information.
         *
         * @var string
         */
        const ENV_DEVELOPMENT = 'development';
    
        /**
         *
         * @var string
         */
        const ENV_PRODUCTION = 'production';
    
        /**
         * 
         * @var string
         */
        protected static $environment;
    
        /**
         * 
         * @param string $env
         */
        public static function setEnviroment($env){
            if(!defined(__CLASS__.'::ENV_'.strtoupper($env))){
                throw new Exception('Unknown enviroment please use one of the '.__CLASS__.'::ENV_* constants instead.');
            }
            static::$environment = $env;
        }
    
        /**
         * 
         * @param closure $callback - a callback with your code in it
         * @param number $options - json_encode arg 2
         * @param number $depth - json_encode arg 3
         * @throws Exception
         * 
         * @example
         * 
         * 
         */
        public static function respond(Closure $callback, $options=0, $depth=32){
            $response = ['userdata' => [
                  'debug' => false,
                  'error' => false
            ]];
            ob_start();
             try{
                 if(!is_callable($callback)){
                    //I have better exception in mine, this is just more portable
                    throw new Exception('Callback is not callable');
                 }
                 $callback($response);
             }catch(\Exception $e){
                  //example 'Exception[code:401]'
                 $response['error'] = get_class($e).'[code:'.$e->getCode().']';
                if(static::$environment == ENV_DEVELOPMENT){
                //prevents leaking data in production
                    $response['error'] .= ' '.$e->getMessage();
                    $response['error'] .= PHP_EOL.$e->getTraceAsString();
                }
             }
             $debug = '';
             for($i=0; $i < ob_get_level(); $i++){
                 //clear any nested output buffers
                 $debug .= ob_get_clean();
             }
             if(static::environment == static::ENV_DEVELOPMENT){
                 //prevents leaking data in production
                  $response['debug'] = $debug;
             }
             header('Content-Type: application/json');
             echo static::jsonEncode($response, $options, $depth);
       }
       /**
        * common Json wrapper to catch json encode errors
        * 
        * @param array $response
        * @param number $options
        * @param number $depth
        * @return string
        */
       public static function jsonEncode(array $response, $options=0, $depth=32){
           $json = json_encode($response, $options, $depth);
           if(JSON_ERROR_NONE !== json_last_error()){
               //debug is not passed in this case, because you cannot be sure that, that was not what caused the error.
               //Such as non-valid UTF-8 in the debug string, depth limit, etc...
               $json = json_encode(['userdata' => [
                  'debug' => false,
                  'error' => json_last_error_msg()
               ]],$options);
           }
           return $json;
       }
    }
    

    For example

    AjaxWrapper::setEnviroment(AjaxWrapper::ENV_DEVELOPMENT);
    
    AjaxWrapper::respond(function(&$result){
          echo "foo";
         //..other code
         $response['success'] = true;
    });
    

    Then when you do console.log it will have whatever was output within the callback as data.debug etc..

    Hope it helps.

    It uses a combination of ob_* output buffering and exception trapping try/catch to catch any output and wrap it up in an item data.debug or data.error if the environment is set correctly.

    Then when you do ajax stuff ... function(data) { console.log(data); } it will be included.

       data.debug = "foo" 
    

    And so on.

    评论

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了