简体   繁体   English

在这种情况下将javascript变量数据传递给MySQL数据库

[英]Pass javascript variable data to MySQL database in this situation

I have followed a codepen project to build an animated form.我遵循了一个 codepen 项目来构建一个动画表单。 May I know how can I store the answer to my SQL database?我可以知道如何将答案存储到我的 SQL 数据库中吗? The answers are stored in the questions array with the key answer but I am not sure how to extract them.答案存储在在questions阵列的关键answer ,但我不知道如何提取它们。 Thanks!谢谢!

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))

In order for it to work you need jquery support for your website为了使其正常工作,您的网站需要 jquery 支持

Try doing following:尝试执行以下操作:

  1. Assume you are storing your variables in JS array like假设您将变量存储在 JS 数组中,例如

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

  1. You can try using built-in JS funtion to cycle through the array like:您可以尝试使用内置的 JS 函数来循环遍历数组,例如:

numbers.forEach(myFunction);

  1. You define your function that you use in point 2, with ajax, smth like你定义你在第 2 点中使用的函数,使用 ajax,就像
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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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