'Advanced Contact Fields not updating repeater field in separate PHP file

I'm trying to dynamically add a new ACF repeater field value (course_slug) to the current user in Wordpress. The value to be added is a URL generated from a separate function.

If I use the following code on the original page using a static URL value for course_slug then the new value is added fine.

<?php $field_key = "field_61e9bb8b66765";

  $value = get_field($field_key, "user_{$current_user_id}");
  echo $value; 

$value[] = array("course_slug" => "http://www.exampleurl.com");
update_field( $field_key, $value, "user_{$current_user_id}");
  ?>

However if I run the code in a separate PHP file using two data values sent from the original page (as per below), then I receive a 500 error.

 url: 'http://localhost:8888/wordpress/wp-content/themes/cookable/write-course-to-user.php',data:{"userIDdata":finaluserid, "courseURLdata":tidycourseurlupdate},
 success: function(data) {
   $('#result3').html(data);

 }
});

write-course-to-user.php

  <?php  

$userIDfinal = isset($_REQUEST['userIDdata'])?$_REQUEST['userIDdata']:"";
$courseURLfinal = isset($_REQUEST['courseURLdata'])?$_REQUEST['courseURLdata']:"";
echo $userIDfinal;
echo $courseURLfinal;
echo "user_{$userIDfinal}";

  $field_key = "field_61e9bb8b66765";
  $value = get_field($field_key, "user_{$userIDfinal}");
  echo $value;
  $value[] = array("course_slug" => $courseURLfinal);
  update_field( $field_key, $value, "user_{$userIDfinal}");



?>

The three test echos in there display data as expected:

echo $userIDfinal; - displays 1

echo $courseURLfinal; - displays https://calendly.com/tjmdigital/cookable-1-week-1

echo "user_{$userIDfinal}"; displays user_1

Am I missing something daft here as to why the code will run fine in the original file but not in a linked one? Any advice hugely appreciated, thanks.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source