Ajax calls in Drupal

Submitted bybarnettech onFri, 05/06/2022 - 10:29

To send the json data in JavaScript

   var xmlhttp = new XMLHttpRequest();   // new HttpRequest instance
   xmlhttp.open("POST", "/user/17352/post-dashboard");
   xmlhttp.setRequestHeader("Content-Type", "application/json");
   xmlhttp.send(JSON.stringify({advisorcomment:advisorComment}));

To receive the json data in PHP

 $json_string = \Drupal::request()->getContent();
    $decoded = \Drupal\Component\Serialization\Json::decode($json_string);
    print_r($decoded);
    $name = $decoded['name'];

    return new JsonResponse($name);

Platform.sh commands

Submitted bybarnettech onFri, 03/25/2022 - 10:12

Activate a new branch and have it register in platform.sh:

platform environment:activate new-feature

Importing data:

platform sql < my_database_backup.sql

Exporting data:

platform db:dump --gzip

Upload files

platform mount:upload

If you get this error: Access denied; you need (at least one of) the SUPER privilege(s) for this operation

sed 's/\sDEFINER=`[^`]*`@`[^`]*`//g' -i oldfile.sql

When using platform sql to upload a new database delete the line with "DEFINER" in it first

JavaScript post request

Submitted bybarnettech onWed, 03/02/2022 - 15:03
var email = document.querySelector('#edit-investor-profiles-0-entity-field-email-0-value').value;
  if (email.length > 3) {
    // construct an HTTP request
    var xhr = new XMLHttpRequest();
    var url = "/user/register/check-email"
    xhr.open("POST", url, true);
    xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');

    var data = JSON.stringify({"email": email});
    // send the collected data as JSON
    xhr.send(JSON.stringify(data));
    xhr.onreadystatechange = function()
    {
      if (xhr.readyState == 4 && xhr.status == 200)
      {