0

I have created a function to get sms message from db using ajax get call and after success i want to replace the {order_id} text to order-id like 454574 and may b few more variables like customer name etc.

MY TRY

$(document).delegate('.sms-template', 'click', function() {
  var tempID = $(this).attr('id').replace('sms-template-','');
  var oID = $(this).attr('data-id').replace('sms-template-','');

  $.ajax({
    url: 'index.php?route=sale/order/getSmsTemplate&token=<?php echo $token; ?>&template_id='+tempID, 
    type: 'get',
    dataType: 'json',          
    success: function(json) {

        $('textarea#input-order-comment-'+oID).append(json['message']);
    },
    error: function(xhr, ajaxOptions, thrownError) {
        alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
    }
  });
});

It works fine and display message in textarea as

Dear {customer_name}, Your Order : {order_id} is successfully placed and ready to process. 

i want to replace {customer_name} & {order_id} with its number which i have stored in php variable.

PS: I don't have much knowledge of RegEx.

1 Answers1

1

The simplest here would most likely be a replace()

string.replace("old value","new value")

E.g.

var the_id = "454574";   // or where you get that from
var new_message = json['message'].replace("{order_id}",the_id);

$('textarea#input-order-comment-'+oID).append(new_message);

Updated based on a comment and question edit

To replace additional info, one could do something like this

var the_id = "Order id", the_name = "Customer name";
var new_message = json['message'].replace("{order_id}",the_id).replace("{customer_name}", the_name);

$('textarea#input-order-comment-'+oID).append(new_message);

If there will many more replacements, here is a post that have some clever solutions:

Asons
  • 84,923
  • 12
  • 110
  • 165