$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>jquery - AJAX POST Value not being returned - Wordpress - AutoTrader API|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

jquery - AJAX POST Value not being returned - Wordpress - AutoTrader API

matteradmin9PV0评论

I've got an API integration which I am currently sorting and I need to return a value when a select field is changed.

The data is being pulled from the Autotrader API - I have a vehicle 'Make' select field on a form which is populated via a curl request and lists out the makes of the vehicles available this is working fine.

I need to get the 'Model' field to update with available makes based on the option when the 'Make' field is selected - i'm trying to achieve this with AJAX but the request to the PHP file is returning no value to the select option field.

I know that the make value is being returned as have checked in the console.log - can anyone see what I am doing wrong here as there is no value being returned?

** FYI - I've excluded the CURLOPT_HTTPHEADER variables from the question.

jQuery( document ).ready( function () {
  jQuery( ".Make" ).change( function () {
    var id = jQuery( this ).val();
    var makeid = id;
    // console.log(id);

    jQuery.ajax( {
      type: "POST",
      url: "../url-to-file",
      data: {'id':makeid},
      cache: false,
      success: function ( html ) {
        jQuery( ".Model" ).html( html );
        alert(html);
      }
    } );
    // console.log(makeid);
  } );

} );

I then have this in my php file

if( $_POST['id'])
{
$make = $_POST['id'];
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "=".$make."&facet=model_family",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
    CURLOPT_HTTPHEADER => array(
    "Cache-Control: no-cache",
    "key: '.$autotrader_key .'",
    "value: '.$autotrader_secret.'",
    "Authorization: Bearer ".$autotrader_bearer
    ),

));

$results = curl_exec($curl);
if($results === false) {
    echo 'Curl error: ' . curl_error($curl);
    } else {    
        curl_close($curl);
        $results = json_decode($results);
        $used_car_models = $results->facets->model_family;
    }
    echo 'Model';
    // list of possible Makes comes from database
    foreach ( $used_car_models as $thismodel ) {

        echo 'displayName;
        echo '"';
        if ( isset( $Model ) && $thismodel->displayName == $Model )
            echo ' selected';
        echo '>';
        echo  $thismodel->displayName .' (' .$thismodel->count. ')';
            echo '      ';
        echo "\n";
    }
}

As I said, this is returning no values at all to the options on the form.

Any help would be great!

Cheers

Post a comment

comment list (0)

  1. No comments so far