简体   繁体   中英

Assign user a tag for Mailchimp based on their selection on a drop down form

I want to assign a user a tag based on their selection in a dropdown. I have two choices in the dropdown: business to business, business to consumer. If the user chooses business to business, I'd like to assign them a tag of B2B, if they choose business to consumer, I'd like to assign them the tag of B2C.

These tags would be pushed or assigned to the tags array specified in the data variable below:

   app.post('/' , (req, res) =>{
  var firstName = req.body.firstName;
  var lastName = req.body.lastName;
  var email = req.body.email;

  var data = {
    status: "active",
    members:[
      {
        tags: [],
      email_address: email,
      status: "subscribed",
      merge_fields: {
        FNAME: firstName,
        LNAME: lastName,
      },
      }
    ],
    
  }

I have assigned the dropdown selection as buttons and dynamically rendered it from EJS

<div class="dropdown me-1">
        <button class="btn btn-secondary dropdown-toggle" type="button" 

id="dropDownText"
                data-bs-toggle="dropdown" aria-expanded="false">
                Choose your interest
            </button>
            <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1">
                <li><button class="dropdown-item" name="B2B" id="B2B"><%=b2b%></button></li>
                <li><button class="dropdown-item" name="B2C" id="B2C"><%=b2c%></button></li>
            </ul>
        </div>

This is the server.js

 let b2b = "Business to Business";
let b2c = "Business to Consumer"


    app.get('/', (req, res) => {

  res.render('index', {
    b2b: b2b,
    b2c: b2c
  });

The dropdown currently isn't sitting in a form so i guess I would have to create one to handle it in a POST request, but, I am stuck on how to create the logic to check which dropwdown the user selected to pass it into the tags array.

So I did this by adding the dropdown into the form, and then pairing the dropDown selection to the post request and pushing it into the tags array:

In EJS, note the select tag needs to have a name, this is what we use for the req.body:

<form action="/" method="post">
        <div class="dropdown me-1">
            <select name="dropDown" id="dropDown">
                <option value="Select option">Choose your interest</option>
                <option value="B2B" name="B2B">Business to Business</option>
                <option value="B2C">Business to Consumer</option>
            </select>

I create it as a variable in the POST

app.post('/' , (req, res) =>{
  var firstName = req.body.firstName;
  var lastName = req.body.lastName;
  var email = req.body.email;
  let tag = req.body.dropDown;

Then i assign the values in the data being sent back to Mailchimp API

var data = {
    status: "active",
    members:[
      {
        tags: [tag],
      email_address: email,
      status: "subscribed",
      merge_fields: {
        FNAME: firstName,
        LNAME: lastName,
      },
      }
    ],
    
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM