javascript - 使用javascript从数据库保存和检索用户输入?

标签 javascript html database

我正在开发一个小型临时网站,该网站将用于收集一些姓名。

这个想法是,人们填写类(class)编号和姓名,一旦按下按钮,这两个值就会添加到页面中,并且每个访问该网站的人都可以查看。

我已经使用 javascript 对此有了一个概念:www.googledrive.com/host/0B_eZKT0Ni3-tOXF5OVVQeWZRRjQ

唯一的问题是这些项目并未真正存储在网站上。据我所知,只能使用数据库来完成此操作,但我没有将数据库链接到网页的经验。

有人可以帮我解决这个问题吗?或者有人知道我可以找到解决方案的来源吗?我的搜索一无所获。

如果我听起来像“帮助吸血鬼”,我很抱歉。我只是向你们寻求解决方案,因为我在其他地方找不到它。

HTML:

<body>
<div id="wrapper">
    <div id="header">
        <h2 id="title">Italiëreis 2015: opdiening tijdens quiz</h2>
    </div>  
    <div id="content">
        <!-- eerste ploeg -->
        <div class="L">
            <p id="kop">Ploeg 1</p>
            <p class="klas"><input type="text" id="klas1" class="text" maxlength="2" placeholder="Klas"/></p>
            <p class="naam"><input type="text" id="naam1" class="text" placeholder="Naam"/></p>
            <input type="button" onclick="changeText1()" value="Schrijf in" class="button" />
            <br>
            <p>Reeds ingeschreven mensen:</p>
            <div class="overflow">
                <ol id="lijst1"></ol>
            </div>
        </div>
        <!-- tweede ploeg -->
        <div class="L">
            <p id="kop">Ploeg 2</p>
            <p class="klas"><input type="text" id="klas2" class="text" maxlength="2" placeholder="Klas"/></p>
            <p class="naam"><input type="text" id="naam2" class="text" placeholder="Naam"/></p>
            <input type="button" onclick="changeText2()" value="Schrijf in" class="button"/>
            <br>
            <p>Reeds ingeschreven mensen:</p>
            <div class="overflow">
                <ol id="lijst2"></ol>
            </div>
         </div>
        <!-- derde ploeg -->
        <div class="L">
            <p id="kop">Ploeg 3</p>
            <p class="klas"><input type="text" id="klas3" class="text" maxlength="2" placeholder="Klas"/></p>
            <p class="naam"><input type="text" id="naam3" class="text" placeholder="Naam"/></p>
            <input type="button" onclick="changeText3()" value="Schrijf in" class="button"/>
            <br>
            <p>Reeds ingeschreven mensen:</p>
            <div class="overflow">
                <ol id="lijst3"></ol>
            </div>
        </div>
        <!-- vierde ploeg -->
        <div class="L">
            <p id="kop">Ploeg 4</p>
            <p class="klas"><input type="text" id="klas4" class="text" maxlength="2" placeholder="Klas"/></p>
            <p class="naam"><input type="text" id="naam4" class="text" placeholder="Naam"/></p>
            <input type="button" onclick="changeText4()" value="Schrijf in" class="button"/>
            <br>
            <p>Reeds ingeschreven mensen:</p>
            <div class="overflow">
                <ol id="lijst4"></ol>
            </div>
        </div>
    </div>
    <div id="footer">
        <div id="credits">Code geschreven door Bert-Jan van Dronkelaar - 6E</div>
    </div>
</div>

CSS 无关紧要。

Javascript:

        //eerste ploeg
    function changeText1() {
        var klas1 = document.getElementById('klas1').value;
        var naam1 = document.getElementById('naam1').value;
        if (document.getElementById('klas1').value != "" && document.getElementById('naam1').value != "") {
            var node = document.createElement("LI");
            var textnode1 = document.createTextNode(klas1 + " " + naam1);
            node.appendChild(textnode1);
            document.getElementById("lijst1").appendChild(node);
        }
    }
    //tweede ploeg
    function changeText2() {
        var klas2 = document.getElementById('klas2').value;
        var naam2 = document.getElementById('naam2').value;
        if (document.getElementById('klas2').value != "" && document.getElementById('naam2').value != "") {
            var node = document.createElement("LI");
            var textnode2 = document.createTextNode(klas2 + " " + naam2);
            node.appendChild(textnode2);
            document.getElementById("lijst2").appendChild(node);
        }
    }
    //derde ploeg
    function changeText3() {
        var klas3 = document.getElementById('klas3').value;
        var naam3 = document.getElementById('naam3').value;
        if (document.getElementById('klas3').value != "" && document.getElementById('naam3').value != "") {
            var node = document.createElement("LI");
            var textnode3 = document.createTextNode(klas3 + " " + naam3);
            node.appendChild(textnode3);
            document.getElementById("lijst3").appendChild(node);
        }
    }
    //vierde ploeg
    function changeText4() {
        var klas4 = document.getElementById('klas4').value;
        var naam4 = document.getElementById('naam4').value;
        if (document.getElementById('klas4').value != "" && document.getElementById('naam4').value != "") {
            var node = document.createElement("LI");
            var textnode4 = document.createTextNode(klas4 + " " + naam4);
            node.appendChild(textnode4);
            document.getElementById("lijst4").appendChild(node);
        }
    }

最佳答案

@SpencerWieczorek 没有错,PHPMySql会满足你的需要。然而,那里有一些学习曲线。

对于初学者,我建议使用 Parse 。它是免费的,并且使保存和检索数据变得轻而易举。下面是一个简单的应用程序,允许用户输入类(class)年份和姓名并保存它们,以便其他人可以在同一页面上看到它们。

由于 SO 限制,此处的代码片段将无法工作...

...但这里是 working jsfiddle

这是通过普通的 ole javascript 顺便说一句来完成的


要自行完成此操作,您需要:

  1. 您需要访问 https://www.parse.com/#signup并与他们创建一个帐户

  2. 转到https://www.parse.com/apps/new并创建一个应用程序

  3. 将其添加到 html 的 head 标记 <script type="text/javascript" src="https://www.parsecdn.com/js/parse-1.3.0.min.js"></script>

  4. 转到https://www.parse.com/apps/quickstart#parse_data/web/new ,从下拉列表(右上方)中选择您的应用程序 Parse.initialize()此处将显示该函数以及您应用的应用程序 IDJavaScript key ,复制此行以供稍后使用

  5. 替换 Parse.initialize()在我的示例中使用您在步骤 4 中复制的函数

  6. 阅读他们的 javascript guide here看看你能用 parse 做什么

  7. 要更深入地了解,请查看 Parse JavaScript SDK & Cloud Code Reference

如果您愿意,您还可以与其他脚本语言进行解析交互。


Parse 在达到一定的使用量之前是免费的。我有一款应用程序,每天都有 100 多名用户使用,而且几乎不需要支付任何费用。

Parse.initialize("Application ID", "JavaScript key");

function saveInput(){
        //get our new values 
        var klassYear = document.getElementById('klassYear').value.trim();
        var studentName = document.getElementById('studentName').value.trim();
  
        
       // dont continue if either value is blank
       if(klassYear=="" ||studentName=="" ){
           alert ('Please fill in both fields.') ;
           return; 
       }
       
   
        // create the `Parse` object
        var Klass = Parse.Object.extend("Klass");
		var _klass = new Klass();
		
        // set the object's values to our input values
		_klass.set("klassYear", klassYear);
		_klass.set("studentName", studentName);
		
        // save the object
		_klass.save(null, {
		  success: function(_klass) {
            // if the save succeeded, add the new info to our page
            retrieveSavedInputs()
            
		  },
		  error: function(_klass, error) {
            // save failed, do error handeling here
			console.log('Failed to create new object, with error code: ' + error.message);
		  }
	    });
  }


function retrieveSavedInputs(){
     
      // create a query to search for  our `Klass` items
      var Klass = Parse.Object.extend("Klass");
      var query = new Parse.Query(Klass);
          query.find({
			success: function(results) {
            
              // get our table's `tbody`  and clear it
              var myTbl = document.getElementById('mytbl');
              myTbl.innerHTML='';
              
              // `results` is an array of all the matches
              // loop through each
              for(var i =0; i < results.length; i++){
                
                  // get the values from the saved object
                  // note that `klassYear` and `studentName` 
                  // are not available within the scope of the `success` function
                  var k = results[i].get("klassYear")
                  var s = results[i].get("studentName")
            
                  // create a table row with the info and add it at the top of `contents`
                 myTbl.innerHTML = '<tr><td>'+k+'</td><td>'+s+'</td></tr>' + myTbl.innerHTML;
                  
                              
              }
			},
			error: function(error) {
			console.log("Error: " + error.code + " " + error.message);
			}
      });
}

// load all previously saved items
window.onload = retrieveSavedInputs(); 

//clcik handeler for the btn
document.getElementById("myBtn").addEventListener('click', saveInput , false);
table{
  margin-top:50px;
  }
 Class Year: <input type="text" id="klassYear"  value=""/> <br>
 Name: <input type="text" id="studentName" value=""/> <br>


 <input type="button" id="myBtn" value="Submit" class="button" />
<br> Add a ame and year above and see it added to the list below
    
<div id="myDiv"></div>


<table  width="400" border="1">
   <thead>    
       <tr>
          <th scope="col">Class Year</th>
          <th scope="col">Student Name</th>
       </tr>
  </thead>     
  <tbody id="mytbl">
  </tbody>
</table> Class Year: <input type="text" id="klassYear"  value=""/> <br>
 Name: <input type="text" id="studentName" value=""/> <br>


 <input type="button" id="myBtn" value="Submit" class="button" />
<br> Add a ame and year above and see it added to the list below
    
<div id="myDiv"></div>


<table  width="400" border="1">
   <thead>    
       <tr>
          <th scope="col">Class Year</th>
          <th scope="col">Student Name</th>
       </tr>
  </thead>     
  <tbody id="mytbl">
  </tbody>
</table>

关于javascript - 使用javascript从数据库保存和检索用户输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27881449/

相关文章:

javascript - 如何在javascript中使用OR语句

javascript - 更改 Bootstrap slider 中选定部分的颜色

javascript - 用js处理mailchimp的ajax查询

html/css - 居中、水平的 img 列表,每个 img 下都有文本

php - 无法弄清楚如何在 Laravel 上设置数据库

javascript - 存储附加到用户贡献内容的 YouTube 内容的最佳方法是什么?

javascript - 如何防止 HTML 忽略来自 JavaScript 的换行符?

javascript - 如何使用 html 文档中的 javascript 动态寻址单词/字符串,然后对其进行标记?

产品聚合器的数据库设计

mysql - mysql 中前一天的记录计数