Need to be member before submitting a booking form

Hi,

So I have a booking form page with a ‘everyone permission’, after they enter all the information when they click submit button, the user need to log in or register to be a member before the booking form is submit. And after the form is submitted, I can know which member submit it.

Can that be done?
Thanks!

If you want to have a database (for form submissions) then set the database permissions to ‘Site Member Only’. This will only allow Site Members to make a submission.

Now there are many creative ways you can prompt user’s to become a site member before form submission. One method is to check whether the user is logged in or not at the time of submission. An example code would be like below.

export function button_click(event) {
    if(wixUsers.currentUser.loggedIn === true) {
       //allow submission
    } else {
       authorize();
    }
}

function authorize() {
    wixUsers.promptLogin()
    .then( (user) => {
        return user.getEmail();
    })
    .then( (email) => {
        let userEmail = email; //submit email along with the data
    })
    .catch( (err) => {
        let errorMsg = err; //show an error
    });
}

Submit the data using Wix Data’s insert() function

1 more thing, I also tried to send the submitted form using email using sendGrid. Not sure where to place the code. Here’s my code below.

Also how can I retrieve member email address? So I can add to the ’ const recipient’. Hope you can help!

  1. import wixData from ‘wix-data’;

  2. import wixUsers from ‘wix-users’;

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

  4. $w.onReady( function () {

  5. $w(" #SHsmarthub ").onAfterSave(sendFormData); //‘#SHsmarthub’ is not a valid selector//

  6. });

  7. function sendFormData() {

  8. const subject = Smart Hub Integration;

  9. const body = `Smart Hub Integration }

  10. \rPlease select which brand of hub will be installed: ${$w(" #brand ").value}

  11. \rWhat is the specific model of the device?: ${$w(" #model ").value}

  12. \rAnything else you want us to know?: ${$w(" #others ").value}

  13. `;

  14. const recipient = ‘email@gmail.com’;

  15. sendEmailWithRecipient(subject, body, recipient)

  16. .then(response => console.log(response));

  17. sendEmail(subject, body)

  18. .then(response => console.log(response));

  19. }

  20. let brand

  21. let model

  22. let devicesamt

  23. let mobiletype

  24. let others

  25. export function button42_click(event) {

  26. if (wixUsers.currentUser.loggedIn === true ) {

  27. let toInsert = {

  28. “brand”: $w(" #brand ").value,

  29. “model”: $w(" #model ").value,

  30. “devicesamt”: $w(" #devicesamt ").value,

  31. “mobiletype”: $w(" #mobiletype ").value,

  32. “others”: $w(" #others ").value

  33. };

  34. wixData.insert(“SHsmarthub”, toInsert)

  35. .then( (results) => {

  36. let item = results; //see item below

  37. } )

  38. . catch ( (err) => {

  39. let errorMsg = err;

  40. } );

  41. } else {

  42. authorize();

  43. }

  44. }

  45. function authorize() {

  46. wixUsers.promptLogin()

  47. .then( (user) => {

  48. return user.getEmail();

  49. })

  50. .then( (email) => {

  51. let userEmail = email; //submit email along with the data

  52. })

  53. . catch ( (err) => {

  54. let errorMsg = err; //show an error

  55. });

  56. }