将表单数据发送到Parse.com

时间:2014-03-23 20:41:43

标签: javascript parse-platform

我有一个我创建的表单,我想将表单信息发送到名为Parse.com的后端数据库。我在Parse中使用与表单上的字段相同的名称创建表,但我不确定如何使用js将其发送到Parse。

<form id="contact-form" class="contact-form" method="post" action="" onSubmit="return checkMail()" name="validation">
            <div class="form-group row" id="price">                
                <div class="col-lg-4">
                    <input type="text" name="fname" class="form-control" id="name" placeholder="First *" required >
                </div>

                <div class="col-lg-4">
                    <input type="text" name="lname" class="form-control" id="name" placeholder="Last *" required>
                </div>

                <div class="col-lg-4">
                    <input type="text" name="email" class="form-control" id="name" placeholder="E-mail *"  required>
                </div>                
            </div>

            </div>
            <div class="form-group row" align="center">
              <div class="col-lg-12" align="center">                
                <button type="submit" class="button default">SEND <i class="glyphicon glyphicon-send"></i></button>
              </div>
            </div>

2 个答案:

答案 0 :(得分:3)

将您的解析对象保存功能附加到表单提交。这可以通过使用JQuery轻松实现。

接下来,您必须捕获表单输入数据,然后将其保存到Parse对象。

此脚本假设您已使用fname,lname和email的[string]列在parse中创建了一个类。

    <script>
        Parse.initialize("API KEY", "JAVASCRIPT KEY");

        var ParseObj = Parse.Object.extend('myClass'); //create local parse object from your Parse class

        $('#contact-form').submit(function(e) {
        //on form submit
            e.preventDefault();

            //get data from form
            var data = {
                fname: $("#fname").val(),
                lname: $("#lname").val(),
                email: $("#email").val()
            };

            //create new Parse object
            parseObj = new ParseObj();

            //match the key values from the form, to your parse class, then save it
            parseObj.save(data, {
                    //if successful
                    success: function(parseObj) {
                            alert(parseObj.get('fname') + " " + parseObj.get('lname') + " " + parseObj.get('email') + " saved to Parse.")
                        }
                    ,
                    error: function(parseObj, error) {
                        console.log(parseObj);
                        console.log(error);
                    }
                }
            );
        });
    </script>

答案 1 :(得分:-1)

这是一个典型的“过于宽泛”的问题,因为您并没有真正遇到特定问题,只是要求我们为您编写代码。我能做的最好的就是指向parse.com用户指南,该指南向您展示了如何做到这一点。检查一下,自己尝试一下,如果您对代码有特定问题,请再次询问。

从此处的用户指南剪切的示例:https://parse.com/docs/js_guide#objects-saving

var GameScore = Parse.Object.extend("GameScore");
var gameScore = new GameScore();

gameScore.set("score", 1337);
gameScore.set("playerName", "Sean Plott");
gameScore.set("cheatMode", false);

gameScore.save(null, {
  success: function(gameScore) {
    // Execute any logic that should take place after the object is saved.
    alert('New object created with objectId: ' + gameScore.id);
  },
  error: function(gameScore, error) {
    // Execute any logic that should take place if the save fails.
    // error is a Parse.Error with an error code and description.
    alert('Failed to create new object, with error code: ' + error.description);
  }
});