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 );
    } );
    // 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 => "https://api.autotrader.co.uk/service/search1/advert?partner_site=Harratts&make=".$make."&facet=model_family",
    "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 {    
        $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!


  • 1
    Your PHP is using variables like $autotrader_key and $autotrader_secret, but they're not defined anywhere. Did you just exclude them from the question? – Jacob Peattie Feb 27 '19 at 14:12
  • @JacobPeattie - sorry, yes - they're just excluded from the question, should have made that point. Will add to the question. Thanks. – itsandysimpson Feb 27 '19 at 14:13
  • Some general notes: 1st you should be using the REST API or at least Admin AJAX, making requests to a dedicated file like that has security implications, as well as compatibility concerns. 2nd, you didn't make use of the WP HTTP API, using curl directly means you have to set it up from scratch which can have security and performance consequences, additionally you don't gain any of the benefits from the WP HTTP APIs, and it's more effort to do. e.g. using wp_remote_post would have been simpler, 3rd, your response contains raw HTML, not JSON data, and there's no escaping or caching – Tom J Nowell Feb 27 '19 at 14:30
  • But given that there is no WordPress API usage in your PHP file, no WP APIs used in the Javascript, what makes this a WP question and not a general PHP question? Generic PHP Q's are better asked on StackOverflow – Tom J Nowell Feb 27 '19 at 14:31
  • Thanks @TomJNowell - I now realise i'm doing this completely wrong and should be using the WP Admin AJAX and REST API - thanks for your help. – itsandysimpson Feb 27 '19 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.