【问题标题】:Pass javascript variable data to MySQL database in this situation在这种情况下将 javascript 变量数据传递给 MySQL 数据库
【发布时间】:2021-01-04 15:15:51
【问题描述】:

我遵循了一个 codepen 项目来构建一个动画表单。我可以知道如何将答案存储到我的 SQL 数据库中吗?答案存储在questions 数组中,键为answer,但我不确定如何提取它们。谢谢!

var questions = [
  {question:"What's your first name?"},
  {question:"What's your last name?"},
  {question:"What's your email?", pattern: /^[^\s@]+@[^\s@]+\.[^\s@]+$/},
  {question:"Create your password", type: "password"}
]

var onComplete = function() {

    var h1 = document.createElement('h1')
    h1.appendChild(document.createTextNode('Thanks ' + questions[0].answer + ' for checking this pen out!'))
    setTimeout(function() {
      register.parentElement.appendChild(h1)
      setTimeout(function() { h1.style.opacity = 1 }, 50)
    }, 1000)

}

;(function(questions, onComplete) {

    var tTime = 100 // transition transform time from #register in ms
    var wTime = 200 // transition width time from #register in ms
    var eTime = 1000 // transition width time from inputLabel in ms

    if (questions.length == 0) return

    var position = 0

    putQuestion()

    forwardButton.addEventListener('click', validate)
    inputField.addEventListener('keyup', function(e) {
        transform(0, 0) // ie hack to redraw
        if (e.keyCode == 13) validate()
    })

    previousButton.addEventListener('click', function(e) {
        if (position === 0) return
        position -= 1
        hideCurrent(putQuestion)
    })

    function putQuestion() {
        inputLabel.innerHTML = questions[position].question
        inputField.type = questions[position].type || 'text'
        inputField.value = questions[position].answer || ''
        inputField.focus()

        progress.style.width = position * 100 / questions.length + '%'

        previousButton.className = position ? 'ion-android-arrow-back' : 'ion-person'

        showCurrent()

    }

}(questions, onComplete))

【问题讨论】:

  • 如果您将答案与问题一样存储。尝试使用 forEach。使用 Jquery 对它们进行 Ajax 的功能。 w3schools 的 Exert 遵循 var txt = ""; var 数字 = [45, 4, 9, 16, 25]; numbers.forEach(myFunction);函数 myFunction(value, index, array) { txt = txt + value + "
    ";请注意,该函数有 3 个参数: 项目值 项目索引 数组本身
  • 像上面的例子一样,尝试为数组中的每条记录发送 ajax 到你的服务器脚本
  • 该评论似乎可以作为Undry的答案。您应该发布它并获得信誉!
  • 谢谢@Undry。我试图弄清楚这一点,但我刚刚开始学习 javascript,所以不太了解如何练习你的答案。如果您不介意,是否可以发布代码以供参考?非常感谢,不便之处,敬请原谅。

标签: javascript php mysql forms


【解决方案1】:

为了让它工作,您需要为您的网站提供 jquery 支持

尝试执行以下操作:

  1. 假设您将变量存储在 JS 数组中,例如

var numbers = [45, 4, 9, 16, 25];

  1. 您可以尝试使用内置的 JS 函数循环遍历数组,如:

numbers.forEach(myFunction);

  1. 您定义了您在第 2 点中使用的函数,使用 ajax,smth 之类的
myFunction(value){ 
// answers is used to indicate to your server side script type of operation to be performed to be use in isset(), as value is too general`

  var datastring = 'answers' + '&value=' + value;
  // in URL indicate path to your actual server side script that will put records in database

  ajax({
    type: "POST",
    url: "/app/server.php",   
    data: datastring,
    success: function (html) {
      console.log(html);
    }
  }); //End of Ajax
  
  return false;
}

【讨论】:

  • 不知道为什么格式不好,试了好几次
猜你喜欢
  • 2015-08-24
  • 1970-01-01
  • 2012-10-31
  • 2013-10-06
  • 1970-01-01
  • 2011-10-14
  • 2021-09-14
  • 2014-09-22
  • 2019-11-12
相关资源
最近更新 更多