如何查看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.

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.

You need to add the dataType parameter, and the data parameter in the ajax call is a javascript object of key and value. So the key should be in single/double quotes mentioned in the code below:

$.ajax({
    type: 'GET',
    url: 'saveVV.php',
    dataType: 'json',
    data: { 'option':option } // In php you can access like $_GET['option']
}).done(function(resp){
     if(resp == 200) {
         console.log('Success!');
     }else if(resp == 0){
         console.log('Failed..');
     }
});