如何将JS变量拉入EJS主题页面?

发布于 2025-02-05 18:19:21 字数 3246 浏览 2 评论 0原文

这是index.js上的变量:/ *用户可以共享 */let referral_code = shortid.generate.generate();

我只想将其拉到index.ejs上页面类似$({reverrer_code})或从index.js页面获取变量的任何内容。我只是在将转介_Code变量传递到Express Web页面模板的简单任务时遇到了麻烦。

有点新的反应和JS,可以使用一些指导。

请帮助我将我的基本JS变量拉到EJS Express主题页面。

我要做的就是将称为Reverral_code的变量放在页面上,作为可见的HTML文本。

我试图在主题上直接以HTML形式显示的变量可以在index.js页面上看到: “让referral_code = shortid.generate();”

这是完整的索引页面:

var router = express.Router();
var mysql = require('mysql');
var shortid = require('shortid');

/* New POST route for form submissions */
router.post('/', function(req, res, next) {

  /* establish mysql connection */
  var connection = mysql.createConnection({
    host: process.env.DB_HOST,
    user: process.env.DB_USER,
    password: process.env.DB_PASS,
    database: process.env.DB_NAME
  });

  /* user's email address */
  let email = req.body.email;

  /* a unique referral code the user can share */
  let referral_code = shortid.generate();

  /* the referral code a user submitted (might be null) */ 
  let referrer = req.body.referrer;

  /* add user to the database with INSERT */
  let query = "INSERT INTO `users` (`email`, `referral_code`, `referrer`) VALUES (?, ?, ?)";
  connection.query(query, [email, referral_code, referrer], (err, rows) => {
    connection.end(function() {
      if (err) return next();
      return res.send({referralCode: referral_code});
    });
  });
});

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index');
});

module.exports = router;

这是我要更新的主题页面。您可以在我添加的代码中看到:{referral_code},这就是我要显示referral_code的地方。

<!DOCTYPE html>
<html>
  <head>
    <title>The Starseeds Referral App</title>
    <link rel='stylesheet' href='/stylesheets/style.css' />
    <script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
  </head>
  <body>
    <h1>SignUp to Earn Your Free Tokens!</h1>
    <input type="email" id="email" placeholder="Enter your email...">
    <input type="text" id=" as" placeholder="Referral code">
    <button id="submit">Sign up</button>
    <script>
      $('#submit').on('click', function(e) {
        e.preventDefault();
        $.ajax({
          url: '/',
          type: "POST",
          data: {
            'email': $('#email').val(),
            'referrer': $('#387jh6g').val()       
          },
          "newAffiliate" = res.referralCode,
          success: function(res) {
            alert('Your referral code is: ' + res.referralCode);
          },
          error: function(jqXHR, textStatus, errorMessage) {
            alert(errorMessage);
          }
        });
      })
    </script>
    <H2>
      <label>Your referral code: </label>
      <span id='newAffiliate'>
        {referral_code}
      </span>
    </H2>
    <p>
      (Be sure to enter your friend's referral code so you both can get tokens.)
  </body>
</html>

还想将URL附加到代码的开始,但我假设这是标准的?

感谢您看一下帮助我解决这个问题!

This is the variable on the index.js: /* a unique referral code the user can share */ let referral_code = shortid.generate();

I just want to the pull it onto the index.ejs page like $({referrer_code}) or whatever would be appropriate to then get the variable from the index.js page. I'm just having trouble with the simple task of passing the referrer_code variable into the express web page template.

A bit new to React and js and could use some guidance.

Please help me pull my basic js variable into the ejs express theme page.

All I want to do is take the variable called referral_code and place it on the page as visible html text.

The variable I'm trying to display directly in html form on the theme can be seen on the index.js page, where it says:
"let referral_code = shortid.generate();"

Here's the full index page:

var router = express.Router();
var mysql = require('mysql');
var shortid = require('shortid');

/* New POST route for form submissions */
router.post('/', function(req, res, next) {

  /* establish mysql connection */
  var connection = mysql.createConnection({
    host: process.env.DB_HOST,
    user: process.env.DB_USER,
    password: process.env.DB_PASS,
    database: process.env.DB_NAME
  });

  /* user's email address */
  let email = req.body.email;

  /* a unique referral code the user can share */
  let referral_code = shortid.generate();

  /* the referral code a user submitted (might be null) */ 
  let referrer = req.body.referrer;

  /* add user to the database with INSERT */
  let query = "INSERT INTO `users` (`email`, `referral_code`, `referrer`) VALUES (?, ?, ?)";
  connection.query(query, [email, referral_code, referrer], (err, rows) => {
    connection.end(function() {
      if (err) return next();
      return res.send({referralCode: referral_code});
    });
  });
});

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index');
});

module.exports = router;

Here's the theme page I'm trying to update. You can see in the code where I added: {referral_code} and that is where I wnat to display the referral_code.

<!DOCTYPE html>
<html>
  <head>
    <title>The Starseeds Referral App</title>
    <link rel='stylesheet' href='/stylesheets/style.css' />
    <script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
  </head>
  <body>
    <h1>SignUp to Earn Your Free Tokens!</h1>
    <input type="email" id="email" placeholder="Enter your email...">
    <input type="text" id=" as" placeholder="Referral code">
    <button id="submit">Sign up</button>
    <script>
      $('#submit').on('click', function(e) {
        e.preventDefault();
        $.ajax({
          url: '/',
          type: "POST",
          data: {
            'email': $('#email').val(),
            'referrer': $('#387jh6g').val()       
          },
          "newAffiliate" = res.referralCode,
          success: function(res) {
            alert('Your referral code is: ' + res.referralCode);
          },
          error: function(jqXHR, textStatus, errorMessage) {
            alert(errorMessage);
          }
        });
      })
    </script>
    <H2>
      <label>Your referral code: </label>
      <span id='newAffiliate'>
        {referral_code}
      </span>
    </H2>
    <p>
      (Be sure to enter your friend's referral code so you both can get tokens.)
  </body>
</html>

Would also like to append url to the beggining of the code, but I'm assuming that is standard?

Thanks for taking a look to help me solve this!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文