change the email subject according to database entry

Hi,
I am trying to set a button so that clients can click and send and email do a defined email addres with a subject that changes according to a database entry. However, my knowledge on JavaScript is null :frowning:
Can someone help me with the code?
Thank you in advance,
AV

1 Like

No one?

Hi Aldino.

Do you have your site set up for sending an email and the only thing missing is setting a subject?

Regards,
Genry.

Hi Genry,
Yes, I have a button that, when you click, sends an email.
However, in order for me to know what the email refers to (and avoid relying on the costumer’s discernment to write it on the subject) I would like that the subject would correspond to a given entry in the database.

Thank you in advance,
AV

So, I could figure out the code to send the email with a defined subject:

$w.onReady(function destinos_onReady() {
$w(‘#orcamento’).link = “mailto:XXXXX@XXXXX.pt?subject=Pedido de Orçamento - teste”;

} ); 

(Don’t know if it is exactly correct but it works…)

However, this I could have done without the code…
What I need is: where it reads “teste” to have a dataset entry (in text format)…

Thank you again,
AV

Hi Aldino.

If you know exactly the condition what to query from which collection, you can use wix-data to query priory to setting the link for the value and then set the subject accordingly.
If the value is somehow dependent on the logged in user - you can use wix-users to understand who is the logged in user and query the collection according to that data.

https://www.wix.com/code/reference/wix-users.html

Regards,
Genry.

Hi Genry,
thank you for your reply.

I am sorry but I do not understand how I can do this using wix-data query…
As I said, I do not know JavaScript and it is dificult for me to understand the structure of the scrips.

in my mind, it woud be something like below ( I know that it is completly wrong but I hope it is enough to make my point)

$w.onReady( function destinos_onReady() {
let name = $w(“#destinos”).title();
$w(‘#orcamento’).link = “mailto:a@b.pt?subject=Pedido de Orçamento - ${$w(”#name").title}";

} );

so, just to be clear, my database is called: destinos; the data entry is called: title and the button is called orcamento.

Thank you in advance to anyone who can help and my apologies in advance if my question is somewhat naive, but as I said, I know nothing of JavaScript and I have no way of learning it right now…

Best,
AV

Can you please share your site’s URL and describe what value you want to put there from the page?

Thanks,
Genry.

As an example you can have a look at the page:
https://www.viagensalacarte.pt/africa-do-sul

This one is not from a dynamic page, so, no problems here.
However, since this is a travelling agency’s site, I will have different destinations and I want to create the destinations pages using dynamic pages (which I am doing now but they are still hiden…).

In this sence, where you have the button: Solicitar Orçamento, I want it to send an email with the title of the destination, according to my database:


I hope this is clear.
Thank you again for your help and time.
Best,
AV

Hi AV.

Assuming element #text75 is the value of you want to embed in the subject, you should use the following:

const name = $w('#text75').text;
$w('#orcamento').link = `mailto:xxx@xxx.xxx?subject=Pedido de Orçamento - ${name}`;

As a side note, I see you have several $w.onReady statements.
This will be problematic. There should be only one such call on a page.

Hope this helps.

Regards,
Genry.

Hi,
Thank you again for helping me and for the tip on the $w.onReady statements. I will take care of those.
For the time being I still could not figure this one out and I am feeling terrebly frustrated as I feel this should not be such a complication…

I have (only for the email stuff):

$w.onReady(function destinos_onReady() {
const name = $w(‘#title’).text;
$w(‘#orcamento’).link = mailto:a@b.pt?subject=Pedido de Orçamento - ${$w("#name").text};
} );

My database is called: destinos
The cullum ID (from where I want the text extracted) is called: title
The button is called: orcamento

I’m getting the following errors:

One more thing, is: " different from: or: `
I see all of them being used in different posts and I am not sure if it is something I should also consider…

Again, my apologies for all the trouble but I really cannot figure it out :frowning:
Best,
AV

Hi AV.

For reading the title - you should use the element name where the title field is bounded to. From my understanding according to the data binding configuration the element is #text75 , right?

As for your second question - this is a javascript question.
Using " or ’ - has equal meaning. It is up to the preference of the developer :slight_smile: You can enclose a plain text string in those.
Using ` - this is string interpolation according to ES2015 standard. You can enclose string and evaluate expressions enclosed in ${}. More info you can find here Template literals (Template strings) - JavaScript | MDN

Regards,
Genry.

Hello Genry,
thank you for the explanation on the javascript question :slight_smile:

so, the title I wanted was from a collum at the database; however, it is the same thing that appears in element #text75 , so I guess it should still work fine.

so, now I have:

$w.onReady(function destinos_onReady() {
const name = $w(‘#text75’).text; //this sets name to the same as #text75 (my guess)
$w(‘#orcamento’).link = mailto:a@b.pt?subject=Pedido de Orçamento - ${$w('#name').text}; // this should send and email to a@b.pt with the subject : Pedido de Orçamento - #text75

} ); 

What am I missing here? I still get that:
#name is unread
#name is not a valid sector name

Also, if I only type:

$w(‘#orcamento’).link = mailto:a@b.pt?subject=Pedido de Orçamento - ${$w('#text75').text};

shouldn’t I get an email with the subject : Pedido de Orçamento - #text75
(in which #text75 changes according to the page)?
I tried this but what I get is the default text I have written in the the text box (e.g. Pedido de Orçamento - insert text here) [assuming insert text here is what I wrote in the box]…

Thank you,
AV

Please use the dataset’s onReady callback in order all the data bounded to elements to be ready.

So your code should be the following:

$w.onReady(function destinos_onReady() {
   $w('#destinos').onReady( () => {
      const name = $w('#text75').text;
      $w('#orcamento').link = `mailto:a@b.pt?subject=Pedido de Orçamento - ${$w('#name').text}`;
   });
});

Regards,
Genry.

IT WORKS!!

So, I have it working like this:

$w.onReady(function destinos_onReady() {
$w(‘#destinos’).onReady( () => {
$w(‘#orcamento’).link = mailto:a@b.pt?subject=Pedido de Orçamento - ${$w('#text75').text};
});
});

Thank you so much Genry :smiley:
All the best,
Aldino

Happy to be of help :slight_smile:

Regards,
Genry.

Hi! I have been trying to do the same thing by referencing the text in a repeater connected to the dataset. The problem I have is that the returned string in the email subject is the placeholder text (Lorem ipsum), and not the title fetched from the database. Any ideas on why that is? Thank you!

Hello, is it possible to set this in the subject line of the email we get from the form submission? we need to get the job id when a person is applying for a position.

did you managed to set the data field in the email subject line?