Save URL current automatically

Hello community, I need your help, how can I save the current url in a subscription form with database, I need the code since it does not compile me, help please

Hi,

To retrieve the current URL, you can use Wix Location API :

import wixLocation from 'wix-location';
let url = wixLocation.url;

If you wish to set the URL to the field, you can use:

$w('#userInputID).value = url; 

Good luck,
Tal.

1 Like

Hi Tal,
it does not save the current URL, how can I do a GET URL, please help me with the syntax, the idea is to save it automatically in an INPUT

Hi Alfredo,
Did you change the element id in the example to your element id ?
If it’s still doesn’t work, please paste here the relevant code.

Roi

hi Roi
code:
import wixLocation from ‘wix-location’;

let url = wixLocation.protocol;

import {sendEmail, sendEmailWithRecipient} from ‘backend/email’;

$w.onReady(function () {
$w(“#dataset1”).onAfterSave(sendFormData);
});

function sendFormData() {
const subject = A ${$w("#input2").value} LE INTERESA LO SIGUIENTE:;
const body = `Formulario de inscripción CREA TU ECO-HOME

\URL: ${$w(‘#URLcurrent’).value = url}
\Nombre: ${$w(“#input2”).value}
\Teléfono: ${$w(“#input3”).value}
\Email: ${$w(“#input4”).value}
\Terreno: ${$w(“#selection1”).value}`;

const recipient = $w(“#input2”).value;
sendEmailWithRecipient(subject, body, recipient)
.then(response => console.log(response));

sendEmail(subject, body)
.then(response => console.log(response));
}

ELEMENT ID = URLcurrent (input)

Hi,
Here is some changes.
Pay attention to the comments:

$w.onReady(function () {
    let url = wixLocation.url; // Change wixLocation.protocol to wixLocation.url. Should be in $w.onReady scope
    $w('#userInputID).value = url; // #userInputID represents the id name of your input field, change it to the correct id name
    $w("#dataset1").onAfterSave(sendFormData);
});

This should work,
Good luck!

Roi

1 Like

Thank you Roi
this need to rescue the url, the idea is that it is the complete url, but I love you, we are getting closer

Current code, working almost 100%, thanks to you Roi

import wixLocation from ‘wix-location’;

$w.onReady(function () {
let url = wixLocation.url; // Change wixLocation.protocol to wixLocation.url. Should be in $w.onReady scope
$w(‘#URLcurrent’).value = url; // #userInputID represents the id name of your input field, change it to the correct id name
$w(“#dataset1”).onAfterSave(sendFormData);
});

import {sendEmail, sendEmailWithRecipient} from ‘backend/email’;

$w.onReady(function () {
$w(“#dataset1”).onAfterSave(sendFormData);
});

function sendFormData() {
const subject = A ${$w("#input2").value} LE INTERESA LO SIGUIENTE:;
const body = `Formulario de inscripción CREA TU ECO-HOME

\URL: ${$w(‘#URLcurrent’).value}
\Nombre: ${$w(“#input2”).value}
\Teléfono: ${$w(“#input3”).value}
\Email: ${$w(“#input4”).value}
\Terreno: ${$w(“#selection1”).value}`;

const recipient = $w(“#input2”).value;
sendEmailWithRecipient(subject, body, recipient)
.then(response => console.log(response));

sendEmail(subject, body)
.then(response => console.log(response));
}

Hello Alfredo,

EDIT: The solution below will not work for you since the product page is basically an iframe.
There is no integration between wix code and the product page yet.

You are getting an incomplete url since the form is inside a lightbox.
To send the correct url to the light box, disconnect the ‘Me interesa’ button and manually set up the onClick event per the example below.

You will need to send the url to the lightbox using the wixWindow functions:

In your product page:

import wixLocation from 'wix-location';
import wixWindow from 'wix-window';
// ...
wixWindow.openLightbox("LightboxName", wixLocation.url);

Use this code for your lightbox:

import wixWindow from 'wix-window';
 // ...
let receivedData = wixWindow.lightbox.getContext();
//use receivedData to populate the url input box