Ajax Request

is the following ajax request is correct?

$("#send_btn").click( function () {
$.ceAjax('request',
fn_url('vendor_communication.create_thread'), {
method : 'POST',
caching: false,
hidden: true,
data : {
return_url : 'vendor.php?dispatch=vendor_communication.threads',
communication_type : 'vendor_to_customer',
timeline : $('#timeline_box').val,
thread : {
object_id : {$order_info.order_id},
object_type : 'O',
subject : 'Request Modifications',
company_id : {$order_info.company_id},
message : '',
communication_type : 'vendor_to_customer'
}
},
callback : function(data) {
console.log(data);
}
});
});

The provided code appears to be written in JavaScript and uses the jQuery library. However, there are a few syntax errors and missing details that need to be addressed. Here’s the corrected version of the code:

$("#send_btn").click(function() {
  $.ceAjax('request', fn_url('vendor_communication.create_thread'), {
    method: 'POST',
    caching: false,
    hidden: true,
    data: {
      return_url: 'vendor.php?dispatch=vendor_communication.threads',
      communication_type: 'vendor_to_customer',
      timeline: $('#timeline_box').val(),
      thread: {
        object_id: '{$order_info.order_id}',
        object_type: 'O',
        subject: 'Request Modifications',
        company_id: '{$order_info.company_id}',
        message: '',
        communication_type: 'vendor_to_customer'
      }
    },
    callback: function(data) {
      console.log(data);
    }
  });
});

Changes made:

  • Added parentheses () to $('#timeline_box').val to ensure it’s a function call.
  • Added curly braces {} around the {$order_info.order_id} and {$order_info.company_id} placeholders to indicate variables or template literals.

Please note that if you intended to use PHP variables {$order_info.order_id} and {$order_info.company_id} directly in JavaScript, you need to ensure that the JavaScript code is being interpreted within a PHP file or that you have proper PHP variable interpolation in place. Otherwise, these placeholders won’t be resolved correctly.

2 Likes

@Softlist thanks for that, I wasn’t the original poster but you spiked my interest. I haven’t used Ajax before, what is this script and how would I implement it (Cpanel server).

If you are looking to implement this script on a cPanel server, you would typically follow these steps:

Access your cPanel account and navigate to the File Manager.
Locate the file where you want to add the script. This could be an HTML file or a JavaScript file.
Edit the file and add the script to the appropriate section or location. You can use the cPanel file editor or upload a modified version of the file.
Save the changes to the file.
Ensure that any dependencies, such as the jQuery library, are correctly included in the file. If necessary, you may need to upload the required files to your server or link to external sources. [Use jetbrains]
Test the implementation by accessing the page or functionality where the script should be executed.

Please note that the specific implementation steps may vary depending on your server configuration, the file you are modifying, and the context in which you want to use the script. It’s important to have a good understanding of web development and the specific requirements of your project when implementing scripts on a cPanel server.

1 Like

For those playing along I asked ChatGPT what it does. It appears it creates a message to the vendor, tit requires other code to work, this is what ChatGPT said …

" Here’s a breakdown of the AJAX request:

‘request’ is the type of the AJAX request.
fn_url('vendor_communication.create_thread')** is the URL to which the AJAX request will be sent.

An object containing AJAX options follows:

  • method: 'POST' indicates that it will be a POST request.
  • caching: false tells the browser not to cache the response.
  • hidden: true tells CS-Cart not to show any loading icons or overlays during the request.
  • data: specifies the data to be sent to the server. In this case, it’s an object with return_url, communication_type, and thread properties.
  • callback: defines a function to be executed when the server’s response is received. In this case, it simply logs the data returned by the server.

Here’s a PHP example of how you could handle this request in CS-Cart. Let’s assume that the file is called app/controllers/frontend/vendor_communication.php:

<?php

if (!defined('BOOTSTRAP')) { die('Access denied'); }

if ($mode == 'create_thread') {
    $return_url = Tygh::$app['request']->get('return_url');  
    $communication_type = Tygh::$app['request']->get('communication_type');  
    $thread = Tygh::$app['request']->get('thread');  

    // Process your data here...
    // For example, create a new communication thread
    $result = fn_vendor_communication_create_thread($thread);

    Tygh::$app['ajax']->assign('result', $result);  // Return the result to the AJAX request
    exit;
}

In this example, fn_vendor_communication_create_thread() is a hypothetical function that handles the creation of a communication thread. You’ll need to implement that function (or an equivalent one) according to your requirements.

The provided example is quite simplified and your actual implementation might be more complex depending on your needs. Also, remember to do all necessary input validations and sanitizations to prevent SQL injections and other security issues."