问题描述:

I have a form that I created and I want to send that form information to a backend database called Parse.com. I create the table in Parse with the same names as the fields on the form, but I'm not sure how to send it to Parse using js.

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

网友答案:

Attach your parse object save function to the form submit. This can be achieved with ease by using JQuery.

Next you have to capture the form input data, then save it to your Parse object.

This script assumes you have created a class in parse with the [string] columns of fname, lname and email.

    <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>
网友答案:

This is a typical "too broad" question, as you're not really having a specific problem but just asking us to write the code for you. Best I can do is point you to the parse.com user guide which shows you how you can do this. Check it out, try it for yourself and then ask here again if you have specific issues with the code.

Example snipped from the user guide found here: 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);
  }
});
相关阅读:
Top